Merge commit '01662f3e513399107c7278f2ee8f6ee4cf03fa70' into upstream-merge
[qemu/qemu-dev-zwu.git] / qemu-timer.c
blob4141b6edbebf54d06f152370f23f2a3953c8241a
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
30 #include "hw/hw.h"
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <sys/time.h>
37 #include <signal.h>
38 #ifdef __FreeBSD__
39 #include <sys/param.h>
40 #endif
42 #ifdef __linux__
43 #include <sys/ioctl.h>
44 #include <linux/rtc.h>
45 /* For the benefit of older linux systems which don't supply it,
46 we use a local copy of hpet.h. */
47 /* #include <linux/hpet.h> */
48 #include "hpet.h"
49 #endif
51 #ifdef _WIN32
52 #include <windows.h>
53 #include <mmsystem.h>
54 #endif
56 #include "qemu-timer.h"
58 /* Conversion factor from emulated instructions to virtual clock ticks. */
59 int icount_time_shift;
60 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
61 #define MAX_ICOUNT_SHIFT 10
62 /* Compensate for varying guest execution speed. */
63 int64_t qemu_icount_bias;
64 static QEMUTimer *icount_rt_timer;
65 static QEMUTimer *icount_vm_timer;
67 /***********************************************************/
68 /* guest cycle counter */
70 typedef struct TimersState {
71 int64_t cpu_ticks_prev;
72 int64_t cpu_ticks_offset;
73 int64_t cpu_clock_offset;
74 int32_t cpu_ticks_enabled;
75 int64_t dummy;
76 } TimersState;
78 TimersState timers_state;
80 /* return the host CPU cycle counter and handle stop/restart */
81 int64_t cpu_get_ticks(void)
83 if (use_icount) {
84 return cpu_get_icount();
86 if (!timers_state.cpu_ticks_enabled) {
87 return timers_state.cpu_ticks_offset;
88 } else {
89 int64_t ticks;
90 ticks = cpu_get_real_ticks();
91 if (timers_state.cpu_ticks_prev > ticks) {
92 /* Note: non increasing ticks may happen if the host uses
93 software suspend */
94 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
96 timers_state.cpu_ticks_prev = ticks;
97 return ticks + timers_state.cpu_ticks_offset;
101 /* return the host CPU monotonic timer and handle stop/restart */
102 static int64_t cpu_get_clock(void)
104 int64_t ti;
105 if (!timers_state.cpu_ticks_enabled) {
106 return timers_state.cpu_clock_offset;
107 } else {
108 ti = get_clock();
109 return ti + timers_state.cpu_clock_offset;
113 #ifndef CONFIG_IOTHREAD
114 static int64_t qemu_icount_delta(void)
116 if (!use_icount) {
117 return 5000 * (int64_t) 1000000;
118 } else if (use_icount == 1) {
119 /* When not using an adaptive execution frequency
120 we tend to get badly out of sync with real time,
121 so just delay for a reasonable amount of time. */
122 return 0;
123 } else {
124 return cpu_get_icount() - cpu_get_clock();
127 #endif
129 /* enable cpu_get_ticks() */
130 void cpu_enable_ticks(void)
132 if (!timers_state.cpu_ticks_enabled) {
133 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
134 timers_state.cpu_clock_offset -= get_clock();
135 timers_state.cpu_ticks_enabled = 1;
139 /* disable cpu_get_ticks() : the clock is stopped. You must not call
140 cpu_get_ticks() after that. */
141 void cpu_disable_ticks(void)
143 if (timers_state.cpu_ticks_enabled) {
144 timers_state.cpu_ticks_offset = cpu_get_ticks();
145 timers_state.cpu_clock_offset = cpu_get_clock();
146 timers_state.cpu_ticks_enabled = 0;
150 /***********************************************************/
151 /* timers */
153 #define QEMU_CLOCK_REALTIME 0
154 #define QEMU_CLOCK_VIRTUAL 1
155 #define QEMU_CLOCK_HOST 2
157 struct QEMUClock {
158 int type;
159 int enabled;
161 QEMUTimer *warp_timer;
164 struct QEMUTimer {
165 QEMUClock *clock;
166 int64_t expire_time; /* in nanoseconds */
167 int scale;
168 QEMUTimerCB *cb;
169 void *opaque;
170 struct QEMUTimer *next;
173 struct qemu_alarm_timer {
174 char const *name;
175 int (*start)(struct qemu_alarm_timer *t);
176 void (*stop)(struct qemu_alarm_timer *t);
177 void (*rearm)(struct qemu_alarm_timer *t);
178 #if defined(__linux__)
179 int fd;
180 timer_t timer;
181 #elif defined(_WIN32)
182 HANDLE timer;
183 #endif
184 char expired;
185 char pending;
188 static struct qemu_alarm_timer *alarm_timer;
190 static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time)
192 return timer_head && (timer_head->expire_time <= current_time);
195 int qemu_alarm_pending(void)
197 return alarm_timer->pending;
200 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
202 return !!t->rearm;
205 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
207 if (!alarm_has_dynticks(t))
208 return;
210 t->rearm(t);
213 /* TODO: MIN_TIMER_REARM_NS should be optimized */
214 #define MIN_TIMER_REARM_NS 250000
216 #ifdef _WIN32
218 static int mm_start_timer(struct qemu_alarm_timer *t);
219 static void mm_stop_timer(struct qemu_alarm_timer *t);
220 static void mm_rearm_timer(struct qemu_alarm_timer *t);
222 static int win32_start_timer(struct qemu_alarm_timer *t);
223 static void win32_stop_timer(struct qemu_alarm_timer *t);
224 static void win32_rearm_timer(struct qemu_alarm_timer *t);
226 #else
228 static int unix_start_timer(struct qemu_alarm_timer *t);
229 static void unix_stop_timer(struct qemu_alarm_timer *t);
231 #ifdef __linux__
233 static int dynticks_start_timer(struct qemu_alarm_timer *t);
234 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
235 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
237 static int hpet_start_timer(struct qemu_alarm_timer *t);
238 static void hpet_stop_timer(struct qemu_alarm_timer *t);
240 static int rtc_start_timer(struct qemu_alarm_timer *t);
241 static void rtc_stop_timer(struct qemu_alarm_timer *t);
243 #endif /* __linux__ */
245 #endif /* _WIN32 */
247 /* Correlation between real and virtual time is always going to be
248 fairly approximate, so ignore small variation.
249 When the guest is idle real and virtual time will be aligned in
250 the IO wait loop. */
251 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
253 static void icount_adjust(void)
255 int64_t cur_time;
256 int64_t cur_icount;
257 int64_t delta;
258 static int64_t last_delta;
259 /* If the VM is not running, then do nothing. */
260 if (!vm_running)
261 return;
263 cur_time = cpu_get_clock();
264 cur_icount = qemu_get_clock_ns(vm_clock);
265 delta = cur_icount - cur_time;
266 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
267 if (delta > 0
268 && last_delta + ICOUNT_WOBBLE < delta * 2
269 && icount_time_shift > 0) {
270 /* The guest is getting too far ahead. Slow time down. */
271 icount_time_shift--;
273 if (delta < 0
274 && last_delta - ICOUNT_WOBBLE > delta * 2
275 && icount_time_shift < MAX_ICOUNT_SHIFT) {
276 /* The guest is getting too far behind. Speed time up. */
277 icount_time_shift++;
279 last_delta = delta;
280 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
283 static void icount_adjust_rt(void * opaque)
285 qemu_mod_timer(icount_rt_timer,
286 qemu_get_clock_ms(rt_clock) + 1000);
287 icount_adjust();
290 static void icount_adjust_vm(void * opaque)
292 qemu_mod_timer(icount_vm_timer,
293 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
294 icount_adjust();
297 int64_t qemu_icount_round(int64_t count)
299 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
302 static struct qemu_alarm_timer alarm_timers[] = {
303 #ifndef _WIN32
304 #ifdef __linux__
305 {"dynticks", dynticks_start_timer,
306 dynticks_stop_timer, dynticks_rearm_timer},
307 /* HPET - if available - is preferred */
308 {"hpet", hpet_start_timer, hpet_stop_timer, NULL},
309 /* ...otherwise try RTC */
310 {"rtc", rtc_start_timer, rtc_stop_timer, NULL},
311 #endif
312 {"unix", unix_start_timer, unix_stop_timer, NULL},
313 #else
314 {"mmtimer", mm_start_timer, mm_stop_timer, NULL},
315 {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer},
316 {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
317 {"win32", win32_start_timer, win32_stop_timer, NULL},
318 #endif
319 {NULL, }
322 static void show_available_alarms(void)
324 int i;
326 printf("Available alarm timers, in order of precedence:\n");
327 for (i = 0; alarm_timers[i].name; i++)
328 printf("%s\n", alarm_timers[i].name);
331 void configure_alarms(char const *opt)
333 int i;
334 int cur = 0;
335 int count = ARRAY_SIZE(alarm_timers) - 1;
336 char *arg;
337 char *name;
338 struct qemu_alarm_timer tmp;
340 if (!strcmp(opt, "?")) {
341 show_available_alarms();
342 exit(0);
345 arg = qemu_strdup(opt);
347 /* Reorder the array */
348 name = strtok(arg, ",");
349 while (name) {
350 for (i = 0; i < count && alarm_timers[i].name; i++) {
351 if (!strcmp(alarm_timers[i].name, name))
352 break;
355 if (i == count) {
356 fprintf(stderr, "Unknown clock %s\n", name);
357 goto next;
360 if (i < cur)
361 /* Ignore */
362 goto next;
364 /* Swap */
365 tmp = alarm_timers[i];
366 alarm_timers[i] = alarm_timers[cur];
367 alarm_timers[cur] = tmp;
369 cur++;
370 next:
371 name = strtok(NULL, ",");
374 qemu_free(arg);
376 if (cur) {
377 /* Disable remaining timers */
378 for (i = cur; i < count; i++)
379 alarm_timers[i].name = NULL;
380 } else {
381 show_available_alarms();
382 exit(1);
386 #define QEMU_NUM_CLOCKS 3
388 QEMUClock *rt_clock;
389 QEMUClock *vm_clock;
390 QEMUClock *host_clock;
392 static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
394 static QEMUClock *qemu_new_clock(int type)
396 QEMUClock *clock;
397 clock = qemu_mallocz(sizeof(QEMUClock));
398 clock->type = type;
399 clock->enabled = 1;
400 return clock;
403 void qemu_clock_enable(QEMUClock *clock, int enabled)
405 clock->enabled = enabled;
408 static int64_t vm_clock_warp_start;
410 static void icount_warp_rt(void *opaque)
412 if (vm_clock_warp_start == -1) {
413 return;
416 if (vm_running) {
417 int64_t clock = qemu_get_clock_ns(rt_clock);
418 int64_t warp_delta = clock - vm_clock_warp_start;
419 if (use_icount == 1) {
420 qemu_icount_bias += warp_delta;
421 } else {
423 * In adaptive mode, do not let the vm_clock run too
424 * far ahead of real time.
426 int64_t cur_time = cpu_get_clock();
427 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
428 int64_t delta = cur_time - cur_icount;
429 qemu_icount_bias += MIN(warp_delta, delta);
431 if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
432 qemu_get_clock_ns(vm_clock))) {
433 qemu_notify_event();
436 vm_clock_warp_start = -1;
439 void qemu_clock_warp(QEMUClock *clock)
441 int64_t deadline;
443 if (!clock->warp_timer) {
444 return;
448 * There are too many global variables to make the "warp" behavior
449 * applicable to other clocks. But a clock argument removes the
450 * need for if statements all over the place.
452 assert(clock == vm_clock);
455 * If the CPUs have been sleeping, advance the vm_clock timer now. This
456 * ensures that the deadline for the timer is computed correctly below.
457 * This also makes sure that the insn counter is synchronized before the
458 * CPU starts running, in case the CPU is woken by an event other than
459 * the earliest vm_clock timer.
461 icount_warp_rt(NULL);
462 if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
463 qemu_del_timer(clock->warp_timer);
464 return;
467 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
468 deadline = qemu_next_icount_deadline();
469 if (deadline > 0) {
471 * Ensure the vm_clock proceeds even when the virtual CPU goes to
472 * sleep. Otherwise, the CPU might be waiting for a future timer
473 * interrupt to wake it up, but the interrupt never comes because
474 * the vCPU isn't running any insns and thus doesn't advance the
475 * vm_clock.
477 * An extreme solution for this problem would be to never let VCPUs
478 * sleep in icount mode if there is a pending vm_clock timer; rather
479 * time could just advance to the next vm_clock event. Instead, we
480 * do stop VCPUs and only advance vm_clock after some "real" time,
481 * (related to the time left until the next event) has passed. This
482 * rt_clock timer will do this. This avoids that the warps are too
483 * visible externally---for example, you will not be sending network
484 * packets continously instead of every 100ms.
486 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
487 } else {
488 qemu_notify_event();
492 QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
493 QEMUTimerCB *cb, void *opaque)
495 QEMUTimer *ts;
497 ts = qemu_mallocz(sizeof(QEMUTimer));
498 ts->clock = clock;
499 ts->cb = cb;
500 ts->opaque = opaque;
501 ts->scale = scale;
502 return ts;
505 void qemu_free_timer(QEMUTimer *ts)
507 qemu_free(ts);
510 /* stop a timer, but do not dealloc it */
511 void qemu_del_timer(QEMUTimer *ts)
513 QEMUTimer **pt, *t;
515 /* NOTE: this code must be signal safe because
516 qemu_timer_expired() can be called from a signal. */
517 pt = &active_timers[ts->clock->type];
518 for(;;) {
519 t = *pt;
520 if (!t)
521 break;
522 if (t == ts) {
523 *pt = t->next;
524 break;
526 pt = &t->next;
530 /* modify the current timer so that it will be fired when current_time
531 >= expire_time. The corresponding callback will be called. */
532 static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
534 QEMUTimer **pt, *t;
536 qemu_del_timer(ts);
538 /* add the timer in the sorted list */
539 /* NOTE: this code must be signal safe because
540 qemu_timer_expired() can be called from a signal. */
541 pt = &active_timers[ts->clock->type];
542 for(;;) {
543 t = *pt;
544 if (!qemu_timer_expired_ns(t, expire_time)) {
545 break;
547 pt = &t->next;
549 ts->expire_time = expire_time;
550 ts->next = *pt;
551 *pt = ts;
553 /* Rearm if necessary */
554 if (pt == &active_timers[ts->clock->type]) {
555 if (!alarm_timer->pending) {
556 qemu_rearm_alarm_timer(alarm_timer);
558 /* Interrupt execution to force deadline recalculation. */
559 qemu_clock_warp(ts->clock);
560 if (use_icount) {
561 qemu_notify_event();
566 /* modify the current timer so that it will be fired when current_time
567 >= expire_time. The corresponding callback will be called. */
568 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
570 qemu_mod_timer_ns(ts, expire_time * ts->scale);
573 int qemu_timer_pending(QEMUTimer *ts)
575 QEMUTimer *t;
576 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
577 if (t == ts)
578 return 1;
580 return 0;
583 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
585 return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
588 static void qemu_run_timers(QEMUClock *clock)
590 QEMUTimer **ptimer_head, *ts;
591 int64_t current_time;
593 if (!clock->enabled)
594 return;
596 current_time = qemu_get_clock_ns(clock);
597 ptimer_head = &active_timers[clock->type];
598 for(;;) {
599 ts = *ptimer_head;
600 if (!qemu_timer_expired_ns(ts, current_time)) {
601 break;
603 /* remove timer from the list before calling the callback */
604 *ptimer_head = ts->next;
605 ts->next = NULL;
607 /* run the callback (the timer list can be modified) */
608 ts->cb(ts->opaque);
612 int64_t qemu_get_clock_ns(QEMUClock *clock)
614 switch(clock->type) {
615 case QEMU_CLOCK_REALTIME:
616 return get_clock();
617 default:
618 case QEMU_CLOCK_VIRTUAL:
619 if (use_icount) {
620 return cpu_get_icount();
621 } else {
622 return cpu_get_clock();
624 case QEMU_CLOCK_HOST:
625 return get_clock_realtime();
629 void init_clocks(void)
631 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
632 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
633 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
635 rtc_clock = host_clock;
638 /* save a timer */
639 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
641 uint64_t expire_time;
643 if (qemu_timer_pending(ts)) {
644 expire_time = ts->expire_time;
645 } else {
646 expire_time = -1;
648 qemu_put_be64(f, expire_time);
651 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
653 uint64_t expire_time;
655 expire_time = qemu_get_be64(f);
656 if (expire_time != -1) {
657 qemu_mod_timer_ns(ts, expire_time);
658 } else {
659 qemu_del_timer(ts);
663 static const VMStateDescription vmstate_timers = {
664 .name = "timer",
665 .version_id = 2,
666 .minimum_version_id = 1,
667 .minimum_version_id_old = 1,
668 .fields = (VMStateField []) {
669 VMSTATE_INT64(cpu_ticks_offset, TimersState),
670 VMSTATE_INT64(dummy, TimersState),
671 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
672 VMSTATE_END_OF_LIST()
676 void configure_icount(const char *option)
678 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
679 if (!option)
680 return;
682 #ifdef CONFIG_IOTHREAD
683 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
684 #endif
686 if (strcmp(option, "auto") != 0) {
687 icount_time_shift = strtol(option, NULL, 0);
688 use_icount = 1;
689 return;
692 use_icount = 2;
694 /* 125MIPS seems a reasonable initial guess at the guest speed.
695 It will be corrected fairly quickly anyway. */
696 icount_time_shift = 3;
698 /* Have both realtime and virtual time triggers for speed adjustment.
699 The realtime trigger catches emulated time passing too slowly,
700 the virtual time trigger catches emulated time passing too fast.
701 Realtime triggers occur even when idle, so use them less frequently
702 than VM triggers. */
703 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
704 qemu_mod_timer(icount_rt_timer,
705 qemu_get_clock_ms(rt_clock) + 1000);
706 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
707 qemu_mod_timer(icount_vm_timer,
708 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
711 void qemu_run_all_timers(void)
713 alarm_timer->pending = 0;
715 /* rearm timer, if not periodic */
716 if (alarm_timer->expired) {
717 alarm_timer->expired = 0;
718 qemu_rearm_alarm_timer(alarm_timer);
721 /* vm time timers */
722 if (vm_running) {
723 qemu_run_timers(vm_clock);
726 qemu_run_timers(rt_clock);
727 qemu_run_timers(host_clock);
730 static int64_t qemu_next_alarm_deadline(void);
732 #ifdef _WIN32
733 static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
734 #else
735 static void host_alarm_handler(int host_signum)
736 #endif
738 struct qemu_alarm_timer *t = alarm_timer;
739 if (!t)
740 return;
742 #if 0
743 #define DISP_FREQ 1000
745 static int64_t delta_min = INT64_MAX;
746 static int64_t delta_max, delta_cum, last_clock, delta, ti;
747 static int count;
748 ti = qemu_get_clock_ns(vm_clock);
749 if (last_clock != 0) {
750 delta = ti - last_clock;
751 if (delta < delta_min)
752 delta_min = delta;
753 if (delta > delta_max)
754 delta_max = delta;
755 delta_cum += delta;
756 if (++count == DISP_FREQ) {
757 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
758 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
759 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
760 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
761 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
762 count = 0;
763 delta_min = INT64_MAX;
764 delta_max = 0;
765 delta_cum = 0;
768 last_clock = ti;
770 #endif
771 if (alarm_has_dynticks(t) ||
772 qemu_next_alarm_deadline () <= 0) {
773 t->expired = alarm_has_dynticks(t);
774 t->pending = 1;
775 qemu_notify_event();
779 int64_t qemu_next_icount_deadline(void)
781 /* To avoid problems with overflow limit this to 2^32. */
782 int64_t delta = INT32_MAX;
784 assert(use_icount);
785 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
786 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
787 qemu_get_clock_ns(vm_clock);
790 if (delta < 0)
791 delta = 0;
793 return delta;
796 static int64_t qemu_next_alarm_deadline(void)
798 int64_t delta;
799 int64_t rtdelta;
801 if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
802 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
803 qemu_get_clock_ns(vm_clock);
804 } else {
805 delta = INT32_MAX;
807 if (active_timers[QEMU_CLOCK_HOST]) {
808 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
809 qemu_get_clock_ns(host_clock);
810 if (hdelta < delta)
811 delta = hdelta;
813 if (active_timers[QEMU_CLOCK_REALTIME]) {
814 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
815 qemu_get_clock_ns(rt_clock));
816 if (rtdelta < delta)
817 delta = rtdelta;
820 return delta;
823 #if defined(__linux__)
825 #define RTC_FREQ 1024
827 static void enable_sigio_timer(int fd)
829 struct sigaction act;
831 /* timer signal */
832 sigfillset(&act.sa_mask);
833 act.sa_flags = 0;
834 act.sa_handler = host_alarm_handler;
836 sigaction(SIGIO, &act, NULL);
837 fcntl_setfl(fd, O_ASYNC);
838 fcntl(fd, F_SETOWN, getpid());
841 static int hpet_start_timer(struct qemu_alarm_timer *t)
843 struct hpet_info info;
844 int r, fd;
846 fd = qemu_open("/dev/hpet", O_RDONLY);
847 if (fd < 0)
848 return -1;
850 /* Set frequency */
851 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
852 if (r < 0) {
853 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
854 "error, but for better emulation accuracy type:\n"
855 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
856 goto fail;
859 /* Check capabilities */
860 r = ioctl(fd, HPET_INFO, &info);
861 if (r < 0)
862 goto fail;
864 /* Enable periodic mode */
865 r = ioctl(fd, HPET_EPI, 0);
866 if (info.hi_flags && (r < 0))
867 goto fail;
869 /* Enable interrupt */
870 r = ioctl(fd, HPET_IE_ON, 0);
871 if (r < 0)
872 goto fail;
874 enable_sigio_timer(fd);
875 t->fd = fd;
877 return 0;
878 fail:
879 close(fd);
880 return -1;
883 static void hpet_stop_timer(struct qemu_alarm_timer *t)
885 int fd = t->fd;
887 close(fd);
890 static int rtc_start_timer(struct qemu_alarm_timer *t)
892 int rtc_fd;
893 unsigned long current_rtc_freq = 0;
895 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
896 if (rtc_fd < 0)
897 return -1;
898 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
899 if (current_rtc_freq != RTC_FREQ &&
900 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
901 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
902 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
903 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
904 goto fail;
906 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
907 fail:
908 close(rtc_fd);
909 return -1;
912 enable_sigio_timer(rtc_fd);
914 t->fd = rtc_fd;
916 return 0;
919 static void rtc_stop_timer(struct qemu_alarm_timer *t)
921 int rtc_fd = t->fd;
923 close(rtc_fd);
926 static int dynticks_start_timer(struct qemu_alarm_timer *t)
928 struct sigevent ev;
929 timer_t host_timer;
930 struct sigaction act;
932 sigfillset(&act.sa_mask);
933 act.sa_flags = 0;
934 act.sa_handler = host_alarm_handler;
936 sigaction(SIGALRM, &act, NULL);
939 * Initialize ev struct to 0 to avoid valgrind complaining
940 * about uninitialized data in timer_create call
942 memset(&ev, 0, sizeof(ev));
943 ev.sigev_value.sival_int = 0;
944 ev.sigev_notify = SIGEV_SIGNAL;
945 ev.sigev_signo = SIGALRM;
947 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
948 perror("timer_create");
950 /* disable dynticks */
951 fprintf(stderr, "Dynamic Ticks disabled\n");
953 return -1;
956 t->timer = host_timer;
958 return 0;
961 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
963 timer_t host_timer = t->timer;
965 timer_delete(host_timer);
968 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
970 timer_t host_timer = t->timer;
971 struct itimerspec timeout;
972 int64_t nearest_delta_ns = INT64_MAX;
973 int64_t current_ns;
975 assert(alarm_has_dynticks(t));
976 if (!active_timers[QEMU_CLOCK_REALTIME] &&
977 !active_timers[QEMU_CLOCK_VIRTUAL] &&
978 !active_timers[QEMU_CLOCK_HOST])
979 return;
981 nearest_delta_ns = qemu_next_alarm_deadline();
982 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
983 nearest_delta_ns = MIN_TIMER_REARM_NS;
985 /* check whether a timer is already running */
986 if (timer_gettime(host_timer, &timeout)) {
987 perror("gettime");
988 fprintf(stderr, "Internal timer error: aborting\n");
989 exit(1);
991 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
992 if (current_ns && current_ns <= nearest_delta_ns)
993 return;
995 timeout.it_interval.tv_sec = 0;
996 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
997 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
998 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
999 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1000 perror("settime");
1001 fprintf(stderr, "Internal timer error: aborting\n");
1002 exit(1);
1006 #endif /* defined(__linux__) */
1008 #if !defined(_WIN32)
1010 static int unix_start_timer(struct qemu_alarm_timer *t)
1012 struct sigaction act;
1013 struct itimerval itv;
1014 int err;
1016 /* timer signal */
1017 sigfillset(&act.sa_mask);
1018 act.sa_flags = 0;
1019 act.sa_handler = host_alarm_handler;
1021 sigaction(SIGALRM, &act, NULL);
1023 itv.it_interval.tv_sec = 0;
1024 /* for i386 kernel 2.6 to get 1 ms */
1025 itv.it_interval.tv_usec = 999;
1026 itv.it_value.tv_sec = 0;
1027 itv.it_value.tv_usec = 10 * 1000;
1029 err = setitimer(ITIMER_REAL, &itv, NULL);
1030 if (err)
1031 return -1;
1033 return 0;
1036 static void unix_stop_timer(struct qemu_alarm_timer *t)
1038 struct itimerval itv;
1040 memset(&itv, 0, sizeof(itv));
1041 setitimer(ITIMER_REAL, &itv, NULL);
1044 #endif /* !defined(_WIN32) */
1047 #ifdef _WIN32
1049 static MMRESULT mm_timer;
1050 static unsigned mm_period;
1052 static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg,
1053 DWORD_PTR dwUser, DWORD_PTR dw1,
1054 DWORD_PTR dw2)
1056 struct qemu_alarm_timer *t = alarm_timer;
1057 if (!t) {
1058 return;
1060 if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) {
1061 t->expired = alarm_has_dynticks(t);
1062 t->pending = 1;
1063 qemu_notify_event();
1067 static int mm_start_timer(struct qemu_alarm_timer *t)
1069 TIMECAPS tc;
1070 UINT flags;
1072 memset(&tc, 0, sizeof(tc));
1073 timeGetDevCaps(&tc, sizeof(tc));
1075 mm_period = tc.wPeriodMin;
1076 timeBeginPeriod(mm_period);
1078 flags = TIME_CALLBACK_FUNCTION;
1079 if (alarm_has_dynticks(t)) {
1080 flags |= TIME_ONESHOT;
1081 } else {
1082 flags |= TIME_PERIODIC;
1085 mm_timer = timeSetEvent(1, /* interval (ms) */
1086 mm_period, /* resolution */
1087 mm_alarm_handler, /* function */
1088 (DWORD_PTR)t, /* parameter */
1089 flags);
1091 if (!mm_timer) {
1092 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1093 GetLastError());
1094 timeEndPeriod(mm_period);
1095 return -1;
1098 return 0;
1101 static void mm_stop_timer(struct qemu_alarm_timer *t)
1103 timeKillEvent(mm_timer);
1104 timeEndPeriod(mm_period);
1107 static void mm_rearm_timer(struct qemu_alarm_timer *t)
1109 int nearest_delta_ms;
1111 assert(alarm_has_dynticks(t));
1112 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1113 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1114 !active_timers[QEMU_CLOCK_HOST]) {
1115 return;
1118 timeKillEvent(mm_timer);
1120 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1121 if (nearest_delta_ms < 1) {
1122 nearest_delta_ms = 1;
1124 mm_timer = timeSetEvent(nearest_delta_ms,
1125 mm_period,
1126 mm_alarm_handler,
1127 (DWORD_PTR)t,
1128 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1130 if (!mm_timer) {
1131 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1132 GetLastError());
1134 timeEndPeriod(mm_period);
1135 exit(1);
1139 static int win32_start_timer(struct qemu_alarm_timer *t)
1141 HANDLE hTimer;
1142 BOOLEAN success;
1144 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1145 is zero) that has already expired, the timer is not updated. Since
1146 creating a new timer is relatively expensive, set a bogus one-hour
1147 interval in the dynticks case. */
1148 success = CreateTimerQueueTimer(&hTimer,
1149 NULL,
1150 host_alarm_handler,
1153 alarm_has_dynticks(t) ? 3600000 : 1,
1154 WT_EXECUTEINTIMERTHREAD);
1156 if (!success) {
1157 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1158 GetLastError());
1159 return -1;
1162 t->timer = hTimer;
1163 return 0;
1166 static void win32_stop_timer(struct qemu_alarm_timer *t)
1168 HANDLE hTimer = t->timer;
1170 if (hTimer) {
1171 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1175 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1177 HANDLE hTimer = t->timer;
1178 int nearest_delta_ms;
1179 BOOLEAN success;
1181 assert(alarm_has_dynticks(t));
1182 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1183 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1184 !active_timers[QEMU_CLOCK_HOST])
1185 return;
1187 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1188 if (nearest_delta_ms < 1) {
1189 nearest_delta_ms = 1;
1191 success = ChangeTimerQueueTimer(NULL,
1192 hTimer,
1193 nearest_delta_ms,
1194 3600000);
1196 if (!success) {
1197 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
1198 GetLastError());
1199 exit(-1);
1204 #endif /* _WIN32 */
1206 static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1208 if (running)
1209 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1212 int init_timer_alarm(void)
1214 struct qemu_alarm_timer *t = NULL;
1215 int i, err = -1;
1217 for (i = 0; alarm_timers[i].name; i++) {
1218 t = &alarm_timers[i];
1220 err = t->start(t);
1221 if (!err)
1222 break;
1225 if (err) {
1226 err = -ENOENT;
1227 goto fail;
1230 /* first event is at time 0 */
1231 t->pending = 1;
1232 alarm_timer = t;
1233 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1235 return 0;
1237 fail:
1238 return err;
1241 void quit_timers(void)
1243 struct qemu_alarm_timer *t = alarm_timer;
1244 alarm_timer = NULL;
1245 t->stop(t);
1248 int qemu_calculate_timeout(void)
1250 #ifndef CONFIG_IOTHREAD
1251 int timeout;
1253 if (!vm_running)
1254 timeout = 5000;
1255 else {
1256 /* XXX: use timeout computed from timers */
1257 int64_t add;
1258 int64_t delta;
1259 /* Advance virtual time to the next event. */
1260 delta = qemu_icount_delta();
1261 if (delta > 0) {
1262 /* If virtual time is ahead of real time then just
1263 wait for IO. */
1264 timeout = (delta + 999999) / 1000000;
1265 } else {
1266 /* Wait for either IO to occur or the next
1267 timer event. */
1268 add = qemu_next_icount_deadline();
1269 /* We advance the timer before checking for IO.
1270 Limit the amount we advance so that early IO
1271 activity won't get the guest too far ahead. */
1272 if (add > 10000000)
1273 add = 10000000;
1274 delta += add;
1275 qemu_icount += qemu_icount_round (add);
1276 timeout = delta / 1000000;
1277 if (timeout < 0)
1278 timeout = 0;
1282 return timeout;
1283 #else /* CONFIG_IOTHREAD */
1284 return 1000;
1285 #endif