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 static int64_t qemu_icount_delta(void)
116 return 5000 * (int64_t) 1000000;
117 } else if (use_icount
== 1) {
118 /* When not using an adaptive execution frequency
119 we tend to get badly out of sync with real time,
120 so just delay for a reasonable amount of time. */
123 return cpu_get_icount() - cpu_get_clock();
127 /* enable cpu_get_ticks() */
128 void cpu_enable_ticks(void)
130 if (!timers_state
.cpu_ticks_enabled
) {
131 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
132 timers_state
.cpu_clock_offset
-= get_clock();
133 timers_state
.cpu_ticks_enabled
= 1;
137 /* disable cpu_get_ticks() : the clock is stopped. You must not call
138 cpu_get_ticks() after that. */
139 void cpu_disable_ticks(void)
141 if (timers_state
.cpu_ticks_enabled
) {
142 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
143 timers_state
.cpu_clock_offset
= cpu_get_clock();
144 timers_state
.cpu_ticks_enabled
= 0;
148 /***********************************************************/
151 #define QEMU_CLOCK_REALTIME 0
152 #define QEMU_CLOCK_VIRTUAL 1
153 #define QEMU_CLOCK_HOST 2
158 /* XXX: add frequency */
166 struct QEMUTimer
*next
;
169 struct qemu_alarm_timer
{
171 int (*start
)(struct qemu_alarm_timer
*t
);
172 void (*stop
)(struct qemu_alarm_timer
*t
);
173 void (*rearm
)(struct qemu_alarm_timer
*t
);
180 static struct qemu_alarm_timer
*alarm_timer
;
182 int qemu_alarm_pending(void)
184 return alarm_timer
->pending
;
187 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
192 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
194 if (!alarm_has_dynticks(t
))
200 /* TODO: MIN_TIMER_REARM_US should be optimized */
201 #define MIN_TIMER_REARM_US 250
205 struct qemu_alarm_win32
{
208 } alarm_win32_data
= {0, 0};
210 static int win32_start_timer(struct qemu_alarm_timer
*t
);
211 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
212 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
216 static int unix_start_timer(struct qemu_alarm_timer
*t
);
217 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
221 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
222 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
223 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
225 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
226 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
228 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
229 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
231 #endif /* __linux__ */
235 /* Correlation between real and virtual time is always going to be
236 fairly approximate, so ignore small variation.
237 When the guest is idle real and virtual time will be aligned in
239 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
241 static void icount_adjust(void)
246 static int64_t last_delta
;
247 /* If the VM is not running, then do nothing. */
251 cur_time
= cpu_get_clock();
252 cur_icount
= qemu_get_clock(vm_clock
);
253 delta
= cur_icount
- cur_time
;
254 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
256 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
257 && icount_time_shift
> 0) {
258 /* The guest is getting too far ahead. Slow time down. */
262 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
263 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
264 /* The guest is getting too far behind. Speed time up. */
268 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
271 static void icount_adjust_rt(void * opaque
)
273 qemu_mod_timer(icount_rt_timer
,
274 qemu_get_clock(rt_clock
) + 1000);
278 static void icount_adjust_vm(void * opaque
)
280 qemu_mod_timer(icount_vm_timer
,
281 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
285 int64_t qemu_icount_round(int64_t count
)
287 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
290 static struct qemu_alarm_timer alarm_timers
[] = {
293 {"dynticks", dynticks_start_timer
,
294 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
295 /* HPET - if available - is preferred */
296 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
297 /* ...otherwise try RTC */
298 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
300 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
302 {"dynticks", win32_start_timer
,
303 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
304 {"win32", win32_start_timer
,
305 win32_stop_timer
, NULL
, &alarm_win32_data
},
310 static void show_available_alarms(void)
314 printf("Available alarm timers, in order of precedence:\n");
315 for (i
= 0; alarm_timers
[i
].name
; i
++)
316 printf("%s\n", alarm_timers
[i
].name
);
319 void configure_alarms(char const *opt
)
323 int count
= ARRAY_SIZE(alarm_timers
) - 1;
326 struct qemu_alarm_timer tmp
;
328 if (!strcmp(opt
, "?")) {
329 show_available_alarms();
333 arg
= qemu_strdup(opt
);
335 /* Reorder the array */
336 name
= strtok(arg
, ",");
338 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
339 if (!strcmp(alarm_timers
[i
].name
, name
))
344 fprintf(stderr
, "Unknown clock %s\n", name
);
353 tmp
= alarm_timers
[i
];
354 alarm_timers
[i
] = alarm_timers
[cur
];
355 alarm_timers
[cur
] = tmp
;
359 name
= strtok(NULL
, ",");
365 /* Disable remaining timers */
366 for (i
= cur
; i
< count
; i
++)
367 alarm_timers
[i
].name
= NULL
;
369 show_available_alarms();
374 #define QEMU_NUM_CLOCKS 3
378 QEMUClock
*host_clock
;
380 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
382 static QEMUClock
*qemu_new_clock(int type
)
385 clock
= qemu_mallocz(sizeof(QEMUClock
));
391 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
393 clock
->enabled
= enabled
;
396 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
400 ts
= qemu_mallocz(sizeof(QEMUTimer
));
407 void qemu_free_timer(QEMUTimer
*ts
)
412 /* stop a timer, but do not dealloc it */
413 void qemu_del_timer(QEMUTimer
*ts
)
417 /* NOTE: this code must be signal safe because
418 qemu_timer_expired() can be called from a signal. */
419 pt
= &active_timers
[ts
->clock
->type
];
432 /* modify the current timer so that it will be fired when current_time
433 >= expire_time. The corresponding callback will be called. */
434 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
440 /* add the timer in the sorted list */
441 /* NOTE: this code must be signal safe because
442 qemu_timer_expired() can be called from a signal. */
443 pt
= &active_timers
[ts
->clock
->type
];
448 if (t
->expire_time
> expire_time
)
452 ts
->expire_time
= expire_time
;
456 /* Rearm if necessary */
457 if (pt
== &active_timers
[ts
->clock
->type
]) {
458 if (!alarm_timer
->pending
) {
459 qemu_rearm_alarm_timer(alarm_timer
);
461 /* Interrupt execution to force deadline recalculation. */
467 int qemu_timer_pending(QEMUTimer
*ts
)
470 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
477 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
481 return (timer_head
->expire_time
<= current_time
);
484 static void qemu_run_timers(QEMUClock
*clock
)
486 QEMUTimer
**ptimer_head
, *ts
;
487 int64_t current_time
;
492 current_time
= qemu_get_clock (clock
);
493 ptimer_head
= &active_timers
[clock
->type
];
496 if (!ts
|| ts
->expire_time
> current_time
)
498 /* remove timer from the list before calling the callback */
499 *ptimer_head
= ts
->next
;
502 /* run the callback (the timer list can be modified) */
507 int64_t qemu_get_clock(QEMUClock
*clock
)
509 switch(clock
->type
) {
510 case QEMU_CLOCK_REALTIME
:
511 return get_clock() / 1000000;
513 case QEMU_CLOCK_VIRTUAL
:
515 return cpu_get_icount();
517 return cpu_get_clock();
519 case QEMU_CLOCK_HOST
:
520 return get_clock_realtime();
524 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
526 switch(clock
->type
) {
527 case QEMU_CLOCK_REALTIME
:
530 case QEMU_CLOCK_VIRTUAL
:
532 return cpu_get_icount();
534 return cpu_get_clock();
536 case QEMU_CLOCK_HOST
:
537 return get_clock_realtime();
541 void init_clocks(void)
543 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
544 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
545 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
547 rtc_clock
= host_clock
;
551 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
553 uint64_t expire_time
;
555 if (qemu_timer_pending(ts
)) {
556 expire_time
= ts
->expire_time
;
560 qemu_put_be64(f
, expire_time
);
563 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
565 uint64_t expire_time
;
567 expire_time
= qemu_get_be64(f
);
568 if (expire_time
!= -1) {
569 qemu_mod_timer(ts
, expire_time
);
575 static const VMStateDescription vmstate_timers
= {
578 .minimum_version_id
= 1,
579 .minimum_version_id_old
= 1,
580 .fields
= (VMStateField
[]) {
581 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
582 VMSTATE_INT64(dummy
, TimersState
),
583 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
584 VMSTATE_END_OF_LIST()
588 void configure_icount(const char *option
)
590 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
594 if (strcmp(option
, "auto") != 0) {
595 icount_time_shift
= strtol(option
, NULL
, 0);
602 /* 125MIPS seems a reasonable initial guess at the guest speed.
603 It will be corrected fairly quickly anyway. */
604 icount_time_shift
= 3;
606 /* Have both realtime and virtual time triggers for speed adjustment.
607 The realtime trigger catches emulated time passing too slowly,
608 the virtual time trigger catches emulated time passing too fast.
609 Realtime triggers occur even when idle, so use them less frequently
611 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
612 qemu_mod_timer(icount_rt_timer
,
613 qemu_get_clock(rt_clock
) + 1000);
614 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
615 qemu_mod_timer(icount_vm_timer
,
616 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
619 void qemu_run_all_timers(void)
621 alarm_timer
->pending
= 0;
623 /* rearm timer, if not periodic */
624 if (alarm_timer
->expired
) {
625 alarm_timer
->expired
= 0;
626 qemu_rearm_alarm_timer(alarm_timer
);
631 qemu_run_timers(vm_clock
);
634 qemu_run_timers(rt_clock
);
635 qemu_run_timers(host_clock
);
639 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
640 DWORD_PTR dwUser
, DWORD_PTR dw1
,
643 static void host_alarm_handler(int host_signum
)
646 struct qemu_alarm_timer
*t
= alarm_timer
;
651 #define DISP_FREQ 1000
653 static int64_t delta_min
= INT64_MAX
;
654 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
656 ti
= qemu_get_clock(vm_clock
);
657 if (last_clock
!= 0) {
658 delta
= ti
- last_clock
;
659 if (delta
< delta_min
)
661 if (delta
> delta_max
)
664 if (++count
== DISP_FREQ
) {
665 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
666 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
667 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
668 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
669 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
671 delta_min
= INT64_MAX
;
679 if (alarm_has_dynticks(t
) ||
681 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
682 qemu_get_clock(vm_clock
))) ||
683 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
684 qemu_get_clock(rt_clock
)) ||
685 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
686 qemu_get_clock(host_clock
))) {
688 t
->expired
= alarm_has_dynticks(t
);
694 int64_t qemu_next_deadline(void)
696 /* To avoid problems with overflow limit this to 2^32. */
697 int64_t delta
= INT32_MAX
;
699 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
700 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
701 qemu_get_clock(vm_clock
);
703 if (active_timers
[QEMU_CLOCK_HOST
]) {
704 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
705 qemu_get_clock(host_clock
);
718 #if defined(__linux__)
720 #define RTC_FREQ 1024
722 static uint64_t qemu_next_deadline_dyntick(void)
730 delta
= (qemu_next_deadline() + 999) / 1000;
732 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
733 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
734 qemu_get_clock(rt_clock
))*1000;
739 if (delta
< MIN_TIMER_REARM_US
)
740 delta
= MIN_TIMER_REARM_US
;
745 static void enable_sigio_timer(int fd
)
747 struct sigaction act
;
750 sigfillset(&act
.sa_mask
);
752 act
.sa_handler
= host_alarm_handler
;
754 sigaction(SIGIO
, &act
, NULL
);
755 fcntl_setfl(fd
, O_ASYNC
);
756 fcntl(fd
, F_SETOWN
, getpid());
759 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
761 struct hpet_info info
;
764 fd
= qemu_open("/dev/hpet", O_RDONLY
);
769 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
771 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
772 "error, but for better emulation accuracy type:\n"
773 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
777 /* Check capabilities */
778 r
= ioctl(fd
, HPET_INFO
, &info
);
782 /* Enable periodic mode */
783 r
= ioctl(fd
, HPET_EPI
, 0);
784 if (info
.hi_flags
&& (r
< 0))
787 /* Enable interrupt */
788 r
= ioctl(fd
, HPET_IE_ON
, 0);
792 enable_sigio_timer(fd
);
793 t
->priv
= (void *)(long)fd
;
801 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
803 int fd
= (long)t
->priv
;
808 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
811 unsigned long current_rtc_freq
= 0;
813 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
816 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
817 if (current_rtc_freq
!= RTC_FREQ
&&
818 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
819 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
820 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
821 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
824 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
830 enable_sigio_timer(rtc_fd
);
832 t
->priv
= (void *)(long)rtc_fd
;
837 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
839 int rtc_fd
= (long)t
->priv
;
844 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
848 struct sigaction act
;
850 sigfillset(&act
.sa_mask
);
852 act
.sa_handler
= host_alarm_handler
;
854 sigaction(SIGALRM
, &act
, NULL
);
857 * Initialize ev struct to 0 to avoid valgrind complaining
858 * about uninitialized data in timer_create call
860 memset(&ev
, 0, sizeof(ev
));
861 ev
.sigev_value
.sival_int
= 0;
862 ev
.sigev_notify
= SIGEV_SIGNAL
;
863 ev
.sigev_signo
= SIGALRM
;
865 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
866 perror("timer_create");
868 /* disable dynticks */
869 fprintf(stderr
, "Dynamic Ticks disabled\n");
874 t
->priv
= (void *)(long)host_timer
;
879 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
881 timer_t host_timer
= (timer_t
)(long)t
->priv
;
883 timer_delete(host_timer
);
886 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
888 timer_t host_timer
= (timer_t
)(long)t
->priv
;
889 struct itimerspec timeout
;
890 int64_t nearest_delta_us
= INT64_MAX
;
893 assert(alarm_has_dynticks(t
));
894 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
895 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
896 !active_timers
[QEMU_CLOCK_HOST
])
899 nearest_delta_us
= qemu_next_deadline_dyntick();
901 /* check whether a timer is already running */
902 if (timer_gettime(host_timer
, &timeout
)) {
904 fprintf(stderr
, "Internal timer error: aborting\n");
907 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
908 if (current_us
&& current_us
<= nearest_delta_us
)
911 timeout
.it_interval
.tv_sec
= 0;
912 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
913 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
914 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
915 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
917 fprintf(stderr
, "Internal timer error: aborting\n");
922 #endif /* defined(__linux__) */
924 static int unix_start_timer(struct qemu_alarm_timer
*t
)
926 struct sigaction act
;
927 struct itimerval itv
;
931 sigfillset(&act
.sa_mask
);
933 act
.sa_handler
= host_alarm_handler
;
935 sigaction(SIGALRM
, &act
, NULL
);
937 itv
.it_interval
.tv_sec
= 0;
938 /* for i386 kernel 2.6 to get 1 ms */
939 itv
.it_interval
.tv_usec
= 999;
940 itv
.it_value
.tv_sec
= 0;
941 itv
.it_value
.tv_usec
= 10 * 1000;
943 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
950 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
952 struct itimerval itv
;
954 memset(&itv
, 0, sizeof(itv
));
955 setitimer(ITIMER_REAL
, &itv
, NULL
);
958 #endif /* !defined(_WIN32) */
963 static int win32_start_timer(struct qemu_alarm_timer
*t
)
966 struct qemu_alarm_win32
*data
= t
->priv
;
969 memset(&tc
, 0, sizeof(tc
));
970 timeGetDevCaps(&tc
, sizeof(tc
));
972 data
->period
= tc
.wPeriodMin
;
973 timeBeginPeriod(data
->period
);
975 flags
= TIME_CALLBACK_FUNCTION
;
976 if (alarm_has_dynticks(t
))
977 flags
|= TIME_ONESHOT
;
979 flags
|= TIME_PERIODIC
;
981 data
->timerId
= timeSetEvent(1, // interval (ms)
982 data
->period
, // resolution
983 host_alarm_handler
, // function
984 (DWORD
)t
, // parameter
987 if (!data
->timerId
) {
988 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
990 timeEndPeriod(data
->period
);
997 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
999 struct qemu_alarm_win32
*data
= t
->priv
;
1001 timeKillEvent(data
->timerId
);
1002 timeEndPeriod(data
->period
);
1005 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1007 struct qemu_alarm_win32
*data
= t
->priv
;
1009 assert(alarm_has_dynticks(t
));
1010 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1011 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1012 !active_timers
[QEMU_CLOCK_HOST
])
1015 timeKillEvent(data
->timerId
);
1017 data
->timerId
= timeSetEvent(1,
1021 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1023 if (!data
->timerId
) {
1024 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1027 timeEndPeriod(data
->period
);
1034 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1037 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1040 int init_timer_alarm(void)
1042 struct qemu_alarm_timer
*t
= NULL
;
1045 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1046 t
= &alarm_timers
[i
];
1058 /* first event is at time 0 */
1061 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1069 void quit_timers(void)
1071 struct qemu_alarm_timer
*t
= alarm_timer
;
1076 int qemu_calculate_timeout(void)
1081 #if defined (CONFIG_IOTHREAD) || defined (QEMUKVM)
1082 /* When using icount, making forward progress with qemu_icount when the
1083 guest CPU is idle is critical. We only use the static io-thread timeout
1084 for non icount runs. */
1093 /* XXX: use timeout computed from timers */
1096 /* Advance virtual time to the next event. */
1097 delta
= qemu_icount_delta();
1099 /* If virtual time is ahead of real time then just
1101 timeout
= (delta
+ 999999) / 1000000;
1103 /* Wait for either IO to occur or the next
1105 add
= qemu_next_deadline();
1106 /* We advance the timer before checking for IO.
1107 Limit the amount we advance so that early IO
1108 activity won't get the guest too far ahead. */
1112 qemu_icount
+= qemu_icount_round (add
);
1113 timeout
= delta
/ 1000000;