blob: 74f904b310dd54e77a91ef77ec6d3683ff0505e4 [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
Paolo Bonzini8156be52012-03-28 15:42:04 +0200449void 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;
453
454 if (!clock->enabled)
455 return;
456
Paolo Bonzini4a998742011-03-11 16:33:58 +0100457 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100458 for(;;) {
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200459 ts = clock->active_timers;
Alex Blighe93379b2013-08-21 16:02:39 +0100460 if (!timer_expired_ns(ts, current_time)) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100461 break;
Stefan Weil45c7b372011-03-24 21:31:24 +0100462 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100463 /* remove timer from the list before calling the callback */
Paolo Bonzini144b97c2012-09-19 15:52:44 +0200464 clock->active_timers = ts->next;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100465 ts->next = NULL;
466
467 /* run the callback (the timer list can be modified) */
468 ts->cb(ts->opaque);
469 }
470}
471
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100472int64_t qemu_get_clock_ns(QEMUClock *clock)
473{
Jan Kiszka691a0c92011-06-20 14:06:27 +0200474 int64_t now, last;
475
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100476 switch(clock->type) {
477 case QEMU_CLOCK_REALTIME:
478 return get_clock();
479 default:
480 case QEMU_CLOCK_VIRTUAL:
481 if (use_icount) {
482 return cpu_get_icount();
483 } else {
484 return cpu_get_clock();
485 }
486 case QEMU_CLOCK_HOST:
Jan Kiszka691a0c92011-06-20 14:06:27 +0200487 now = get_clock_realtime();
488 last = clock->last;
489 clock->last = now;
490 if (now < last) {
491 notifier_list_notify(&clock->reset_notifiers, &now);
492 }
493 return now;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100494 }
495}
496
Jan Kiszka691a0c92011-06-20 14:06:27 +0200497void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
498{
499 notifier_list_add(&clock->reset_notifiers, notifier);
500}
501
502void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
503{
Paolo Bonzini31552522012-01-13 17:34:01 +0100504 notifier_remove(notifier);
Jan Kiszka691a0c92011-06-20 14:06:27 +0200505}
506
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100507void init_clocks(void)
508{
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100509 if (!rt_clock) {
Alex Bligh58ac56b2013-08-21 16:02:40 +0100510 rt_clock = qemu_clock_new(QEMU_CLOCK_REALTIME);
511 vm_clock = qemu_clock_new(QEMU_CLOCK_VIRTUAL);
512 host_clock = qemu_clock_new(QEMU_CLOCK_HOST);
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100513 }
Alex Blighcd758dd2013-08-21 16:02:44 +0100514#ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK
515 prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0);
516#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100517}
518
Alex Blighe93379b2013-08-21 16:02:39 +0100519uint64_t timer_expire_time_ns(QEMUTimer *ts)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100520{
Alex Blighe93379b2013-08-21 16:02:39 +0100521 return timer_pending(ts) ? ts->expire_time : -1;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100522}
523
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100524void qemu_run_all_timers(void)
525{
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200526 alarm_timer->pending = false;
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100527
Peter Portante158fd3c2012-04-05 11:00:45 -0400528 /* vm time timers */
529 qemu_run_timers(vm_clock);
530 qemu_run_timers(rt_clock);
531 qemu_run_timers(host_clock);
532
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100533 /* rearm timer, if not periodic */
534 if (alarm_timer->expired) {
Stefan Weil5e1ec7b2012-04-20 10:45:48 +0200535 alarm_timer->expired = false;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100536 qemu_rearm_alarm_timer(alarm_timer);
537 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100538}
539
540#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100541static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100542#else
543static void host_alarm_handler(int host_signum)
544#endif
545{
546 struct qemu_alarm_timer *t = alarm_timer;
547 if (!t)
548 return;
549
Stefan Weil82051992012-04-20 11:27:24 +0200550 t->expired = true;
551 t->pending = true;
552 qemu_notify_event();
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100553}
554
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100555#if defined(__linux__)
556
Paolo Bonzini1de7afc2012-12-17 18:20:00 +0100557#include "qemu/compatfd.h"
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200558
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100559static int dynticks_start_timer(struct qemu_alarm_timer *t)
560{
561 struct sigevent ev;
562 timer_t host_timer;
563 struct sigaction act;
564
565 sigfillset(&act.sa_mask);
566 act.sa_flags = 0;
567 act.sa_handler = host_alarm_handler;
568
569 sigaction(SIGALRM, &act, NULL);
570
571 /*
572 * Initialize ev struct to 0 to avoid valgrind complaining
573 * about uninitialized data in timer_create call
574 */
575 memset(&ev, 0, sizeof(ev));
576 ev.sigev_value.sival_int = 0;
577 ev.sigev_notify = SIGEV_SIGNAL;
Richard Henderson1e9737d2012-10-23 07:33:00 +1000578#ifdef CONFIG_SIGEV_THREAD_ID
Jan Kiszkad25f89c2011-06-17 11:25:49 +0200579 if (qemu_signalfd_available()) {
580 ev.sigev_notify = SIGEV_THREAD_ID;
581 ev._sigev_un._tid = qemu_get_thread_id();
582 }
Richard Henderson1e9737d2012-10-23 07:33:00 +1000583#endif /* CONFIG_SIGEV_THREAD_ID */
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100584 ev.sigev_signo = SIGALRM;
585
586 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
587 perror("timer_create");
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100588 return -1;
589 }
590
Stefan Weilcd0544e2011-04-10 20:15:09 +0200591 t->timer = host_timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100592
593 return 0;
594}
595
596static void dynticks_stop_timer(struct qemu_alarm_timer *t)
597{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200598 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100599
600 timer_delete(host_timer);
601}
602
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100603static void dynticks_rearm_timer(struct qemu_alarm_timer *t,
604 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100605{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200606 timer_t host_timer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100607 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100608 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100609
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100610 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
611 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100612
613 /* check whether a timer is already running */
614 if (timer_gettime(host_timer, &timeout)) {
615 perror("gettime");
616 fprintf(stderr, "Internal timer error: aborting\n");
617 exit(1);
618 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100619 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
620 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100621 return;
622
623 timeout.it_interval.tv_sec = 0;
624 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100625 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
626 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100627 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
628 perror("settime");
629 fprintf(stderr, "Internal timer error: aborting\n");
630 exit(1);
631 }
632}
633
634#endif /* defined(__linux__) */
635
Stefan Weilf26e5a52011-02-04 22:01:32 +0100636#if !defined(_WIN32)
637
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100638static int unix_start_timer(struct qemu_alarm_timer *t)
639{
640 struct sigaction act;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100641
642 /* timer signal */
643 sigfillset(&act.sa_mask);
644 act.sa_flags = 0;
645 act.sa_handler = host_alarm_handler;
646
647 sigaction(SIGALRM, &act, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200648 return 0;
649}
650
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100651static void unix_rearm_timer(struct qemu_alarm_timer *t,
652 int64_t nearest_delta_ns)
Paolo Bonzini84682832011-06-09 13:10:25 +0200653{
654 struct itimerval itv;
Paolo Bonzini84682832011-06-09 13:10:25 +0200655 int err;
656
Paolo Bonzini84682832011-06-09 13:10:25 +0200657 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
658 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100659
660 itv.it_interval.tv_sec = 0;
Paolo Bonzini84682832011-06-09 13:10:25 +0200661 itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */
662 itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
663 itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100664 err = setitimer(ITIMER_REAL, &itv, NULL);
Paolo Bonzini84682832011-06-09 13:10:25 +0200665 if (err) {
666 perror("setitimer");
667 fprintf(stderr, "Internal timer error: aborting\n");
668 exit(1);
669 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100670}
671
672static void unix_stop_timer(struct qemu_alarm_timer *t)
673{
674 struct itimerval itv;
675
676 memset(&itv, 0, sizeof(itv));
677 setitimer(ITIMER_REAL, &itv, NULL);
678}
679
680#endif /* !defined(_WIN32) */
681
682
683#ifdef _WIN32
684
Stefan Weil2f9cba02011-04-05 18:34:21 +0200685static MMRESULT mm_timer;
Stefan Weil40f08e82012-04-27 05:34:40 +0000686static TIMECAPS mm_tc;
Stefan Weil2f9cba02011-04-05 18:34:21 +0200687
688static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
689 DWORD_PTR dwUser, DWORD_PTR dw1,
690 DWORD_PTR dw2)
691{
692 struct qemu_alarm_timer *t = alarm_timer;
693 if (!t) {
694 return;
695 }
Stefan Weil82051992012-04-20 11:27:24 +0200696 t->expired = true;
697 t->pending = true;
698 qemu_notify_event();
Stefan Weil2f9cba02011-04-05 18:34:21 +0200699}
700
701static int mm_start_timer(struct qemu_alarm_timer *t)
702{
Stefan Weil40f08e82012-04-27 05:34:40 +0000703 timeGetDevCaps(&mm_tc, sizeof(mm_tc));
Stefan Weil2f9cba02011-04-05 18:34:21 +0200704 return 0;
705}
706
707static void mm_stop_timer(struct qemu_alarm_timer *t)
708{
Paolo Bonzini0727b862013-02-20 14:43:31 +0100709 if (mm_timer) {
710 timeKillEvent(mm_timer);
711 }
Stefan Weil2f9cba02011-04-05 18:34:21 +0200712}
713
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100714static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta)
Stefan Weil2f9cba02011-04-05 18:34:21 +0200715{
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100716 int64_t nearest_delta_ms = delta / 1000000;
Stefan Weil40f08e82012-04-27 05:34:40 +0000717 if (nearest_delta_ms < mm_tc.wPeriodMin) {
718 nearest_delta_ms = mm_tc.wPeriodMin;
719 } else if (nearest_delta_ms > mm_tc.wPeriodMax) {
720 nearest_delta_ms = mm_tc.wPeriodMax;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100721 }
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100722
Paolo Bonzini0727b862013-02-20 14:43:31 +0100723 if (mm_timer) {
724 timeKillEvent(mm_timer);
725 }
Stefan Weil40f08e82012-04-27 05:34:40 +0000726 mm_timer = timeSetEvent((UINT)nearest_delta_ms,
727 mm_tc.wPeriodMin,
Stefan Weil2f9cba02011-04-05 18:34:21 +0200728 mm_alarm_handler,
729 (DWORD_PTR)t,
730 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
731
732 if (!mm_timer) {
Stefan Weil52ef6512012-05-08 19:14:43 +0200733 fprintf(stderr, "Failed to re-arm win32 alarm timer\n");
Stefan Weil40f08e82012-04-27 05:34:40 +0000734 timeEndPeriod(mm_tc.wPeriodMin);
Stefan Weil2f9cba02011-04-05 18:34:21 +0200735 exit(1);
736 }
737}
738
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100739static int win32_start_timer(struct qemu_alarm_timer *t)
740{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100741 HANDLE hTimer;
742 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100743
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100744 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
745 is zero) that has already expired, the timer is not updated. Since
746 creating a new timer is relatively expensive, set a bogus one-hour
747 interval in the dynticks case. */
748 success = CreateTimerQueueTimer(&hTimer,
749 NULL,
750 host_alarm_handler,
751 t,
752 1,
Stefan Weil82051992012-04-20 11:27:24 +0200753 3600000,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100754 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100755
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100756 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100757 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
758 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100759 return -1;
760 }
761
Stefan Weilcd0544e2011-04-10 20:15:09 +0200762 t->timer = hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100763 return 0;
764}
765
766static void win32_stop_timer(struct qemu_alarm_timer *t)
767{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200768 HANDLE hTimer = t->timer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100769
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100770 if (hTimer) {
771 DeleteTimerQueueTimer(NULL, hTimer, NULL);
772 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100773}
774
Paolo Bonzinif3fc6e22011-03-14 09:45:38 +0100775static void win32_rearm_timer(struct qemu_alarm_timer *t,
776 int64_t nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100777{
Stefan Weilcd0544e2011-04-10 20:15:09 +0200778 HANDLE hTimer = t->timer;
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100779 int64_t nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100780 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100781
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100782 nearest_delta_ms = nearest_delta_ns / 1000000;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +0100783 if (nearest_delta_ms < 1) {
784 nearest_delta_ms = 1;
785 }
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100786 /* ULONG_MAX can be 32 bit */
787 if (nearest_delta_ms > ULONG_MAX) {
788 nearest_delta_ms = ULONG_MAX;
789 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100790 success = ChangeTimerQueueTimer(NULL,
791 hTimer,
Stefano Stabellini5bfb7232012-04-13 19:35:02 +0100792 (unsigned long) nearest_delta_ms,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100793 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100794
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100795 if (!success) {
796 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100797 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100798 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100799 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100800
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100801}
802
803#endif /* _WIN32 */
804
Paolo Bonzini4260a732011-09-19 10:18:51 +0200805static void quit_timers(void)
806{
807 struct qemu_alarm_timer *t = alarm_timer;
808 alarm_timer = NULL;
809 t->stop(t);
810}
811
Stefan Weil253ecf82012-11-04 21:42:08 +0100812#ifdef CONFIG_POSIX
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100813static void reinit_timers(void)
814{
815 struct qemu_alarm_timer *t = alarm_timer;
816 t->stop(t);
817 if (t->start(t)) {
818 fprintf(stderr, "Internal timer error: aborting\n");
819 exit(1);
820 }
821 qemu_rearm_alarm_timer(t);
822}
Stefan Weil253ecf82012-11-04 21:42:08 +0100823#endif /* CONFIG_POSIX */
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100824
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100825int init_timer_alarm(void)
826{
827 struct qemu_alarm_timer *t = NULL;
828 int i, err = -1;
829
Paolo Bonzini744ca8e2012-10-29 15:26:28 +0100830 if (alarm_timer) {
831 return 0;
832 }
833
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100834 for (i = 0; alarm_timers[i].name; i++) {
835 t = &alarm_timers[i];
836
837 err = t->start(t);
838 if (!err)
839 break;
840 }
841
842 if (err) {
843 err = -ENOENT;
844 goto fail;
845 }
846
Paolo Bonzini4260a732011-09-19 10:18:51 +0200847 atexit(quit_timers);
Paolo Bonzinic8122c32012-11-02 15:43:22 +0100848#ifdef CONFIG_POSIX
849 pthread_atfork(NULL, NULL, reinit_timers);
850#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100851 alarm_timer = t;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100852 return 0;
853
854fail:
855 return err;
856}
857