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> */
57 #include "qemu-timer.h"
60 /* Conversion factor from emulated instructions to virtual clock ticks. */
61 static int icount_time_shift
;
62 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
63 #define MAX_ICOUNT_SHIFT 10
64 /* Compensate for varying guest execution speed. */
65 static int64_t qemu_icount_bias
;
66 static QEMUTimer
*icount_rt_timer
;
67 static QEMUTimer
*icount_vm_timer
;
70 /***********************************************************/
71 /* real time host monotonic timer */
74 static int64_t get_clock_realtime(void)
78 gettimeofday(&tv
, NULL
);
79 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
84 static int64_t clock_freq
;
86 static void init_get_clock(void)
90 ret
= QueryPerformanceFrequency(&freq
);
92 fprintf(stderr
, "Could not calibrate ticks\n");
95 clock_freq
= freq
.QuadPart
;
98 static int64_t get_clock(void)
101 QueryPerformanceCounter(&ti
);
102 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
107 static int use_rt_clock
;
109 static void init_get_clock(void)
112 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
113 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
116 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
123 static int64_t get_clock(void)
125 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
126 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
129 clock_gettime(CLOCK_MONOTONIC
, &ts
);
130 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
134 /* XXX: using gettimeofday leads to problems if the date
135 changes, so it should be avoided. */
136 return get_clock_realtime();
141 /* Return the virtual CPU time, based on the instruction counter. */
142 static int64_t cpu_get_icount(void)
145 CPUState
*env
= cpu_single_env
;;
146 icount
= qemu_icount
;
149 fprintf(stderr
, "Bad clock read\n");
150 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
152 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
155 /***********************************************************/
156 /* guest cycle counter */
158 typedef struct TimersState
{
159 int64_t cpu_ticks_prev
;
160 int64_t cpu_ticks_offset
;
161 int64_t cpu_clock_offset
;
162 int32_t cpu_ticks_enabled
;
166 TimersState timers_state
;
168 /* return the host CPU cycle counter and handle stop/restart */
169 int64_t cpu_get_ticks(void)
172 return cpu_get_icount();
174 if (!timers_state
.cpu_ticks_enabled
) {
175 return timers_state
.cpu_ticks_offset
;
178 ticks
= cpu_get_real_ticks();
179 if (timers_state
.cpu_ticks_prev
> ticks
) {
180 /* Note: non increasing ticks may happen if the host uses
182 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
184 timers_state
.cpu_ticks_prev
= ticks
;
185 return ticks
+ timers_state
.cpu_ticks_offset
;
189 /* return the host CPU monotonic timer and handle stop/restart */
190 static int64_t cpu_get_clock(void)
193 if (!timers_state
.cpu_ticks_enabled
) {
194 return timers_state
.cpu_clock_offset
;
197 return ti
+ timers_state
.cpu_clock_offset
;
201 #ifndef CONFIG_IOTHREAD
202 static int64_t qemu_icount_delta(void)
205 return 5000 * (int64_t) 1000000;
206 } else if (use_icount
== 1) {
207 /* When not using an adaptive execution frequency
208 we tend to get badly out of sync with real time,
209 so just delay for a reasonable amount of time. */
212 return cpu_get_icount() - cpu_get_clock();
217 /* enable cpu_get_ticks() */
218 void cpu_enable_ticks(void)
220 if (!timers_state
.cpu_ticks_enabled
) {
221 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
222 timers_state
.cpu_clock_offset
-= get_clock();
223 timers_state
.cpu_ticks_enabled
= 1;
227 /* disable cpu_get_ticks() : the clock is stopped. You must not call
228 cpu_get_ticks() after that. */
229 void cpu_disable_ticks(void)
231 if (timers_state
.cpu_ticks_enabled
) {
232 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
233 timers_state
.cpu_clock_offset
= cpu_get_clock();
234 timers_state
.cpu_ticks_enabled
= 0;
238 /***********************************************************/
241 #define QEMU_CLOCK_REALTIME 0
242 #define QEMU_CLOCK_VIRTUAL 1
243 #define QEMU_CLOCK_HOST 2
248 /* XXX: add frequency */
256 struct QEMUTimer
*next
;
259 struct qemu_alarm_timer
{
261 int (*start
)(struct qemu_alarm_timer
*t
);
262 void (*stop
)(struct qemu_alarm_timer
*t
);
263 void (*rearm
)(struct qemu_alarm_timer
*t
);
270 static struct qemu_alarm_timer
*alarm_timer
;
272 int qemu_alarm_pending(void)
274 return alarm_timer
->pending
;
277 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
282 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
284 if (!alarm_has_dynticks(t
))
290 /* TODO: MIN_TIMER_REARM_US should be optimized */
291 #define MIN_TIMER_REARM_US 250
295 struct qemu_alarm_win32
{
298 } alarm_win32_data
= {0, 0};
300 static int win32_start_timer(struct qemu_alarm_timer
*t
);
301 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
302 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
306 static int unix_start_timer(struct qemu_alarm_timer
*t
);
307 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
311 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
312 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
313 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
315 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
316 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
318 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
319 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
321 #endif /* __linux__ */
325 /* Correlation between real and virtual time is always going to be
326 fairly approximate, so ignore small variation.
327 When the guest is idle real and virtual time will be aligned in
329 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
331 static void icount_adjust(void)
336 static int64_t last_delta
;
337 /* If the VM is not running, then do nothing. */
341 cur_time
= cpu_get_clock();
342 cur_icount
= qemu_get_clock(vm_clock
);
343 delta
= cur_icount
- cur_time
;
344 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
346 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
347 && icount_time_shift
> 0) {
348 /* The guest is getting too far ahead. Slow time down. */
352 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
353 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
354 /* The guest is getting too far behind. Speed time up. */
358 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
361 static void icount_adjust_rt(void * opaque
)
363 qemu_mod_timer(icount_rt_timer
,
364 qemu_get_clock(rt_clock
) + 1000);
368 static void icount_adjust_vm(void * opaque
)
370 qemu_mod_timer(icount_vm_timer
,
371 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
375 int64_t qemu_icount_round(int64_t count
)
377 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
380 static struct qemu_alarm_timer alarm_timers
[] = {
383 {"dynticks", dynticks_start_timer
,
384 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
385 /* HPET - if available - is preferred */
386 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
387 /* ...otherwise try RTC */
388 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
390 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
392 {"dynticks", win32_start_timer
,
393 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
394 {"win32", win32_start_timer
,
395 win32_stop_timer
, NULL
, &alarm_win32_data
},
400 static void show_available_alarms(void)
404 printf("Available alarm timers, in order of precedence:\n");
405 for (i
= 0; alarm_timers
[i
].name
; i
++)
406 printf("%s\n", alarm_timers
[i
].name
);
409 void configure_alarms(char const *opt
)
413 int count
= ARRAY_SIZE(alarm_timers
) - 1;
416 struct qemu_alarm_timer tmp
;
418 if (!strcmp(opt
, "?")) {
419 show_available_alarms();
423 arg
= qemu_strdup(opt
);
425 /* Reorder the array */
426 name
= strtok(arg
, ",");
428 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
429 if (!strcmp(alarm_timers
[i
].name
, name
))
434 fprintf(stderr
, "Unknown clock %s\n", name
);
443 tmp
= alarm_timers
[i
];
444 alarm_timers
[i
] = alarm_timers
[cur
];
445 alarm_timers
[cur
] = tmp
;
449 name
= strtok(NULL
, ",");
455 /* Disable remaining timers */
456 for (i
= cur
; i
< count
; i
++)
457 alarm_timers
[i
].name
= NULL
;
459 show_available_alarms();
464 #define QEMU_NUM_CLOCKS 3
468 QEMUClock
*host_clock
;
470 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
472 static QEMUClock
*qemu_new_clock(int type
)
475 clock
= qemu_mallocz(sizeof(QEMUClock
));
481 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
483 clock
->enabled
= enabled
;
486 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
490 ts
= qemu_mallocz(sizeof(QEMUTimer
));
497 void qemu_free_timer(QEMUTimer
*ts
)
502 /* stop a timer, but do not dealloc it */
503 void qemu_del_timer(QEMUTimer
*ts
)
507 /* NOTE: this code must be signal safe because
508 qemu_timer_expired() can be called from a signal. */
509 pt
= &active_timers
[ts
->clock
->type
];
522 /* modify the current timer so that it will be fired when current_time
523 >= expire_time. The corresponding callback will be called. */
524 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
530 /* add the timer in the sorted list */
531 /* NOTE: this code must be signal safe because
532 qemu_timer_expired() can be called from a signal. */
533 pt
= &active_timers
[ts
->clock
->type
];
538 if (t
->expire_time
> expire_time
)
542 ts
->expire_time
= expire_time
;
546 /* Rearm if necessary */
547 if (pt
== &active_timers
[ts
->clock
->type
]) {
548 if (!alarm_timer
->pending
) {
549 qemu_rearm_alarm_timer(alarm_timer
);
551 /* Interrupt execution to force deadline recalculation. */
557 int qemu_timer_pending(QEMUTimer
*ts
)
560 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
567 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
571 return (timer_head
->expire_time
<= current_time
);
574 static void qemu_run_timers(QEMUClock
*clock
)
576 QEMUTimer
**ptimer_head
, *ts
;
577 int64_t current_time
;
582 current_time
= qemu_get_clock (clock
);
583 ptimer_head
= &active_timers
[clock
->type
];
586 if (!ts
|| ts
->expire_time
> current_time
)
588 /* remove timer from the list before calling the callback */
589 *ptimer_head
= ts
->next
;
592 /* run the callback (the timer list can be modified) */
597 int64_t qemu_get_clock(QEMUClock
*clock
)
599 switch(clock
->type
) {
600 case QEMU_CLOCK_REALTIME
:
601 return get_clock() / 1000000;
603 case QEMU_CLOCK_VIRTUAL
:
605 return cpu_get_icount();
607 return cpu_get_clock();
609 case QEMU_CLOCK_HOST
:
610 return get_clock_realtime();
614 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
616 switch(clock
->type
) {
617 case QEMU_CLOCK_REALTIME
:
620 case QEMU_CLOCK_VIRTUAL
:
622 return cpu_get_icount();
624 return cpu_get_clock();
626 case QEMU_CLOCK_HOST
:
627 return get_clock_realtime();
631 void init_clocks(void)
634 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
635 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
636 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
638 rtc_clock
= host_clock
;
642 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
644 uint64_t expire_time
;
646 if (qemu_timer_pending(ts
)) {
647 expire_time
= ts
->expire_time
;
651 qemu_put_be64(f
, expire_time
);
654 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
656 uint64_t expire_time
;
658 expire_time
= qemu_get_be64(f
);
659 if (expire_time
!= -1) {
660 qemu_mod_timer(ts
, expire_time
);
666 static const VMStateDescription vmstate_timers
= {
669 .minimum_version_id
= 1,
670 .minimum_version_id_old
= 1,
671 .fields
= (VMStateField
[]) {
672 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
673 VMSTATE_INT64(dummy
, TimersState
),
674 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
675 VMSTATE_END_OF_LIST()
679 void configure_icount(const char *option
)
681 vmstate_register(0, &vmstate_timers
, &timers_state
);
685 if (strcmp(option
, "auto") != 0) {
686 icount_time_shift
= strtol(option
, NULL
, 0);
693 /* 125MIPS seems a reasonable initial guess at the guest speed.
694 It will be corrected fairly quickly anyway. */
695 icount_time_shift
= 3;
697 /* Have both realtime and virtual time triggers for speed adjustment.
698 The realtime trigger catches emulated time passing too slowly,
699 the virtual time trigger catches emulated time passing too fast.
700 Realtime triggers occur even when idle, so use them less frequently
702 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
703 qemu_mod_timer(icount_rt_timer
,
704 qemu_get_clock(rt_clock
) + 1000);
705 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
706 qemu_mod_timer(icount_vm_timer
,
707 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
710 void qemu_run_all_timers(void)
712 alarm_timer
->pending
= 0;
714 /* rearm timer, if not periodic */
715 if (alarm_timer
->expired
) {
716 alarm_timer
->expired
= 0;
717 qemu_rearm_alarm_timer(alarm_timer
);
722 qemu_run_timers(vm_clock
);
725 qemu_run_timers(rt_clock
);
726 qemu_run_timers(host_clock
);
730 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
731 DWORD_PTR dwUser
, DWORD_PTR dw1
,
734 static void host_alarm_handler(int host_signum
)
737 struct qemu_alarm_timer
*t
= alarm_timer
;
742 #define DISP_FREQ 1000
744 static int64_t delta_min
= INT64_MAX
;
745 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
747 ti
= qemu_get_clock(vm_clock
);
748 if (last_clock
!= 0) {
749 delta
= ti
- last_clock
;
750 if (delta
< delta_min
)
752 if (delta
> delta_max
)
755 if (++count
== DISP_FREQ
) {
756 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
757 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
758 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
759 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
760 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
762 delta_min
= INT64_MAX
;
770 if (alarm_has_dynticks(t
) ||
772 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
773 qemu_get_clock(vm_clock
))) ||
774 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
775 qemu_get_clock(rt_clock
)) ||
776 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
777 qemu_get_clock(host_clock
))) {
779 t
->expired
= alarm_has_dynticks(t
);
785 int64_t qemu_next_deadline(void)
787 /* To avoid problems with overflow limit this to 2^32. */
788 int64_t delta
= INT32_MAX
;
790 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
791 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
792 qemu_get_clock(vm_clock
);
794 if (active_timers
[QEMU_CLOCK_HOST
]) {
795 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
796 qemu_get_clock(host_clock
);
809 #if defined(__linux__)
811 #define RTC_FREQ 1024
813 static uint64_t qemu_next_deadline_dyntick(void)
821 delta
= (qemu_next_deadline() + 999) / 1000;
823 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
824 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
825 qemu_get_clock(rt_clock
))*1000;
830 if (delta
< MIN_TIMER_REARM_US
)
831 delta
= MIN_TIMER_REARM_US
;
836 static void enable_sigio_timer(int fd
)
838 struct sigaction act
;
841 sigfillset(&act
.sa_mask
);
843 act
.sa_handler
= host_alarm_handler
;
845 sigaction(SIGIO
, &act
, NULL
);
846 fcntl_setfl(fd
, O_ASYNC
);
847 fcntl(fd
, F_SETOWN
, getpid());
850 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
852 struct hpet_info info
;
855 fd
= qemu_open("/dev/hpet", O_RDONLY
);
860 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
862 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
863 "error, but for better emulation accuracy type:\n"
864 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
868 /* Check capabilities */
869 r
= ioctl(fd
, HPET_INFO
, &info
);
873 /* Enable periodic mode */
874 r
= ioctl(fd
, HPET_EPI
, 0);
875 if (info
.hi_flags
&& (r
< 0))
878 /* Enable interrupt */
879 r
= ioctl(fd
, HPET_IE_ON
, 0);
883 enable_sigio_timer(fd
);
884 t
->priv
= (void *)(long)fd
;
892 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
894 int fd
= (long)t
->priv
;
899 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
902 unsigned long current_rtc_freq
= 0;
904 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
907 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
908 if (current_rtc_freq
!= RTC_FREQ
&&
909 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
910 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
911 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
912 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
915 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
921 enable_sigio_timer(rtc_fd
);
923 t
->priv
= (void *)(long)rtc_fd
;
928 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
930 int rtc_fd
= (long)t
->priv
;
935 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
939 struct sigaction act
;
941 sigfillset(&act
.sa_mask
);
943 act
.sa_handler
= host_alarm_handler
;
945 sigaction(SIGALRM
, &act
, NULL
);
948 * Initialize ev struct to 0 to avoid valgrind complaining
949 * about uninitialized data in timer_create call
951 memset(&ev
, 0, sizeof(ev
));
952 ev
.sigev_value
.sival_int
= 0;
953 ev
.sigev_notify
= SIGEV_SIGNAL
;
954 ev
.sigev_signo
= SIGALRM
;
956 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
957 perror("timer_create");
959 /* disable dynticks */
960 fprintf(stderr
, "Dynamic Ticks disabled\n");
965 t
->priv
= (void *)(long)host_timer
;
970 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
972 timer_t host_timer
= (timer_t
)(long)t
->priv
;
974 timer_delete(host_timer
);
977 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
979 timer_t host_timer
= (timer_t
)(long)t
->priv
;
980 struct itimerspec timeout
;
981 int64_t nearest_delta_us
= INT64_MAX
;
984 assert(alarm_has_dynticks(t
));
985 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
986 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
987 !active_timers
[QEMU_CLOCK_HOST
])
990 nearest_delta_us
= qemu_next_deadline_dyntick();
992 /* check whether a timer is already running */
993 if (timer_gettime(host_timer
, &timeout
)) {
995 fprintf(stderr
, "Internal timer error: aborting\n");
998 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
999 if (current_us
&& current_us
<= nearest_delta_us
)
1002 timeout
.it_interval
.tv_sec
= 0;
1003 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1004 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1005 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1006 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1008 fprintf(stderr
, "Internal timer error: aborting\n");
1013 #endif /* defined(__linux__) */
1015 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1017 struct sigaction act
;
1018 struct itimerval itv
;
1022 sigfillset(&act
.sa_mask
);
1024 act
.sa_handler
= host_alarm_handler
;
1026 sigaction(SIGALRM
, &act
, NULL
);
1028 itv
.it_interval
.tv_sec
= 0;
1029 /* for i386 kernel 2.6 to get 1 ms */
1030 itv
.it_interval
.tv_usec
= 999;
1031 itv
.it_value
.tv_sec
= 0;
1032 itv
.it_value
.tv_usec
= 10 * 1000;
1034 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1041 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1043 struct itimerval itv
;
1045 memset(&itv
, 0, sizeof(itv
));
1046 setitimer(ITIMER_REAL
, &itv
, NULL
);
1049 #endif /* !defined(_WIN32) */
1054 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1057 struct qemu_alarm_win32
*data
= t
->priv
;
1060 memset(&tc
, 0, sizeof(tc
));
1061 timeGetDevCaps(&tc
, sizeof(tc
));
1063 data
->period
= tc
.wPeriodMin
;
1064 timeBeginPeriod(data
->period
);
1066 flags
= TIME_CALLBACK_FUNCTION
;
1067 if (alarm_has_dynticks(t
))
1068 flags
|= TIME_ONESHOT
;
1070 flags
|= TIME_PERIODIC
;
1072 data
->timerId
= timeSetEvent(1, // interval (ms)
1073 data
->period
, // resolution
1074 host_alarm_handler
, // function
1075 (DWORD
)t
, // parameter
1078 if (!data
->timerId
) {
1079 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1081 timeEndPeriod(data
->period
);
1088 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1090 struct qemu_alarm_win32
*data
= t
->priv
;
1092 timeKillEvent(data
->timerId
);
1093 timeEndPeriod(data
->period
);
1096 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1098 struct qemu_alarm_win32
*data
= t
->priv
;
1100 assert(alarm_has_dynticks(t
));
1101 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1102 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1103 !active_timers
[QEMU_CLOCK_HOST
])
1106 timeKillEvent(data
->timerId
);
1108 data
->timerId
= timeSetEvent(1,
1112 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1114 if (!data
->timerId
) {
1115 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1118 timeEndPeriod(data
->period
);
1125 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1128 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1131 int init_timer_alarm(void)
1133 struct qemu_alarm_timer
*t
= NULL
;
1136 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1137 t
= &alarm_timers
[i
];
1149 /* first event is at time 0 */
1152 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1160 void quit_timers(void)
1162 struct qemu_alarm_timer
*t
= alarm_timer
;
1167 int qemu_calculate_timeout(void)
1169 #ifndef CONFIG_IOTHREAD
1175 /* XXX: use timeout computed from timers */
1178 /* Advance virtual time to the next event. */
1179 delta
= qemu_icount_delta();
1181 /* If virtual time is ahead of real time then just
1183 timeout
= (delta
+ 999999) / 1000000;
1185 /* Wait for either IO to occur or the next
1187 add
= qemu_next_deadline();
1188 /* We advance the timer before checking for IO.
1189 Limit the amount we advance so that early IO
1190 activity won't get the guest too far ahead. */
1194 qemu_icount
+= qemu_icount_round (add
);
1195 timeout
= delta
/ 1000000;
1202 #else /* CONFIG_IOTHREAD */