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
;
68 /***********************************************************/
69 /* real time host monotonic timer */
72 static int64_t get_clock_realtime(void)
76 gettimeofday(&tv
, NULL
);
77 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
82 static int64_t clock_freq
;
84 static void init_get_clock(void)
88 ret
= QueryPerformanceFrequency(&freq
);
90 fprintf(stderr
, "Could not calibrate ticks\n");
93 clock_freq
= freq
.QuadPart
;
96 static int64_t get_clock(void)
99 QueryPerformanceCounter(&ti
);
100 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
105 static int use_rt_clock
;
107 static void init_get_clock(void)
110 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
111 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
114 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
121 static int64_t get_clock(void)
123 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
124 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
127 clock_gettime(CLOCK_MONOTONIC
, &ts
);
128 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
132 /* XXX: using gettimeofday leads to problems if the date
133 changes, so it should be avoided. */
134 return get_clock_realtime();
139 /***********************************************************/
140 /* guest cycle counter */
142 typedef struct TimersState
{
143 int64_t cpu_ticks_prev
;
144 int64_t cpu_ticks_offset
;
145 int64_t cpu_clock_offset
;
146 int32_t cpu_ticks_enabled
;
150 TimersState timers_state
;
152 /* return the host CPU cycle counter and handle stop/restart */
153 int64_t cpu_get_ticks(void)
156 return cpu_get_icount();
158 if (!timers_state
.cpu_ticks_enabled
) {
159 return timers_state
.cpu_ticks_offset
;
162 ticks
= cpu_get_real_ticks();
163 if (timers_state
.cpu_ticks_prev
> ticks
) {
164 /* Note: non increasing ticks may happen if the host uses
166 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
168 timers_state
.cpu_ticks_prev
= ticks
;
169 return ticks
+ timers_state
.cpu_ticks_offset
;
173 /* return the host CPU monotonic timer and handle stop/restart */
174 static int64_t cpu_get_clock(void)
177 if (!timers_state
.cpu_ticks_enabled
) {
178 return timers_state
.cpu_clock_offset
;
181 return ti
+ timers_state
.cpu_clock_offset
;
185 /* FIXME: qemu-kvm hack */
186 #define CONFIG_IOTHREAD 1
187 #ifndef CONFIG_IOTHREAD
188 static int64_t qemu_icount_delta(void)
191 return 5000 * (int64_t) 1000000;
192 } else if (use_icount
== 1) {
193 /* When not using an adaptive execution frequency
194 we tend to get badly out of sync with real time,
195 so just delay for a reasonable amount of time. */
198 return cpu_get_icount() - cpu_get_clock();
203 /* enable cpu_get_ticks() */
204 void cpu_enable_ticks(void)
206 if (!timers_state
.cpu_ticks_enabled
) {
207 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
208 timers_state
.cpu_clock_offset
-= get_clock();
209 timers_state
.cpu_ticks_enabled
= 1;
213 /* disable cpu_get_ticks() : the clock is stopped. You must not call
214 cpu_get_ticks() after that. */
215 void cpu_disable_ticks(void)
217 if (timers_state
.cpu_ticks_enabled
) {
218 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
219 timers_state
.cpu_clock_offset
= cpu_get_clock();
220 timers_state
.cpu_ticks_enabled
= 0;
224 /***********************************************************/
227 #define QEMU_CLOCK_REALTIME 0
228 #define QEMU_CLOCK_VIRTUAL 1
229 #define QEMU_CLOCK_HOST 2
234 /* XXX: add frequency */
242 struct QEMUTimer
*next
;
245 struct qemu_alarm_timer
{
247 int (*start
)(struct qemu_alarm_timer
*t
);
248 void (*stop
)(struct qemu_alarm_timer
*t
);
249 void (*rearm
)(struct qemu_alarm_timer
*t
);
256 static struct qemu_alarm_timer
*alarm_timer
;
258 int qemu_alarm_pending(void)
260 return alarm_timer
->pending
;
263 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
268 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
270 if (!alarm_has_dynticks(t
))
276 /* TODO: MIN_TIMER_REARM_US should be optimized */
277 #define MIN_TIMER_REARM_US 250
281 struct qemu_alarm_win32
{
284 } alarm_win32_data
= {0, 0};
286 static int win32_start_timer(struct qemu_alarm_timer
*t
);
287 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
288 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
292 static int unix_start_timer(struct qemu_alarm_timer
*t
);
293 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
297 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
298 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
299 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
301 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
302 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
304 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
305 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
307 #endif /* __linux__ */
311 /* Correlation between real and virtual time is always going to be
312 fairly approximate, so ignore small variation.
313 When the guest is idle real and virtual time will be aligned in
315 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
317 static void icount_adjust(void)
322 static int64_t last_delta
;
323 /* If the VM is not running, then do nothing. */
327 cur_time
= cpu_get_clock();
328 cur_icount
= qemu_get_clock(vm_clock
);
329 delta
= cur_icount
- cur_time
;
330 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
332 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
333 && icount_time_shift
> 0) {
334 /* The guest is getting too far ahead. Slow time down. */
338 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
339 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
340 /* The guest is getting too far behind. Speed time up. */
344 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
347 static void icount_adjust_rt(void * opaque
)
349 qemu_mod_timer(icount_rt_timer
,
350 qemu_get_clock(rt_clock
) + 1000);
354 static void icount_adjust_vm(void * opaque
)
356 qemu_mod_timer(icount_vm_timer
,
357 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
361 int64_t qemu_icount_round(int64_t count
)
363 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
366 static struct qemu_alarm_timer alarm_timers
[] = {
369 {"dynticks", dynticks_start_timer
,
370 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
371 /* HPET - if available - is preferred */
372 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
373 /* ...otherwise try RTC */
374 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
376 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
378 {"dynticks", win32_start_timer
,
379 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
380 {"win32", win32_start_timer
,
381 win32_stop_timer
, NULL
, &alarm_win32_data
},
386 static void show_available_alarms(void)
390 printf("Available alarm timers, in order of precedence:\n");
391 for (i
= 0; alarm_timers
[i
].name
; i
++)
392 printf("%s\n", alarm_timers
[i
].name
);
395 void configure_alarms(char const *opt
)
399 int count
= ARRAY_SIZE(alarm_timers
) - 1;
402 struct qemu_alarm_timer tmp
;
404 if (!strcmp(opt
, "?")) {
405 show_available_alarms();
409 arg
= qemu_strdup(opt
);
411 /* Reorder the array */
412 name
= strtok(arg
, ",");
414 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
415 if (!strcmp(alarm_timers
[i
].name
, name
))
420 fprintf(stderr
, "Unknown clock %s\n", name
);
429 tmp
= alarm_timers
[i
];
430 alarm_timers
[i
] = alarm_timers
[cur
];
431 alarm_timers
[cur
] = tmp
;
435 name
= strtok(NULL
, ",");
441 /* Disable remaining timers */
442 for (i
= cur
; i
< count
; i
++)
443 alarm_timers
[i
].name
= NULL
;
445 show_available_alarms();
450 #define QEMU_NUM_CLOCKS 3
454 QEMUClock
*host_clock
;
456 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
458 static QEMUClock
*qemu_new_clock(int type
)
461 clock
= qemu_mallocz(sizeof(QEMUClock
));
467 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
469 clock
->enabled
= enabled
;
472 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
476 ts
= qemu_mallocz(sizeof(QEMUTimer
));
483 void qemu_free_timer(QEMUTimer
*ts
)
488 /* stop a timer, but do not dealloc it */
489 void qemu_del_timer(QEMUTimer
*ts
)
493 /* NOTE: this code must be signal safe because
494 qemu_timer_expired() can be called from a signal. */
495 pt
= &active_timers
[ts
->clock
->type
];
508 /* modify the current timer so that it will be fired when current_time
509 >= expire_time. The corresponding callback will be called. */
510 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
516 /* add the timer in the sorted list */
517 /* NOTE: this code must be signal safe because
518 qemu_timer_expired() can be called from a signal. */
519 pt
= &active_timers
[ts
->clock
->type
];
524 if (t
->expire_time
> expire_time
)
528 ts
->expire_time
= expire_time
;
532 /* Rearm if necessary */
533 if (pt
== &active_timers
[ts
->clock
->type
]) {
534 if (!alarm_timer
->pending
) {
535 qemu_rearm_alarm_timer(alarm_timer
);
537 /* Interrupt execution to force deadline recalculation. */
543 int qemu_timer_pending(QEMUTimer
*ts
)
546 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
553 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
557 return (timer_head
->expire_time
<= current_time
);
560 static void qemu_run_timers(QEMUClock
*clock
)
562 QEMUTimer
**ptimer_head
, *ts
;
563 int64_t current_time
;
568 current_time
= qemu_get_clock (clock
);
569 ptimer_head
= &active_timers
[clock
->type
];
572 if (!ts
|| ts
->expire_time
> current_time
)
574 /* remove timer from the list before calling the callback */
575 *ptimer_head
= ts
->next
;
578 /* run the callback (the timer list can be modified) */
583 int64_t qemu_get_clock(QEMUClock
*clock
)
585 switch(clock
->type
) {
586 case QEMU_CLOCK_REALTIME
:
587 return get_clock() / 1000000;
589 case QEMU_CLOCK_VIRTUAL
:
591 return cpu_get_icount();
593 return cpu_get_clock();
595 case QEMU_CLOCK_HOST
:
596 return get_clock_realtime();
600 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
602 switch(clock
->type
) {
603 case QEMU_CLOCK_REALTIME
:
606 case QEMU_CLOCK_VIRTUAL
:
608 return cpu_get_icount();
610 return cpu_get_clock();
612 case QEMU_CLOCK_HOST
:
613 return get_clock_realtime();
617 void init_clocks(void)
620 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
621 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
622 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
624 rtc_clock
= host_clock
;
628 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
630 uint64_t expire_time
;
632 if (qemu_timer_pending(ts
)) {
633 expire_time
= ts
->expire_time
;
637 qemu_put_be64(f
, expire_time
);
640 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
642 uint64_t expire_time
;
644 expire_time
= qemu_get_be64(f
);
645 if (expire_time
!= -1) {
646 qemu_mod_timer(ts
, expire_time
);
652 static const VMStateDescription vmstate_timers
= {
655 .minimum_version_id
= 1,
656 .minimum_version_id_old
= 1,
657 .fields
= (VMStateField
[]) {
658 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
659 VMSTATE_INT64(dummy
, TimersState
),
660 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
661 VMSTATE_END_OF_LIST()
665 void configure_icount(const char *option
)
667 vmstate_register(0, &vmstate_timers
, &timers_state
);
671 if (strcmp(option
, "auto") != 0) {
672 icount_time_shift
= strtol(option
, NULL
, 0);
679 /* 125MIPS seems a reasonable initial guess at the guest speed.
680 It will be corrected fairly quickly anyway. */
681 icount_time_shift
= 3;
683 /* Have both realtime and virtual time triggers for speed adjustment.
684 The realtime trigger catches emulated time passing too slowly,
685 the virtual time trigger catches emulated time passing too fast.
686 Realtime triggers occur even when idle, so use them less frequently
688 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
689 qemu_mod_timer(icount_rt_timer
,
690 qemu_get_clock(rt_clock
) + 1000);
691 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
692 qemu_mod_timer(icount_vm_timer
,
693 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
696 void qemu_run_all_timers(void)
698 alarm_timer
->pending
= 0;
700 /* rearm timer, if not periodic */
701 if (alarm_timer
->expired
) {
702 alarm_timer
->expired
= 0;
703 qemu_rearm_alarm_timer(alarm_timer
);
708 qemu_run_timers(vm_clock
);
711 qemu_run_timers(rt_clock
);
712 qemu_run_timers(host_clock
);
716 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
717 DWORD_PTR dwUser
, DWORD_PTR dw1
,
720 static void host_alarm_handler(int host_signum
)
723 struct qemu_alarm_timer
*t
= alarm_timer
;
728 #define DISP_FREQ 1000
730 static int64_t delta_min
= INT64_MAX
;
731 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
733 ti
= qemu_get_clock(vm_clock
);
734 if (last_clock
!= 0) {
735 delta
= ti
- last_clock
;
736 if (delta
< delta_min
)
738 if (delta
> delta_max
)
741 if (++count
== DISP_FREQ
) {
742 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
743 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
744 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
745 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
746 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
748 delta_min
= INT64_MAX
;
756 if (alarm_has_dynticks(t
) ||
758 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
759 qemu_get_clock(vm_clock
))) ||
760 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
761 qemu_get_clock(rt_clock
)) ||
762 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
763 qemu_get_clock(host_clock
))) {
765 t
->expired
= alarm_has_dynticks(t
);
771 int64_t qemu_next_deadline(void)
773 /* To avoid problems with overflow limit this to 2^32. */
774 int64_t delta
= INT32_MAX
;
776 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
777 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
778 qemu_get_clock(vm_clock
);
780 if (active_timers
[QEMU_CLOCK_HOST
]) {
781 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
782 qemu_get_clock(host_clock
);
795 #if defined(__linux__)
797 #define RTC_FREQ 1024
799 static uint64_t qemu_next_deadline_dyntick(void)
807 delta
= (qemu_next_deadline() + 999) / 1000;
809 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
810 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
811 qemu_get_clock(rt_clock
))*1000;
816 if (delta
< MIN_TIMER_REARM_US
)
817 delta
= MIN_TIMER_REARM_US
;
822 static void enable_sigio_timer(int fd
)
824 struct sigaction act
;
827 sigfillset(&act
.sa_mask
);
829 act
.sa_handler
= host_alarm_handler
;
831 sigaction(SIGIO
, &act
, NULL
);
832 fcntl_setfl(fd
, O_ASYNC
);
833 fcntl(fd
, F_SETOWN
, getpid());
836 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
838 struct hpet_info info
;
841 fd
= qemu_open("/dev/hpet", O_RDONLY
);
846 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
848 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
849 "error, but for better emulation accuracy type:\n"
850 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
854 /* Check capabilities */
855 r
= ioctl(fd
, HPET_INFO
, &info
);
859 /* Enable periodic mode */
860 r
= ioctl(fd
, HPET_EPI
, 0);
861 if (info
.hi_flags
&& (r
< 0))
864 /* Enable interrupt */
865 r
= ioctl(fd
, HPET_IE_ON
, 0);
869 enable_sigio_timer(fd
);
870 t
->priv
= (void *)(long)fd
;
878 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
880 int fd
= (long)t
->priv
;
885 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
888 unsigned long current_rtc_freq
= 0;
890 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
893 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
894 if (current_rtc_freq
!= RTC_FREQ
&&
895 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
896 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
897 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
898 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
901 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
907 enable_sigio_timer(rtc_fd
);
909 t
->priv
= (void *)(long)rtc_fd
;
914 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
916 int rtc_fd
= (long)t
->priv
;
921 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
925 struct sigaction act
;
927 sigfillset(&act
.sa_mask
);
929 act
.sa_handler
= host_alarm_handler
;
931 sigaction(SIGALRM
, &act
, NULL
);
934 * Initialize ev struct to 0 to avoid valgrind complaining
935 * about uninitialized data in timer_create call
937 memset(&ev
, 0, sizeof(ev
));
938 ev
.sigev_value
.sival_int
= 0;
939 ev
.sigev_notify
= SIGEV_SIGNAL
;
940 ev
.sigev_signo
= SIGALRM
;
942 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
943 perror("timer_create");
945 /* disable dynticks */
946 fprintf(stderr
, "Dynamic Ticks disabled\n");
951 t
->priv
= (void *)(long)host_timer
;
956 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
958 timer_t host_timer
= (timer_t
)(long)t
->priv
;
960 timer_delete(host_timer
);
963 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
965 timer_t host_timer
= (timer_t
)(long)t
->priv
;
966 struct itimerspec timeout
;
967 int64_t nearest_delta_us
= INT64_MAX
;
970 assert(alarm_has_dynticks(t
));
971 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
972 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
973 !active_timers
[QEMU_CLOCK_HOST
])
976 nearest_delta_us
= qemu_next_deadline_dyntick();
978 /* check whether a timer is already running */
979 if (timer_gettime(host_timer
, &timeout
)) {
981 fprintf(stderr
, "Internal timer error: aborting\n");
984 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
985 if (current_us
&& current_us
<= nearest_delta_us
)
988 timeout
.it_interval
.tv_sec
= 0;
989 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
990 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
991 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
992 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
994 fprintf(stderr
, "Internal timer error: aborting\n");
999 #endif /* defined(__linux__) */
1001 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1003 struct sigaction act
;
1004 struct itimerval itv
;
1008 sigfillset(&act
.sa_mask
);
1010 act
.sa_handler
= host_alarm_handler
;
1012 sigaction(SIGALRM
, &act
, NULL
);
1014 itv
.it_interval
.tv_sec
= 0;
1015 /* for i386 kernel 2.6 to get 1 ms */
1016 itv
.it_interval
.tv_usec
= 999;
1017 itv
.it_value
.tv_sec
= 0;
1018 itv
.it_value
.tv_usec
= 10 * 1000;
1020 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1027 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1029 struct itimerval itv
;
1031 memset(&itv
, 0, sizeof(itv
));
1032 setitimer(ITIMER_REAL
, &itv
, NULL
);
1035 #endif /* !defined(_WIN32) */
1040 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1043 struct qemu_alarm_win32
*data
= t
->priv
;
1046 memset(&tc
, 0, sizeof(tc
));
1047 timeGetDevCaps(&tc
, sizeof(tc
));
1049 data
->period
= tc
.wPeriodMin
;
1050 timeBeginPeriod(data
->period
);
1052 flags
= TIME_CALLBACK_FUNCTION
;
1053 if (alarm_has_dynticks(t
))
1054 flags
|= TIME_ONESHOT
;
1056 flags
|= TIME_PERIODIC
;
1058 data
->timerId
= timeSetEvent(1, // interval (ms)
1059 data
->period
, // resolution
1060 host_alarm_handler
, // function
1061 (DWORD
)t
, // parameter
1064 if (!data
->timerId
) {
1065 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1067 timeEndPeriod(data
->period
);
1074 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1076 struct qemu_alarm_win32
*data
= t
->priv
;
1078 timeKillEvent(data
->timerId
);
1079 timeEndPeriod(data
->period
);
1082 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1084 struct qemu_alarm_win32
*data
= t
->priv
;
1086 assert(alarm_has_dynticks(t
));
1087 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1088 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1089 !active_timers
[QEMU_CLOCK_HOST
])
1092 timeKillEvent(data
->timerId
);
1094 data
->timerId
= timeSetEvent(1,
1098 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1100 if (!data
->timerId
) {
1101 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1104 timeEndPeriod(data
->period
);
1111 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1114 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1117 int init_timer_alarm(void)
1119 struct qemu_alarm_timer
*t
= NULL
;
1122 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1123 t
= &alarm_timers
[i
];
1135 /* first event is at time 0 */
1138 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1146 void quit_timers(void)
1148 struct qemu_alarm_timer
*t
= alarm_timer
;
1153 int qemu_calculate_timeout(void)
1155 #ifndef CONFIG_IOTHREAD
1161 /* XXX: use timeout computed from timers */
1164 /* Advance virtual time to the next event. */
1165 delta
= qemu_icount_delta();
1167 /* If virtual time is ahead of real time then just
1169 timeout
= (delta
+ 999999) / 1000000;
1171 /* Wait for either IO to occur or the next
1173 add
= qemu_next_deadline();
1174 /* We advance the timer before checking for IO.
1175 Limit the amount we advance so that early IO
1176 activity won't get the guest too far ahead. */
1180 qemu_icount
+= qemu_icount_round (add
);
1181 timeout
= delta
/ 1000000;
1188 #else /* CONFIG_IOTHREAD */