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
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> */
54 #include "qemu-timer.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)
75 gettimeofday(&tv
, NULL
);
76 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
81 static int64_t clock_freq
;
83 static void init_get_clock(void)
87 ret
= QueryPerformanceFrequency(&freq
);
89 fprintf(stderr
, "Could not calibrate ticks\n");
92 clock_freq
= freq
.QuadPart
;
95 static int64_t get_clock(void)
98 QueryPerformanceCounter(&ti
);
99 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
104 static int use_rt_clock
;
106 static void init_get_clock(void)
109 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
110 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
113 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
120 static int64_t get_clock(void)
122 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
123 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
126 clock_gettime(CLOCK_MONOTONIC
, &ts
);
127 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
131 /* XXX: using gettimeofday leads to problems if the date
132 changes, so it should be avoided. */
133 return get_clock_realtime();
138 /* Return the virtual CPU time, based on the instruction counter. */
139 static int64_t cpu_get_icount(void)
142 CPUState
*env
= cpu_single_env
;;
143 icount
= qemu_icount
;
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
;
163 TimersState timers_state
;
165 /* return the host CPU cycle counter and handle stop/restart */
166 int64_t cpu_get_ticks(void)
169 return cpu_get_icount();
171 if (!timers_state
.cpu_ticks_enabled
) {
172 return timers_state
.cpu_ticks_offset
;
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
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)
190 if (!timers_state
.cpu_ticks_enabled
) {
191 return timers_state
.cpu_clock_offset
;
194 return ti
+ timers_state
.cpu_clock_offset
;
198 #ifndef CONFIG_IOTHREAD
199 static int64_t qemu_icount_delta(void)
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. */
209 return cpu_get_icount() - cpu_get_clock();
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 /***********************************************************/
238 #define QEMU_CLOCK_REALTIME 0
239 #define QEMU_CLOCK_VIRTUAL 1
240 #define QEMU_CLOCK_HOST 2
245 /* XXX: add frequency */
253 struct QEMUTimer
*next
;
256 struct qemu_alarm_timer
{
258 int (*start
)(struct qemu_alarm_timer
*t
);
259 void (*stop
)(struct qemu_alarm_timer
*t
);
260 void (*rearm
)(struct qemu_alarm_timer
*t
);
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
)
279 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
281 if (!alarm_has_dynticks(t
))
287 /* TODO: MIN_TIMER_REARM_US should be optimized */
288 #define MIN_TIMER_REARM_US 250
292 struct qemu_alarm_win32
{
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
);
303 static int unix_start_timer(struct qemu_alarm_timer
*t
);
304 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
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__ */
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
326 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
328 static void icount_adjust(void)
333 static int64_t last_delta
;
334 /* If the VM is not running, then do nothing. */
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. */
343 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
344 && icount_time_shift
> 0) {
345 /* The guest is getting too far ahead. Slow time down. */
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. */
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);
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);
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
[] = {
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
},
387 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
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
},
397 static void show_available_alarms(void)
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
)
410 int count
= ARRAY_SIZE(alarm_timers
) - 1;
413 struct qemu_alarm_timer tmp
;
415 if (!strcmp(opt
, "?")) {
416 show_available_alarms();
420 arg
= qemu_strdup(opt
);
422 /* Reorder the array */
423 name
= strtok(arg
, ",");
425 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
426 if (!strcmp(alarm_timers
[i
].name
, name
))
431 fprintf(stderr
, "Unknown clock %s\n", name
);
440 tmp
= alarm_timers
[i
];
441 alarm_timers
[i
] = alarm_timers
[cur
];
442 alarm_timers
[cur
] = tmp
;
446 name
= strtok(NULL
, ",");
452 /* Disable remaining timers */
453 for (i
= cur
; i
< count
; i
++)
454 alarm_timers
[i
].name
= NULL
;
456 show_available_alarms();
461 #define QEMU_NUM_CLOCKS 3
465 QEMUClock
*host_clock
;
467 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
469 static QEMUClock
*qemu_new_clock(int type
)
472 clock
= qemu_mallocz(sizeof(QEMUClock
));
478 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
480 clock
->enabled
= enabled
;
483 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
487 ts
= qemu_mallocz(sizeof(QEMUTimer
));
494 void qemu_free_timer(QEMUTimer
*ts
)
499 /* stop a timer, but do not dealloc it */
500 void qemu_del_timer(QEMUTimer
*ts
)
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
];
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
)
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
];
535 if (t
->expire_time
> expire_time
)
539 ts
->expire_time
= expire_time
;
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. */
554 int qemu_timer_pending(QEMUTimer
*ts
)
557 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
564 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
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
;
579 current_time
= qemu_get_clock (clock
);
580 ptimer_head
= &active_timers
[clock
->type
];
583 if (!ts
|| ts
->expire_time
> current_time
)
585 /* remove timer from the list before calling the callback */
586 *ptimer_head
= ts
->next
;
589 /* run the callback (the timer list can be modified) */
594 int64_t qemu_get_clock(QEMUClock
*clock
)
596 switch(clock
->type
) {
597 case QEMU_CLOCK_REALTIME
:
598 return get_clock() / 1000000;
600 case QEMU_CLOCK_VIRTUAL
:
602 return cpu_get_icount();
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
:
617 case QEMU_CLOCK_VIRTUAL
:
619 return cpu_get_icount();
621 return cpu_get_clock();
623 case QEMU_CLOCK_HOST
:
624 return get_clock_realtime();
628 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(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(0, &vmstate_timers
, &timers_state
);
682 if (strcmp(option
, "auto") != 0) {
683 icount_time_shift
= strtol(option
, NULL
, 0);
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
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;
719 qemu_run_timers(vm_clock
);
722 qemu_run_timers(rt_clock
);
723 qemu_run_timers(host_clock
);
727 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
728 DWORD_PTR dwUser
, DWORD_PTR dw1
,
731 static void host_alarm_handler(int host_signum
)
734 struct qemu_alarm_timer
*t
= alarm_timer
;
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
;
744 ti
= qemu_get_clock(vm_clock
);
745 if (last_clock
!= 0) {
746 delta
= ti
- last_clock
;
747 if (delta
< delta_min
)
749 if (delta
> delta_max
)
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
));
759 delta_min
= INT64_MAX
;
767 if (alarm_has_dynticks(t
) ||
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
);
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
);
806 #if defined(__linux__)
808 #define RTC_FREQ 1024
810 static uint64_t qemu_next_deadline_dyntick(void)
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;
827 if (delta
< MIN_TIMER_REARM_US
)
828 delta
= MIN_TIMER_REARM_US
;
833 static void enable_sigio_timer(int fd
)
835 struct sigaction act
;
838 sigfillset(&act
.sa_mask
);
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
;
852 fd
= qemu_open("/dev/hpet", O_RDONLY
);
857 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
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");
865 /* Check capabilities */
866 r
= ioctl(fd
, HPET_INFO
, &info
);
870 /* Enable periodic mode */
871 r
= ioctl(fd
, HPET_EPI
, 0);
872 if (info
.hi_flags
&& (r
< 0))
875 /* Enable interrupt */
876 r
= ioctl(fd
, HPET_IE_ON
, 0);
880 enable_sigio_timer(fd
);
881 t
->priv
= (void *)(long)fd
;
889 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
891 int fd
= (long)t
->priv
;
896 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
899 unsigned long current_rtc_freq
= 0;
901 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
904 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_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");
912 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
918 enable_sigio_timer(rtc_fd
);
920 t
->priv
= (void *)(long)rtc_fd
;
925 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
927 int rtc_fd
= (long)t
->priv
;
932 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
936 struct sigaction act
;
938 sigfillset(&act
.sa_mask
);
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");
962 t
->priv
= (void *)(long)host_timer
;
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
;
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
])
987 nearest_delta_us
= qemu_next_deadline_dyntick();
989 /* check whether a timer is already running */
990 if (timer_gettime(host_timer
, &timeout
)) {
992 fprintf(stderr
, "Internal timer error: aborting\n");
995 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
996 if (current_us
&& current_us
<= nearest_delta_us
)
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
)) {
1005 fprintf(stderr
, "Internal timer error: aborting\n");
1010 #endif /* defined(__linux__) */
1012 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1014 struct sigaction act
;
1015 struct itimerval itv
;
1019 sigfillset(&act
.sa_mask
);
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
);
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) */
1051 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1054 struct qemu_alarm_win32
*data
= t
->priv
;
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
;
1067 flags
|= TIME_PERIODIC
;
1069 data
->timerId
= timeSetEvent(1, // interval (ms)
1070 data
->period
, // resolution
1071 host_alarm_handler
, // function
1072 (DWORD
)t
, // parameter
1075 if (!data
->timerId
) {
1076 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1078 timeEndPeriod(data
->period
);
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
])
1103 timeKillEvent(data
->timerId
);
1105 data
->timerId
= timeSetEvent(1,
1109 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1111 if (!data
->timerId
) {
1112 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1115 timeEndPeriod(data
->period
);
1122 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1125 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1128 int init_timer_alarm(void)
1130 struct qemu_alarm_timer
*t
= NULL
;
1133 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1134 t
= &alarm_timers
[i
];
1146 /* first event is at time 0 */
1149 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1157 void quit_timers(void)
1159 struct qemu_alarm_timer
*t
= alarm_timer
;
1164 int qemu_calculate_timeout(void)
1166 #ifndef CONFIG_IOTHREAD
1172 /* XXX: use timeout computed from timers */
1175 /* Advance virtual time to the next event. */
1176 delta
= qemu_icount_delta();
1178 /* If virtual time is ahead of real time then just
1180 timeout
= (delta
+ 999999) / 1000000;
1182 /* Wait for either IO to occur or the next
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. */
1191 qemu_icount
+= qemu_icount_round (add
);
1192 timeout
= delta
/ 1000000;
1199 #else /* CONFIG_IOTHREAD */