Add support for generic notifier lists
[qemu/aliguori-queue.git] / qemu-timer.c
blob329d3a4e60f5912009d387427454a80ea7480a28
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>
39 #ifdef __linux__
40 #include <sys/ioctl.h>
41 #include <linux/rtc.h>
42 /* For the benefit of older linux systems which don't supply it,
43 we use a local copy of hpet.h. */
44 /* #include <linux/hpet.h> */
45 #include "hpet.h"
46 #endif
48 #ifdef _WIN32
49 #include <windows.h>
50 #include <mmsystem.h>
51 #endif
53 #include "cpu-defs.h"
54 #include "qemu-timer.h"
55 #include "exec-all.h"
57 /* Conversion factor from emulated instructions to virtual clock ticks. */
58 static int icount_time_shift;
59 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
60 #define MAX_ICOUNT_SHIFT 10
61 /* Compensate for varying guest execution speed. */
62 static int64_t qemu_icount_bias;
63 static QEMUTimer *icount_rt_timer;
64 static QEMUTimer *icount_vm_timer;
67 /***********************************************************/
68 /* real time host monotonic timer */
71 static int64_t get_clock_realtime(void)
73 struct timeval tv;
75 gettimeofday(&tv, NULL);
76 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
79 #ifdef WIN32
81 static int64_t clock_freq;
83 static void init_get_clock(void)
85 LARGE_INTEGER freq;
86 int ret;
87 ret = QueryPerformanceFrequency(&freq);
88 if (ret == 0) {
89 fprintf(stderr, "Could not calibrate ticks\n");
90 exit(1);
92 clock_freq = freq.QuadPart;
95 static int64_t get_clock(void)
97 LARGE_INTEGER ti;
98 QueryPerformanceCounter(&ti);
99 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
102 #else
104 static int use_rt_clock;
106 static void init_get_clock(void)
108 use_rt_clock = 0;
109 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
110 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
112 struct timespec ts;
113 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
114 use_rt_clock = 1;
117 #endif
120 static int64_t get_clock(void)
122 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
123 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
124 if (use_rt_clock) {
125 struct timespec ts;
126 clock_gettime(CLOCK_MONOTONIC, &ts);
127 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
128 } else
129 #endif
131 /* XXX: using gettimeofday leads to problems if the date
132 changes, so it should be avoided. */
133 return get_clock_realtime();
136 #endif
138 /* Return the virtual CPU time, based on the instruction counter. */
139 static int64_t cpu_get_icount(void)
141 int64_t icount;
142 CPUState *env = cpu_single_env;;
143 icount = qemu_icount;
144 if (env) {
145 if (!can_do_io(env))
146 fprintf(stderr, "Bad clock read\n");
147 icount -= (env->icount_decr.u16.low + env->icount_extra);
149 return qemu_icount_bias + (icount << icount_time_shift);
152 /***********************************************************/
153 /* guest cycle counter */
155 typedef struct TimersState {
156 int64_t cpu_ticks_prev;
157 int64_t cpu_ticks_offset;
158 int64_t cpu_clock_offset;
159 int32_t cpu_ticks_enabled;
160 int64_t dummy;
161 } TimersState;
163 TimersState timers_state;
165 /* return the host CPU cycle counter and handle stop/restart */
166 int64_t cpu_get_ticks(void)
168 if (use_icount) {
169 return cpu_get_icount();
171 if (!timers_state.cpu_ticks_enabled) {
172 return timers_state.cpu_ticks_offset;
173 } else {
174 int64_t ticks;
175 ticks = cpu_get_real_ticks();
176 if (timers_state.cpu_ticks_prev > ticks) {
177 /* Note: non increasing ticks may happen if the host uses
178 software suspend */
179 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
181 timers_state.cpu_ticks_prev = ticks;
182 return ticks + timers_state.cpu_ticks_offset;
186 /* return the host CPU monotonic timer and handle stop/restart */
187 static int64_t cpu_get_clock(void)
189 int64_t ti;
190 if (!timers_state.cpu_ticks_enabled) {
191 return timers_state.cpu_clock_offset;
192 } else {
193 ti = get_clock();
194 return ti + timers_state.cpu_clock_offset;
198 #ifndef CONFIG_IOTHREAD
199 static int64_t qemu_icount_delta(void)
201 if (!use_icount) {
202 return 5000 * (int64_t) 1000000;
203 } else if (use_icount == 1) {
204 /* When not using an adaptive execution frequency
205 we tend to get badly out of sync with real time,
206 so just delay for a reasonable amount of time. */
207 return 0;
208 } else {
209 return cpu_get_icount() - cpu_get_clock();
212 #endif
214 /* enable cpu_get_ticks() */
215 void cpu_enable_ticks(void)
217 if (!timers_state.cpu_ticks_enabled) {
218 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
219 timers_state.cpu_clock_offset -= get_clock();
220 timers_state.cpu_ticks_enabled = 1;
224 /* disable cpu_get_ticks() : the clock is stopped. You must not call
225 cpu_get_ticks() after that. */
226 void cpu_disable_ticks(void)
228 if (timers_state.cpu_ticks_enabled) {
229 timers_state.cpu_ticks_offset = cpu_get_ticks();
230 timers_state.cpu_clock_offset = cpu_get_clock();
231 timers_state.cpu_ticks_enabled = 0;
235 /***********************************************************/
236 /* timers */
238 #define QEMU_CLOCK_REALTIME 0
239 #define QEMU_CLOCK_VIRTUAL 1
240 #define QEMU_CLOCK_HOST 2
242 struct QEMUClock {
243 int type;
244 int enabled;
245 /* XXX: add frequency */
248 struct QEMUTimer {
249 QEMUClock *clock;
250 int64_t expire_time;
251 QEMUTimerCB *cb;
252 void *opaque;
253 struct QEMUTimer *next;
256 struct qemu_alarm_timer {
257 char const *name;
258 int (*start)(struct qemu_alarm_timer *t);
259 void (*stop)(struct qemu_alarm_timer *t);
260 void (*rearm)(struct qemu_alarm_timer *t);
261 void *priv;
263 char expired;
264 char pending;
267 static struct qemu_alarm_timer *alarm_timer;
269 int qemu_alarm_pending(void)
271 return alarm_timer->pending;
274 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
276 return !!t->rearm;
279 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
281 if (!alarm_has_dynticks(t))
282 return;
284 t->rearm(t);
287 /* TODO: MIN_TIMER_REARM_US should be optimized */
288 #define MIN_TIMER_REARM_US 250
290 #ifdef _WIN32
292 struct qemu_alarm_win32 {
293 MMRESULT timerId;
294 unsigned int period;
295 } alarm_win32_data = {0, 0};
297 static int win32_start_timer(struct qemu_alarm_timer *t);
298 static void win32_stop_timer(struct qemu_alarm_timer *t);
299 static void win32_rearm_timer(struct qemu_alarm_timer *t);
301 #else
303 static int unix_start_timer(struct qemu_alarm_timer *t);
304 static void unix_stop_timer(struct qemu_alarm_timer *t);
306 #ifdef __linux__
308 static int dynticks_start_timer(struct qemu_alarm_timer *t);
309 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
310 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
312 static int hpet_start_timer(struct qemu_alarm_timer *t);
313 static void hpet_stop_timer(struct qemu_alarm_timer *t);
315 static int rtc_start_timer(struct qemu_alarm_timer *t);
316 static void rtc_stop_timer(struct qemu_alarm_timer *t);
318 #endif /* __linux__ */
320 #endif /* _WIN32 */
322 /* Correlation between real and virtual time is always going to be
323 fairly approximate, so ignore small variation.
324 When the guest is idle real and virtual time will be aligned in
325 the IO wait loop. */
326 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
328 static void icount_adjust(void)
330 int64_t cur_time;
331 int64_t cur_icount;
332 int64_t delta;
333 static int64_t last_delta;
334 /* If the VM is not running, then do nothing. */
335 if (!vm_running)
336 return;
338 cur_time = cpu_get_clock();
339 cur_icount = qemu_get_clock(vm_clock);
340 delta = cur_icount - cur_time;
341 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
342 if (delta > 0
343 && last_delta + ICOUNT_WOBBLE < delta * 2
344 && icount_time_shift > 0) {
345 /* The guest is getting too far ahead. Slow time down. */
346 icount_time_shift--;
348 if (delta < 0
349 && last_delta - ICOUNT_WOBBLE > delta * 2
350 && icount_time_shift < MAX_ICOUNT_SHIFT) {
351 /* The guest is getting too far behind. Speed time up. */
352 icount_time_shift++;
354 last_delta = delta;
355 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
358 static void icount_adjust_rt(void * opaque)
360 qemu_mod_timer(icount_rt_timer,
361 qemu_get_clock(rt_clock) + 1000);
362 icount_adjust();
365 static void icount_adjust_vm(void * opaque)
367 qemu_mod_timer(icount_vm_timer,
368 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
369 icount_adjust();
372 int64_t qemu_icount_round(int64_t count)
374 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
377 static struct qemu_alarm_timer alarm_timers[] = {
378 #ifndef _WIN32
379 #ifdef __linux__
380 {"dynticks", dynticks_start_timer,
381 dynticks_stop_timer, dynticks_rearm_timer, NULL},
382 /* HPET - if available - is preferred */
383 {"hpet", hpet_start_timer, hpet_stop_timer, NULL, NULL},
384 /* ...otherwise try RTC */
385 {"rtc", rtc_start_timer, rtc_stop_timer, NULL, NULL},
386 #endif
387 {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
388 #else
389 {"dynticks", win32_start_timer,
390 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
391 {"win32", win32_start_timer,
392 win32_stop_timer, NULL, &alarm_win32_data},
393 #endif
394 {NULL, }
397 static void show_available_alarms(void)
399 int i;
401 printf("Available alarm timers, in order of precedence:\n");
402 for (i = 0; alarm_timers[i].name; i++)
403 printf("%s\n", alarm_timers[i].name);
406 void configure_alarms(char const *opt)
408 int i;
409 int cur = 0;
410 int count = ARRAY_SIZE(alarm_timers) - 1;
411 char *arg;
412 char *name;
413 struct qemu_alarm_timer tmp;
415 if (!strcmp(opt, "?")) {
416 show_available_alarms();
417 exit(0);
420 arg = qemu_strdup(opt);
422 /* Reorder the array */
423 name = strtok(arg, ",");
424 while (name) {
425 for (i = 0; i < count && alarm_timers[i].name; i++) {
426 if (!strcmp(alarm_timers[i].name, name))
427 break;
430 if (i == count) {
431 fprintf(stderr, "Unknown clock %s\n", name);
432 goto next;
435 if (i < cur)
436 /* Ignore */
437 goto next;
439 /* Swap */
440 tmp = alarm_timers[i];
441 alarm_timers[i] = alarm_timers[cur];
442 alarm_timers[cur] = tmp;
444 cur++;
445 next:
446 name = strtok(NULL, ",");
449 qemu_free(arg);
451 if (cur) {
452 /* Disable remaining timers */
453 for (i = cur; i < count; i++)
454 alarm_timers[i].name = NULL;
455 } else {
456 show_available_alarms();
457 exit(1);
461 #define QEMU_NUM_CLOCKS 3
463 QEMUClock *rt_clock;
464 QEMUClock *vm_clock;
465 QEMUClock *host_clock;
467 static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
469 static QEMUClock *qemu_new_clock(int type)
471 QEMUClock *clock;
472 clock = qemu_mallocz(sizeof(QEMUClock));
473 clock->type = type;
474 clock->enabled = 1;
475 return clock;
478 void qemu_clock_enable(QEMUClock *clock, int enabled)
480 clock->enabled = enabled;
483 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
485 QEMUTimer *ts;
487 ts = qemu_mallocz(sizeof(QEMUTimer));
488 ts->clock = clock;
489 ts->cb = cb;
490 ts->opaque = opaque;
491 return ts;
494 void qemu_free_timer(QEMUTimer *ts)
496 qemu_free(ts);
499 /* stop a timer, but do not dealloc it */
500 void qemu_del_timer(QEMUTimer *ts)
502 QEMUTimer **pt, *t;
504 /* NOTE: this code must be signal safe because
505 qemu_timer_expired() can be called from a signal. */
506 pt = &active_timers[ts->clock->type];
507 for(;;) {
508 t = *pt;
509 if (!t)
510 break;
511 if (t == ts) {
512 *pt = t->next;
513 break;
515 pt = &t->next;
519 /* modify the current timer so that it will be fired when current_time
520 >= expire_time. The corresponding callback will be called. */
521 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
523 QEMUTimer **pt, *t;
525 qemu_del_timer(ts);
527 /* add the timer in the sorted list */
528 /* NOTE: this code must be signal safe because
529 qemu_timer_expired() can be called from a signal. */
530 pt = &active_timers[ts->clock->type];
531 for(;;) {
532 t = *pt;
533 if (!t)
534 break;
535 if (t->expire_time > expire_time)
536 break;
537 pt = &t->next;
539 ts->expire_time = expire_time;
540 ts->next = *pt;
541 *pt = ts;
543 /* Rearm if necessary */
544 if (pt == &active_timers[ts->clock->type]) {
545 if (!alarm_timer->pending) {
546 qemu_rearm_alarm_timer(alarm_timer);
548 /* Interrupt execution to force deadline recalculation. */
549 if (use_icount)
550 qemu_notify_event();
554 int qemu_timer_pending(QEMUTimer *ts)
556 QEMUTimer *t;
557 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
558 if (t == ts)
559 return 1;
561 return 0;
564 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
566 if (!timer_head)
567 return 0;
568 return (timer_head->expire_time <= current_time);
571 static void qemu_run_timers(QEMUClock *clock)
573 QEMUTimer **ptimer_head, *ts;
574 int64_t current_time;
576 if (!clock->enabled)
577 return;
579 current_time = qemu_get_clock (clock);
580 ptimer_head = &active_timers[clock->type];
581 for(;;) {
582 ts = *ptimer_head;
583 if (!ts || ts->expire_time > current_time)
584 break;
585 /* remove timer from the list before calling the callback */
586 *ptimer_head = ts->next;
587 ts->next = NULL;
589 /* run the callback (the timer list can be modified) */
590 ts->cb(ts->opaque);
594 int64_t qemu_get_clock(QEMUClock *clock)
596 switch(clock->type) {
597 case QEMU_CLOCK_REALTIME:
598 return get_clock() / 1000000;
599 default:
600 case QEMU_CLOCK_VIRTUAL:
601 if (use_icount) {
602 return cpu_get_icount();
603 } else {
604 return cpu_get_clock();
606 case QEMU_CLOCK_HOST:
607 return get_clock_realtime();
611 int64_t qemu_get_clock_ns(QEMUClock *clock)
613 switch(clock->type) {
614 case QEMU_CLOCK_REALTIME:
615 return get_clock();
616 default:
617 case QEMU_CLOCK_VIRTUAL:
618 if (use_icount) {
619 return cpu_get_icount();
620 } else {
621 return cpu_get_clock();
623 case QEMU_CLOCK_HOST:
624 return get_clock_realtime();
628 void init_clocks(void)
630 init_get_clock();
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(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(0, &vmstate_timers, &timers_state);
679 if (!option)
680 return;
682 if (strcmp(option, "auto") != 0) {
683 icount_time_shift = strtol(option, NULL, 0);
684 use_icount = 1;
685 return;
688 use_icount = 2;
690 /* 125MIPS seems a reasonable initial guess at the guest speed.
691 It will be corrected fairly quickly anyway. */
692 icount_time_shift = 3;
694 /* Have both realtime and virtual time triggers for speed adjustment.
695 The realtime trigger catches emulated time passing too slowly,
696 the virtual time trigger catches emulated time passing too fast.
697 Realtime triggers occur even when idle, so use them less frequently
698 than VM triggers. */
699 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
700 qemu_mod_timer(icount_rt_timer,
701 qemu_get_clock(rt_clock) + 1000);
702 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
703 qemu_mod_timer(icount_vm_timer,
704 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
707 void qemu_run_all_timers(void)
709 /* rearm timer, if not periodic */
710 if (alarm_timer->expired) {
711 alarm_timer->expired = 0;
712 qemu_rearm_alarm_timer(alarm_timer);
715 alarm_timer->pending = 0;
717 /* vm time timers */
718 if (vm_running) {
719 qemu_run_timers(vm_clock);
722 qemu_run_timers(rt_clock);
723 qemu_run_timers(host_clock);
726 #ifdef _WIN32
727 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
728 DWORD_PTR dwUser, DWORD_PTR dw1,
729 DWORD_PTR dw2)
730 #else
731 static void host_alarm_handler(int host_signum)
732 #endif
734 struct qemu_alarm_timer *t = alarm_timer;
735 if (!t)
736 return;
738 #if 0
739 #define DISP_FREQ 1000
741 static int64_t delta_min = INT64_MAX;
742 static int64_t delta_max, delta_cum, last_clock, delta, ti;
743 static int count;
744 ti = qemu_get_clock(vm_clock);
745 if (last_clock != 0) {
746 delta = ti - last_clock;
747 if (delta < delta_min)
748 delta_min = delta;
749 if (delta > delta_max)
750 delta_max = delta;
751 delta_cum += delta;
752 if (++count == DISP_FREQ) {
753 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
754 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
755 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
756 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
757 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
758 count = 0;
759 delta_min = INT64_MAX;
760 delta_max = 0;
761 delta_cum = 0;
764 last_clock = ti;
766 #endif
767 if (alarm_has_dynticks(t) ||
768 (!use_icount &&
769 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
770 qemu_get_clock(vm_clock))) ||
771 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
772 qemu_get_clock(rt_clock)) ||
773 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
774 qemu_get_clock(host_clock))) {
776 t->expired = alarm_has_dynticks(t);
777 t->pending = 1;
778 qemu_notify_event();
782 int64_t qemu_next_deadline(void)
784 /* To avoid problems with overflow limit this to 2^32. */
785 int64_t delta = INT32_MAX;
787 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
788 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
789 qemu_get_clock(vm_clock);
791 if (active_timers[QEMU_CLOCK_HOST]) {
792 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
793 qemu_get_clock(host_clock);
794 if (hdelta < delta)
795 delta = hdelta;
798 if (delta < 0)
799 delta = 0;
801 return delta;
804 #ifndef _WIN32
806 #if defined(__linux__)
808 #define RTC_FREQ 1024
810 static uint64_t qemu_next_deadline_dyntick(void)
812 int64_t delta;
813 int64_t rtdelta;
815 if (use_icount)
816 delta = INT32_MAX;
817 else
818 delta = (qemu_next_deadline() + 999) / 1000;
820 if (active_timers[QEMU_CLOCK_REALTIME]) {
821 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
822 qemu_get_clock(rt_clock))*1000;
823 if (rtdelta < delta)
824 delta = rtdelta;
827 if (delta < MIN_TIMER_REARM_US)
828 delta = MIN_TIMER_REARM_US;
830 return delta;
833 static void enable_sigio_timer(int fd)
835 struct sigaction act;
837 /* timer signal */
838 sigfillset(&act.sa_mask);
839 act.sa_flags = 0;
840 act.sa_handler = host_alarm_handler;
842 sigaction(SIGIO, &act, NULL);
843 fcntl_setfl(fd, O_ASYNC);
844 fcntl(fd, F_SETOWN, getpid());
847 static int hpet_start_timer(struct qemu_alarm_timer *t)
849 struct hpet_info info;
850 int r, fd;
852 fd = qemu_open("/dev/hpet", O_RDONLY);
853 if (fd < 0)
854 return -1;
856 /* Set frequency */
857 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
858 if (r < 0) {
859 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
860 "error, but for better emulation accuracy type:\n"
861 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
862 goto fail;
865 /* Check capabilities */
866 r = ioctl(fd, HPET_INFO, &info);
867 if (r < 0)
868 goto fail;
870 /* Enable periodic mode */
871 r = ioctl(fd, HPET_EPI, 0);
872 if (info.hi_flags && (r < 0))
873 goto fail;
875 /* Enable interrupt */
876 r = ioctl(fd, HPET_IE_ON, 0);
877 if (r < 0)
878 goto fail;
880 enable_sigio_timer(fd);
881 t->priv = (void *)(long)fd;
883 return 0;
884 fail:
885 close(fd);
886 return -1;
889 static void hpet_stop_timer(struct qemu_alarm_timer *t)
891 int fd = (long)t->priv;
893 close(fd);
896 static int rtc_start_timer(struct qemu_alarm_timer *t)
898 int rtc_fd;
899 unsigned long current_rtc_freq = 0;
901 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
902 if (rtc_fd < 0)
903 return -1;
904 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
905 if (current_rtc_freq != RTC_FREQ &&
906 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
907 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
908 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
909 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
910 goto fail;
912 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
913 fail:
914 close(rtc_fd);
915 return -1;
918 enable_sigio_timer(rtc_fd);
920 t->priv = (void *)(long)rtc_fd;
922 return 0;
925 static void rtc_stop_timer(struct qemu_alarm_timer *t)
927 int rtc_fd = (long)t->priv;
929 close(rtc_fd);
932 static int dynticks_start_timer(struct qemu_alarm_timer *t)
934 struct sigevent ev;
935 timer_t host_timer;
936 struct sigaction act;
938 sigfillset(&act.sa_mask);
939 act.sa_flags = 0;
940 act.sa_handler = host_alarm_handler;
942 sigaction(SIGALRM, &act, NULL);
945 * Initialize ev struct to 0 to avoid valgrind complaining
946 * about uninitialized data in timer_create call
948 memset(&ev, 0, sizeof(ev));
949 ev.sigev_value.sival_int = 0;
950 ev.sigev_notify = SIGEV_SIGNAL;
951 ev.sigev_signo = SIGALRM;
953 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
954 perror("timer_create");
956 /* disable dynticks */
957 fprintf(stderr, "Dynamic Ticks disabled\n");
959 return -1;
962 t->priv = (void *)(long)host_timer;
964 return 0;
967 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
969 timer_t host_timer = (timer_t)(long)t->priv;
971 timer_delete(host_timer);
974 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
976 timer_t host_timer = (timer_t)(long)t->priv;
977 struct itimerspec timeout;
978 int64_t nearest_delta_us = INT64_MAX;
979 int64_t current_us;
981 assert(alarm_has_dynticks(t));
982 if (!active_timers[QEMU_CLOCK_REALTIME] &&
983 !active_timers[QEMU_CLOCK_VIRTUAL] &&
984 !active_timers[QEMU_CLOCK_HOST])
985 return;
987 nearest_delta_us = qemu_next_deadline_dyntick();
989 /* check whether a timer is already running */
990 if (timer_gettime(host_timer, &timeout)) {
991 perror("gettime");
992 fprintf(stderr, "Internal timer error: aborting\n");
993 exit(1);
995 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
996 if (current_us && current_us <= nearest_delta_us)
997 return;
999 timeout.it_interval.tv_sec = 0;
1000 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1001 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1002 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1003 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1004 perror("settime");
1005 fprintf(stderr, "Internal timer error: aborting\n");
1006 exit(1);
1010 #endif /* defined(__linux__) */
1012 static int unix_start_timer(struct qemu_alarm_timer *t)
1014 struct sigaction act;
1015 struct itimerval itv;
1016 int err;
1018 /* timer signal */
1019 sigfillset(&act.sa_mask);
1020 act.sa_flags = 0;
1021 act.sa_handler = host_alarm_handler;
1023 sigaction(SIGALRM, &act, NULL);
1025 itv.it_interval.tv_sec = 0;
1026 /* for i386 kernel 2.6 to get 1 ms */
1027 itv.it_interval.tv_usec = 999;
1028 itv.it_value.tv_sec = 0;
1029 itv.it_value.tv_usec = 10 * 1000;
1031 err = setitimer(ITIMER_REAL, &itv, NULL);
1032 if (err)
1033 return -1;
1035 return 0;
1038 static void unix_stop_timer(struct qemu_alarm_timer *t)
1040 struct itimerval itv;
1042 memset(&itv, 0, sizeof(itv));
1043 setitimer(ITIMER_REAL, &itv, NULL);
1046 #endif /* !defined(_WIN32) */
1049 #ifdef _WIN32
1051 static int win32_start_timer(struct qemu_alarm_timer *t)
1053 TIMECAPS tc;
1054 struct qemu_alarm_win32 *data = t->priv;
1055 UINT flags;
1057 memset(&tc, 0, sizeof(tc));
1058 timeGetDevCaps(&tc, sizeof(tc));
1060 data->period = tc.wPeriodMin;
1061 timeBeginPeriod(data->period);
1063 flags = TIME_CALLBACK_FUNCTION;
1064 if (alarm_has_dynticks(t))
1065 flags |= TIME_ONESHOT;
1066 else
1067 flags |= TIME_PERIODIC;
1069 data->timerId = timeSetEvent(1, // interval (ms)
1070 data->period, // resolution
1071 host_alarm_handler, // function
1072 (DWORD)t, // parameter
1073 flags);
1075 if (!data->timerId) {
1076 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1077 GetLastError());
1078 timeEndPeriod(data->period);
1079 return -1;
1082 return 0;
1085 static void win32_stop_timer(struct qemu_alarm_timer *t)
1087 struct qemu_alarm_win32 *data = t->priv;
1089 timeKillEvent(data->timerId);
1090 timeEndPeriod(data->period);
1093 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1095 struct qemu_alarm_win32 *data = t->priv;
1097 assert(alarm_has_dynticks(t));
1098 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1099 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1100 !active_timers[QEMU_CLOCK_HOST])
1101 return;
1103 timeKillEvent(data->timerId);
1105 data->timerId = timeSetEvent(1,
1106 data->period,
1107 host_alarm_handler,
1108 (DWORD)t,
1109 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1111 if (!data->timerId) {
1112 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1113 GetLastError());
1115 timeEndPeriod(data->period);
1116 exit(1);
1120 #endif /* _WIN32 */
1122 static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1124 if (running)
1125 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1128 int init_timer_alarm(void)
1130 struct qemu_alarm_timer *t = NULL;
1131 int i, err = -1;
1133 for (i = 0; alarm_timers[i].name; i++) {
1134 t = &alarm_timers[i];
1136 err = t->start(t);
1137 if (!err)
1138 break;
1141 if (err) {
1142 err = -ENOENT;
1143 goto fail;
1146 /* first event is at time 0 */
1147 t->pending = 1;
1148 alarm_timer = t;
1149 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1151 return 0;
1153 fail:
1154 return err;
1157 void quit_timers(void)
1159 struct qemu_alarm_timer *t = alarm_timer;
1160 alarm_timer = NULL;
1161 t->stop(t);
1164 int qemu_calculate_timeout(void)
1166 #ifndef CONFIG_IOTHREAD
1167 int timeout;
1169 if (!vm_running)
1170 timeout = 5000;
1171 else {
1172 /* XXX: use timeout computed from timers */
1173 int64_t add;
1174 int64_t delta;
1175 /* Advance virtual time to the next event. */
1176 delta = qemu_icount_delta();
1177 if (delta > 0) {
1178 /* If virtual time is ahead of real time then just
1179 wait for IO. */
1180 timeout = (delta + 999999) / 1000000;
1181 } else {
1182 /* Wait for either IO to occur or the next
1183 timer event. */
1184 add = qemu_next_deadline();
1185 /* We advance the timer before checking for IO.
1186 Limit the amount we advance so that early IO
1187 activity won't get the guest too far ahead. */
1188 if (add > 10000000)
1189 add = 10000000;
1190 delta += add;
1191 qemu_icount += qemu_icount_round (add);
1192 timeout = delta / 1000000;
1193 if (timeout < 0)
1194 timeout = 0;
1198 return timeout;
1199 #else /* CONFIG_IOTHREAD */
1200 return 1000;
1201 #endif