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
39 #include <sys/param.h>
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> */
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
;
78 TimersState timers_state
;
80 /* return the host CPU cycle counter and handle stop/restart */
81 int64_t cpu_get_ticks(void)
84 return cpu_get_icount();
86 if (!timers_state
.cpu_ticks_enabled
) {
87 return timers_state
.cpu_ticks_offset
;
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
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)
105 if (!timers_state
.cpu_ticks_enabled
) {
106 return timers_state
.cpu_clock_offset
;
109 return ti
+ timers_state
.cpu_clock_offset
;
113 #ifndef CONFIG_IOTHREAD
114 static int64_t qemu_icount_delta(void)
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. */
124 return cpu_get_icount() - cpu_get_clock();
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 /***********************************************************/
153 #define QEMU_CLOCK_REALTIME 0
154 #define QEMU_CLOCK_VIRTUAL 1
155 #define QEMU_CLOCK_HOST 2
161 QEMUTimer
*warp_timer
;
166 int64_t expire_time
; /* in nanoseconds */
170 struct QEMUTimer
*next
;
173 struct qemu_alarm_timer
{
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__)
181 #elif defined(_WIN32)
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
)
205 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
207 if (!alarm_has_dynticks(t
))
213 /* TODO: MIN_TIMER_REARM_NS should be optimized */
214 #define MIN_TIMER_REARM_NS 250000
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
);
228 static int unix_start_timer(struct qemu_alarm_timer
*t
);
229 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
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__ */
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
251 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
253 static void icount_adjust(void)
258 static int64_t last_delta
;
259 /* If the VM is not running, then do nothing. */
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. */
268 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
269 && icount_time_shift
> 0) {
270 /* The guest is getting too far ahead. Slow time down. */
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. */
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);
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);
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
[] = {
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
},
312 {"unix", unix_start_timer
, unix_stop_timer
, NULL
},
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
},
322 static void show_available_alarms(void)
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
)
335 int count
= ARRAY_SIZE(alarm_timers
) - 1;
338 struct qemu_alarm_timer tmp
;
340 if (!strcmp(opt
, "?")) {
341 show_available_alarms();
345 arg
= qemu_strdup(opt
);
347 /* Reorder the array */
348 name
= strtok(arg
, ",");
350 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
351 if (!strcmp(alarm_timers
[i
].name
, name
))
356 fprintf(stderr
, "Unknown clock %s\n", name
);
365 tmp
= alarm_timers
[i
];
366 alarm_timers
[i
] = alarm_timers
[cur
];
367 alarm_timers
[cur
] = tmp
;
371 name
= strtok(NULL
, ",");
377 /* Disable remaining timers */
378 for (i
= cur
; i
< count
; i
++)
379 alarm_timers
[i
].name
= NULL
;
381 show_available_alarms();
386 #define QEMU_NUM_CLOCKS 3
390 QEMUClock
*host_clock
;
392 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
394 static QEMUClock
*qemu_new_clock(int type
)
397 clock
= qemu_mallocz(sizeof(QEMUClock
));
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) {
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
;
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
))) {
436 vm_clock_warp_start
= -1;
439 void qemu_clock_warp(QEMUClock
*clock
)
443 if (!clock
->warp_timer
) {
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
);
467 vm_clock_warp_start
= qemu_get_clock_ns(rt_clock
);
468 deadline
= qemu_next_icount_deadline();
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
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
);
492 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
493 QEMUTimerCB
*cb
, void *opaque
)
497 ts
= qemu_mallocz(sizeof(QEMUTimer
));
505 void qemu_free_timer(QEMUTimer
*ts
)
510 /* stop a timer, but do not dealloc it */
511 void qemu_del_timer(QEMUTimer
*ts
)
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
];
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
)
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
];
544 if (!qemu_timer_expired_ns(t
, expire_time
)) {
549 ts
->expire_time
= expire_time
;
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
);
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
)
576 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
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
;
596 current_time
= qemu_get_clock_ns(clock
);
597 ptimer_head
= &active_timers
[clock
->type
];
600 if (!qemu_timer_expired_ns(ts
, current_time
)) {
603 /* remove timer from the list before calling the callback */
604 *ptimer_head
= ts
->next
;
607 /* run the callback (the timer list can be modified) */
612 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
614 switch(clock
->type
) {
615 case QEMU_CLOCK_REALTIME
:
618 case QEMU_CLOCK_VIRTUAL
:
620 return cpu_get_icount();
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
;
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
;
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
);
663 static const VMStateDescription vmstate_timers
= {
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
);
682 #ifdef CONFIG_IOTHREAD
683 vm_clock
->warp_timer
= qemu_new_timer_ns(rt_clock
, icount_warp_rt
, NULL
);
686 if (strcmp(option
, "auto") != 0) {
687 icount_time_shift
= strtol(option
, NULL
, 0);
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
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
);
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);
733 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
735 static void host_alarm_handler(int host_signum
)
738 struct qemu_alarm_timer
*t
= alarm_timer
;
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
;
748 ti
= qemu_get_clock_ns(vm_clock
);
749 if (last_clock
!= 0) {
750 delta
= ti
- last_clock
;
751 if (delta
< delta_min
)
753 if (delta
> delta_max
)
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
));
763 delta_min
= INT64_MAX
;
771 if (alarm_has_dynticks(t
) ||
772 qemu_next_alarm_deadline () <= 0) {
773 t
->expired
= alarm_has_dynticks(t
);
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
;
785 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
786 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
787 qemu_get_clock_ns(vm_clock
);
796 static int64_t qemu_next_alarm_deadline(void)
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
);
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
);
813 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
814 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
815 qemu_get_clock_ns(rt_clock
));
823 #if defined(__linux__)
825 #define RTC_FREQ 1024
827 static void enable_sigio_timer(int fd
)
829 struct sigaction act
;
832 sigfillset(&act
.sa_mask
);
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
;
846 fd
= qemu_open("/dev/hpet", O_RDONLY
);
851 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
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");
859 /* Check capabilities */
860 r
= ioctl(fd
, HPET_INFO
, &info
);
864 /* Enable periodic mode */
865 r
= ioctl(fd
, HPET_EPI
, 0);
866 if (info
.hi_flags
&& (r
< 0))
869 /* Enable interrupt */
870 r
= ioctl(fd
, HPET_IE_ON
, 0);
874 enable_sigio_timer(fd
);
883 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
890 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
893 unsigned long current_rtc_freq
= 0;
895 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
898 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_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");
906 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
912 enable_sigio_timer(rtc_fd
);
919 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
926 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
930 struct sigaction act
;
932 sigfillset(&act
.sa_mask
);
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");
956 t
->timer
= host_timer
;
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
;
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
])
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
)) {
988 fprintf(stderr
, "Internal timer error: aborting\n");
991 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
992 if (current_ns
&& current_ns
<= nearest_delta_ns
)
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
)) {
1001 fprintf(stderr
, "Internal timer error: aborting\n");
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
;
1017 sigfillset(&act
.sa_mask
);
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
);
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) */
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
,
1056 struct qemu_alarm_timer
*t
= alarm_timer
;
1060 if (alarm_has_dynticks(t
) || qemu_next_alarm_deadline() <= 0) {
1061 t
->expired
= alarm_has_dynticks(t
);
1063 qemu_notify_event();
1067 static int mm_start_timer(struct qemu_alarm_timer
*t
)
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
;
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 */
1092 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1094 timeEndPeriod(mm_period
);
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
]) {
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
,
1128 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1131 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1134 timeEndPeriod(mm_period
);
1139 static int win32_start_timer(struct qemu_alarm_timer
*t
)
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
,
1153 alarm_has_dynticks(t
) ? 3600000 : 1,
1154 WT_EXECUTEINTIMERTHREAD
);
1157 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1166 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1168 HANDLE hTimer
= t
->timer
;
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
;
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
])
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
,
1197 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
1206 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1209 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1212 int init_timer_alarm(void)
1214 struct qemu_alarm_timer
*t
= NULL
;
1217 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1218 t
= &alarm_timers
[i
];
1230 /* first event is at time 0 */
1233 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1241 void quit_timers(void)
1243 struct qemu_alarm_timer
*t
= alarm_timer
;
1248 int qemu_calculate_timeout(void)
1250 #ifndef CONFIG_IOTHREAD
1256 /* XXX: use timeout computed from timers */
1259 /* Advance virtual time to the next event. */
1260 delta
= qemu_icount_delta();
1262 /* If virtual time is ahead of real time then just
1264 timeout
= (delta
+ 999999) / 1000000;
1266 /* Wait for either IO to occur or the next
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. */
1275 qemu_icount
+= qemu_icount_round (add
);
1276 timeout
= delta
/ 1000000;
1283 #else /* CONFIG_IOTHREAD */