blob: 52a194780050dbc8b471828c8acba6bbf478e930 [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
Alex Blighcd758dd2013-08-21 16:02:44 +010044#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
45#include <sys/prctl.h>
46#endif
47
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010048/***********************************************************/
49/* timers */
50
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010051struct QEMUClock {
Paolo Bonzini688eb382011-09-13 11:42:26 +020052 QEMUTimer *active_timers;
Jan Kiszka691a0c92011-06-20 14:06:27 +020053
54 NotifierList reset_notifiers;
55 int64_t last;
Stefan Weil9a14b292012-04-20 11:51:58 +020056
57 int type;
58 bool enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010059};
60
61struct QEMUTimer {
Paolo Bonzini4a998742011-03-11 16:33:58 +010062 int64_t expire_time; /* in nanoseconds */
Stefan Weil9a14b292012-04-20 11:51:58 +020063 QEMUClock *clock;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010064 QEMUTimerCB *cb;
65 void *opaque;
Stefan Weil9a14b292012-04-20 11:51:58 +020066 QEMUTimer *next;
67 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010068};
69
70struct qemu_alarm_timer {
71 char const *name;
72 int (*start)(struct qemu_alarm_timer *t);
73 void (*stop)(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010074 void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns);
Stefan Weilcd0544e2011-04-10 20:15:09 +020075#if defined(__linux__)
Stefan Weilcd0544e2011-04-10 20:15:09 +020076 timer_t timer;
Stefan Weil9a14b292012-04-20 11:51:58 +020077 int fd;
Stefan Weilcd0544e2011-04-10 20:15:09 +020078#elif defined(_WIN32)
79 HANDLE timer;
80#endif
Stefan Weil5e1ec7b2012-04-20 10:45:48 +020081 bool expired;
82 bool pending;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010083};
84
85static struct qemu_alarm_timer *alarm_timer;
86
Alex Blighe93379b2013-08-21 16:02:39 +010087static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
Stefan Weil45c7b372011-03-24 21:31:24 +010088{
89 return timer_head && (timer_head->expire_time <= current_time);
90}
91
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010092static int64_t qemu_next_alarm_deadline(void)
93{
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010094 int64_t delta = INT64_MAX;
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010095 int64_t rtdelta;
96
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +010097 if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +010098 delta = vm_clock->active_timers->expire_time -
99 qemu_get_clock_ns(vm_clock);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100100 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100101 if (host_clock->enabled && host_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100102 int64_t hdelta = host_clock->active_timers->expire_time -
103 qemu_get_clock_ns(host_clock);
104 if (hdelta < delta) {
105 delta = hdelta;
106 }
107 }
Stefano Stabellini4ffd16f2012-04-13 19:35:03 +0100108 if (rt_clock->enabled && rt_clock->active_timers) {
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100109 rtdelta = (rt_clock->active_timers->expire_time -
110 qemu_get_clock_ns(rt_clock));
111 if (rtdelta < delta) {
112 delta = rtdelta;
113 }
114 }
115
116 return delta;
117}
118
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100119static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
120{
Stefano Stabellini82274212012-05-29 03:35:24 +0000121 int64_t nearest_delta_ns = qemu_next_alarm_deadline();
122 if (nearest_delta_ns < INT64_MAX) {
123 t->rearm(t, nearest_delta_ns);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100124 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100125}
126
Paolo Bonzini9c132462011-02-03 14:48:59 +0100127/* TODO: MIN_TIMER_REARM_NS should be optimized */
128#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100129
130#ifdef _WIN32
131
Stefan Weil2f9cba02011-04-05 18:34:21 +0200132static int mm_start_timer(struct qemu_alarm_timer *t);
133static void mm_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100134static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200135
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100136static int win32_start_timer(struct qemu_alarm_timer *t);
137static void win32_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100138static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100139
140#else
141
142static int unix_start_timer(struct qemu_alarm_timer *t);
143static void unix_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100144static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100145
146#ifdef __linux__
147
148static int dynticks_start_timer(struct qemu_alarm_timer *t);
149static void dynticks_stop_timer(struct qemu_alarm_timer *t);
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100150static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100151
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100152#endif /* __linux__ */
153
154#endif /* _WIN32 */
155
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100156static struct qemu_alarm_timer alarm_timers[] = {
157#ifndef _WIN32
158#ifdef __linux__
159 {"dynticks", dynticks_start_timer,
Stefan Weilcd0544e2011-04-10 20:15:09 +0200160 dynticks_stop_timer, dynticks_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100161#endif
Paolo Bonzini84682832011-06-09 13:10:25 +0200162 {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100163#else
Paolo Bonzinicca5de72011-11-09 12:46:56 +0100164 {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
Stefan Weilcd0544e2011-04-10 20:15:09 +0200165 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100166#endif
167 {NULL, }
168};
169
170static void show_available_alarms(void)
171{
172 int i;
173
174 printf("Available alarm timers, in order of precedence:\n");
175 for (i = 0; alarm_timers[i].name; i++)
176 printf("%s\n", alarm_timers[i].name);
177}
178
179void configure_alarms(char const *opt)
180{
181 int i;
182 int cur = 0;
183 int count = ARRAY_SIZE(alarm_timers) - 1;
184 char *arg;
185 char *name;
186 struct qemu_alarm_timer tmp;
187
Peter Maydellc8057f92012-08-02 13:45:54 +0100188 if (is_help_option(opt)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100189 show_available_alarms();
190 exit(0);
191 }
192
Anthony Liguori7267c092011-08-20 22:09:37 -0500193 arg = g_strdup(opt);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100194
195 /* Reorder the array */
196 name = strtok(arg, ",");
197 while (name) {
198 for (i = 0; i < count && alarm_timers[i].name; i++) {
199 if (!strcmp(alarm_timers[i].name, name))
200 break;
201 }
202
203 if (i == count) {
204 fprintf(stderr, "Unknown clock %s\n", name);
205 goto next;
206 }
207
208 if (i < cur)
209 /* Ignore */
210 goto next;
211
212 /* Swap */
213 tmp = alarm_timers[i];
214 alarm_timers[i] = alarm_timers[cur];
215 alarm_timers[cur] = tmp;
216
217 cur++;
218next:
219 name = strtok(NULL, ",");
220 }
221
Anthony Liguori7267c092011-08-20 22:09:37 -0500222 g_free(arg);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100223
224 if (cur) {
225 /* Disable remaining timers */
226 for (i = cur; i < count; i++)
227 alarm_timers[i].name = NULL;
228 } else {
229 show_available_alarms();
230 exit(1);
231 }
232}
233
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100234QEMUClock *rt_clock;
235QEMUClock *vm_clock;
236QEMUClock *host_clock;
237
Alex Bligh58ac56b2013-08-21 16:02:40 +0100238static QEMUClock *qemu_clock_new(int type)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100239{
240 QEMUClock *clock;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200241
Anthony Liguori7267c092011-08-20 22:09:37 -0500242 clock = g_malloc0(sizeof(QEMUClock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100243 clock->type = type;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200244 clock->enabled = true;
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200245 clock->last = INT64_MIN;
Jan Kiszka691a0c92011-06-20 14:06:27 +0200246 notifier_list_init(&clock->reset_notifiers);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100247 return clock;
248}
249
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200250void qemu_clock_enable(QEMUClock *clock, bool enabled)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100251{
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200252 bool old = clock->enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100253 clock->enabled = enabled;
Paolo Bonzinifbdc14e2011-09-27 18:23:14 +0200254 if (enabled && !old) {
255 qemu_rearm_alarm_timer(alarm_timer);
256 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100257}
258
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200259int64_t qemu_clock_has_timers(QEMUClock *clock)
260{
261 return !!clock->active_timers;
262}
263
264int64_t qemu_clock_expired(QEMUClock *clock)
265{
266 return (clock->active_timers &&
267 clock->active_timers->expire_time < qemu_get_clock_ns(clock));
268}
269
270int64_t qemu_clock_deadline(QEMUClock *clock)
271{
272 /* To avoid problems with overflow limit this to 2^32. */
273 int64_t delta = INT32_MAX;
274
Alex Bligh043a7e12013-08-21 16:02:42 +0100275 if (clock->enabled && clock->active_timers) {
Paolo Bonzinidc2dfcf2011-09-12 15:50:16 +0200276 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
277 }
278 if (delta < 0) {
279 delta = 0;
280 }
281 return delta;
282}
283
Alex Bligh02a03a92013-08-21 16:02:41 +0100284/*
285 * As above, but return -1 for no deadline, and do not cap to 2^32
286 * as we know the result is always positive.
287 */
288
289int64_t qemu_clock_deadline_ns(QEMUClock *clock)
290{
291 int64_t delta;
292
293 if (!clock->enabled || !clock->active_timers) {
294 return -1;
295 }
296
297 delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock);
298
299 if (delta <= 0) {
300 return 0;
301 }
302
303 return delta;
304}
305
306/* Transition function to convert a nanosecond timeout to ms
307 * This is used where a system does not support ppoll
308 */
309int qemu_timeout_ns_to_ms(int64_t ns)
310{
311 int64_t ms;
312 if (ns < 0) {
313 return -1;
314 }
315
316 if (!ns) {
317 return 0;
318 }
319
320 /* Always round up, because it's better to wait too long than to wait too
321 * little and effectively busy-wait
322 */
323 ms = (ns + SCALE_MS - 1) / SCALE_MS;
324
325 /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
326 if (ms > (int64_t) INT32_MAX) {
327 ms = INT32_MAX;
328 }
329
330 return (int) ms;
331}
332
333
Alex Bligh4e0c6522013-08-21 16:02:43 +0100334/* qemu implementation of g_poll which uses a nanosecond timeout but is
335 * otherwise identical to g_poll
336 */
337int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout)
338{
339#ifdef CONFIG_PPOLL
340 if (timeout < 0) {
341 return ppoll((struct pollfd *)fds, nfds, NULL, NULL);
342 } else {
343 struct timespec ts;
344 ts.tv_sec = timeout / 1000000000LL;
345 ts.tv_nsec = timeout % 1000000000LL;
346 return ppoll((struct pollfd *)fds, nfds, &ts, NULL);
347 }
348#else
349 return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout));
350#endif
351}
352
353
Paolo Bonzini4a998742011-03-11 16:33:58 +0100354QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
355 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100356{
357 QEMUTimer *ts;
358
Anthony Liguori7267c092011-08-20 22:09:37 -0500359 ts = g_malloc0(sizeof(QEMUTimer));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100360 ts->clock = clock;
361 ts->cb = cb;
362 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100363 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100364 return ts;
365}
366
367void qemu_free_timer(QEMUTimer *ts)
368{
Anthony Liguori7267c092011-08-20 22:09:37 -0500369 g_free(ts);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100370}
371
372/* stop a timer, but do not dealloc it */
373void qemu_del_timer(QEMUTimer *ts)
374{
375 QEMUTimer **pt, *t;
376
377 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100378 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200379 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100380 for(;;) {
381 t = *pt;
382 if (!t)
383 break;
384 if (t == ts) {
385 *pt = t->next;
386 break;
387 }
388 pt = &t->next;
389 }
390}
391
392/* modify the current timer so that it will be fired when current_time
393 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200394void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100395{
396 QEMUTimer **pt, *t;
397
398 qemu_del_timer(ts);
399
400 /* add the timer in the sorted list */
401 /* NOTE: this code must be signal safe because
Alex Blighe93379b2013-08-21 16:02:39 +0100402 timer_expired() can be called from a signal. */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200403 pt = &ts->clock->active_timers;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100404 for(;;) {
405 t = *pt;
Alex Blighe93379b2013-08-21 16:02:39 +0100406 if (!timer_expired_ns(t, expire_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100407 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100408 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100409 pt = &t->next;
410 }
411 ts->expire_time = expire_time;
412 ts->next = *pt;
413 *pt = ts;
414
415 /* Rearm if necessary */
Paolo Bonzini688eb382011-09-13 11:42:26 +0200416 if (pt == &ts->clock->active_timers) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100417 if (!alarm_timer->pending) {
418 qemu_rearm_alarm_timer(alarm_timer);
419 }
420 /* Interrupt execution to force deadline recalculation. */
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200421 qemu_clock_warp(ts->clock);
422 if (use_icount) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100423 qemu_notify_event();
Paolo Bonziniab33fcd2011-04-13 10:03:44 +0200424 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100425 }
426}
427
Paolo Bonzini4a998742011-03-11 16:33:58 +0100428void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
429{
430 qemu_mod_timer_ns(ts, expire_time * ts->scale);
431}
432
Alex Blighe93379b2013-08-21 16:02:39 +0100433bool timer_pending(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100434{
435 QEMUTimer *t;
Paolo Bonzini688eb382011-09-13 11:42:26 +0200436 for (t = ts->clock->active_timers; t != NULL; t = t->next) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200437 if (t == ts) {
438 return true;
439 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100440 }
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200441 return false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100442}
443
Alex Blighe93379b2013-08-21 16:02:39 +0100444bool timer_expired(QEMUTimer *timer_head, int64_t current_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100445{
Alex Blighe93379b2013-08-21 16:02:39 +0100446 return timer_expired_ns(timer_head, current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100447}
448
Alex Blighf9a976b2013-08-21 16:02:45 +0100449bool qemu_run_timers(QEMUClock *clock)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100450{
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200451 QEMUTimer *ts;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100452 int64_t current_time;
Alex Blighf9a976b2013-08-21 16:02:45 +0100453 bool progress = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100454
455 if (!clock->enabled)
Alex Blighf9a976b2013-08-21 16:02:45 +0100456 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100457
Paolo Bonzini4a998742011-03-11 16:33:58 +0100458 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100459 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200460 ts = clock->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100461 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100462 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100463 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100464 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200465 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100466 ts->next = NULL;
467
468 /* run the callback (the timer list can be modified) */
469 ts->cb(ts->opaque);
Alex Blighf9a976b2013-08-21 16:02:45 +0100470 progress = true;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100471 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100472 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100473}
474
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100475int64_t qemu_get_clock_ns(QEMUClock *clock)
476{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200477 int64_t now, last;
478
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100479 switch(clock->type) {
480 case QEMU_CLOCK_REALTIME:
481 return get_clock();
482 default:
483 case QEMU_CLOCK_VIRTUAL:
484 if (use_icount) {
485 return cpu_get_icount();
486 } else {
487 return cpu_get_clock();
488 }
489 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200490 now = get_clock_realtime();
491 last = clock->last;
492 clock->last = now;
493 if (now < last) {
494 notifier_list_notify(&clock->reset_notifiers, &now);
495 }
496 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100497 }
498}
499
Jan Kiszka691a0c92011-06-20 14:06:27 +0200500void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
501{
502 notifier_list_add(&clock->reset_notifiers, notifier);
503}
504
505void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
506{
Paolo Bonzini31552522012-01-13 17:34:01 +0100507 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200508}
509
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100510void init_clocks(void)
511{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100512 if (!rt_clock) {
Alex Bligh58ac56b2013-08-21 16:02:40 +0100513 rt_clock = qemu_clock_new(QEMU_CLOCK_REALTIME);
514 vm_clock = qemu_clock_new(QEMU_CLOCK_VIRTUAL);
515 host_clock = qemu_clock_new(QEMU_CLOCK_HOST);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100516 }
Alex Blighcd758dd2013-08-21 16:02:44 +0100517#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
518 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
519#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520}
521
Alex Blighe93379b2013-08-21 16:02:39 +0100522uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100523{
Alex Blighe93379b2013-08-21 16:02:39 +0100524 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100525}
526
Alex Blighf9a976b2013-08-21 16:02:45 +0100527bool qemu_run_all_timers(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100528{
Alex Blighf9a976b2013-08-21 16:02:45 +0100529 bool progress = false;
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200530 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100531
Peter Portante158fd3c2012-04-05 11:00:45 -0400532 /* vm time timers */
Alex Blighf9a976b2013-08-21 16:02:45 +0100533 progress |= qemu_run_timers(vm_clock);
534 progress |= qemu_run_timers(rt_clock);
535 progress |= qemu_run_timers(host_clock);
Peter Portante158fd3c2012-04-05 11:00:45 -0400536
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100537 /* rearm timer, if not periodic */
538 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200539 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100540 qemu_rearm_alarm_timer(alarm_timer);
541 }
Alex Blighf9a976b2013-08-21 16:02:45 +0100542
543 return progress;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100544}
545
546#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100547static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100548#else
549static void host_alarm_handler(int host_signum)
550#endif
551{
552 struct qemu_alarm_timer *t = alarm_timer;
553 if (!t)
554 return;
555
Stefan Weil82051992012-04-20 11:27:24 +0200556 t->expired = true;
557 t->pending = true;
558 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100559}
560
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100561#if defined(__linux__)
562
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100563#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200564
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100565static int dynticks_start_timer(struct qemu_alarm_timer *t)
566{
567 struct sigevent ev;
568 timer_t host_timer;
569 struct sigaction act;
570
571 sigfillset(&act.sa_mask);
572 act.sa_flags = 0;
573 act.sa_handler = host_alarm_handler;
574
575 sigaction(SIGALRM, &act, NULL);
576
577 /*
578 * Initialize ev struct to 0 to avoid valgrind complaining
579 * about uninitialized data in timer_create call
580 */
581 memset(&ev, 0, sizeof(ev));
582 ev.sigev_value.sival_int = 0;
583 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000584#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200585 if (qemu_signalfd_available()) {
586 ev.sigev_notify = SIGEV_THREAD_ID;
587 ev._sigev_un._tid = qemu_get_thread_id();
588 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000589#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100590 ev.sigev_signo = SIGALRM;
591
592 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
593 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100594 return -1;
595 }
596
Stefan Weilcd0544e2011-04-10 20:15:09 +0200597 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100598
599 return 0;
600}
601
602static void dynticks_stop_timer(struct qemu_alarm_timer *t)
603{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200604 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100605
606 timer_delete(host_timer);
607}
608
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100609static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
610 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100611{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200612 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100613 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100614 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100615
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100616 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
617 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100618
619 /* check whether a timer is already running */
620 if (timer_gettime(host_timer, &timeout)) {
621 perror("gettime");
622 fprintf(stderr, "Internal timer error: aborting\n");
623 exit(1);
624 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100625 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
626 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100627 return;
628
629 timeout.it_interval.tv_sec = 0;
630 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100631 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
632 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100633 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
634 perror("settime");
635 fprintf(stderr, "Internal timer error: aborting\n");
636 exit(1);
637 }
638}
639
640#endif /* defined(__linux__) */
641
Stefan Weilf26e5a52011-02-04 22:01:32 +0100642#if !defined(_WIN32)
643
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100644static int unix_start_timer(struct qemu_alarm_timer *t)
645{
646 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100647
648 /* timer signal */
649 sigfillset(&act.sa_mask);
650 act.sa_flags = 0;
651 act.sa_handler = host_alarm_handler;
652
653 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200654 return 0;
655}
656
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100657static void unix_rearm_timer(struct qemu_alarm_timer *t,
658 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200659{
660 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200661 int err;
662
Paolo Bonzini84682832011-06-09 13:10:25 +0200663 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
664 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100665
666 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200667 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
668 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
669 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100670 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200671 if (err) {
672 perror("setitimer");
673 fprintf(stderr, "Internal timer error: aborting\n");
674 exit(1);
675 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100676}
677
678static void unix_stop_timer(struct qemu_alarm_timer *t)
679{
680 struct itimerval itv;
681
682 memset(&itv, 0, sizeof(itv));
683 setitimer(ITIMER_REAL, &itv, NULL);
684}
685
686#endif /* !defined(_WIN32) */
687
688
689#ifdef _WIN32
690
Stefan Weil2f9cba02011-04-05 18:34:21 +0200691static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000692static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200693
694static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
695 DWORD_PTR dwUser, DWORD_PTR dw1,
696 DWORD_PTR dw2)
697{
698 struct qemu_alarm_timer *t = alarm_timer;
699 if (!t) {
700 return;
701 }
Stefan Weil82051992012-04-20 11:27:24 +0200702 t->expired = true;
703 t->pending = true;
704 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200705}
706
707static int mm_start_timer(struct qemu_alarm_timer *t)
708{
Stefan Weil40f08e82012-04-27 05:34:40 +0000709 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200710 return 0;
711}
712
713static void mm_stop_timer(struct qemu_alarm_timer *t)
714{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100715 if (mm_timer) {
716 timeKillEvent(mm_timer);
717 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200718}
719
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100720static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200721{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100722 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000723 if (nearest_delta_ms < mm_tc.wPeriodMin) {
724 nearest_delta_ms = mm_tc.wPeriodMin;
725 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
726 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100727 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100728
Paolo Bonzini0727b862013-02-20 14:43:31 +0100729 if (mm_timer) {
730 timeKillEvent(mm_timer);
731 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000732 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
733 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200734 mm_alarm_handler,
735 (DWORD_PTR)t,
736 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
737
738 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200739 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000740 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200741 exit(1);
742 }
743}
744
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100745static int win32_start_timer(struct qemu_alarm_timer *t)
746{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100747 HANDLE hTimer;
748 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100749
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100750 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
751 is zero) that has already expired, the timer is not updated. Since
752 creating a new timer is relatively expensive, set a bogus one-hour
753 interval in the dynticks case. */
754 success = CreateTimerQueueTimer(&hTimer,
755 NULL,
756 host_alarm_handler,
757 t,
758 1,
Stefan Weil82051992012-04-20 11:27:24 +0200759 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100760 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100761
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100762 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100763 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
764 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100765 return -1;
766 }
767
Stefan Weilcd0544e2011-04-10 20:15:09 +0200768 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769 return 0;
770}
771
772static void win32_stop_timer(struct qemu_alarm_timer *t)
773{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200774 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100775
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100776 if (hTimer) {
777 DeleteTimerQueueTimer(NULL, hTimer, NULL);
778 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100779}
780
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100781static void win32_rearm_timer(struct qemu_alarm_timer *t,
782 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100783{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200784 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100785 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100786 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100787
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100788 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100789 if (nearest_delta_ms < 1) {
790 nearest_delta_ms = 1;
791 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100792 /* ULONG_MAX can be 32 bit */
793 if (nearest_delta_ms > ULONG_MAX) {
794 nearest_delta_ms = ULONG_MAX;
795 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100796 success = ChangeTimerQueueTimer(NULL,
797 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100798 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100799 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100800
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100801 if (!success) {
802 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100803 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100804 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100805 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100806
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100807}
808
809#endif /* _WIN32 */
810
Paolo Bonzini4260a732011-09-19 10:18:51 +0200811static void quit_timers(void)
812{
813 struct qemu_alarm_timer *t = alarm_timer;
814 alarm_timer = NULL;
815 t->stop(t);
816}
817
Stefan Weil253ecf82012-11-04 21:42:08 +0100818#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100819static void reinit_timers(void)
820{
821 struct qemu_alarm_timer *t = alarm_timer;
822 t->stop(t);
823 if (t->start(t)) {
824 fprintf(stderr, "Internal timer error: aborting\n");
825 exit(1);
826 }
827 qemu_rearm_alarm_timer(t);
828}
Stefan Weil253ecf82012-11-04 21:42:08 +0100829#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100830
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100831int init_timer_alarm(void)
832{
833 struct qemu_alarm_timer *t = NULL;
834 int i, err = -1;
835
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100836 if (alarm_timer) {
837 return 0;
838 }
839
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100840 for (i = 0; alarm_timers[i].name; i++) {
841 t = &alarm_timers[i];
842
843 err = t->start(t);
844 if (!err)
845 break;
846 }
847
848 if (err) {
849 err = -ENOENT;
850 goto fail;
851 }
852
Paolo Bonzini4260a732011-09-19 10:18:51 +0200853 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100854#ifdef CONFIG_POSIX
855 pthread_atfork(NULL, NULL, reinit_timers);
856#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100857 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100858 return 0;
859
860fail:
861 return err;
862}
863