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 #ifndef CONFIG_IOTHREAD
186 static int64_t qemu_icount_delta(void)
189 return 5000 * (int64_t) 1000000;
190 } else if (use_icount
== 1) {
191 /* When not using an adaptive execution frequency
192 we tend to get badly out of sync with real time,
193 so just delay for a reasonable amount of time. */
196 return cpu_get_icount() - cpu_get_clock();
201 /* enable cpu_get_ticks() */
202 void cpu_enable_ticks(void)
204 if (!timers_state
.cpu_ticks_enabled
) {
205 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
206 timers_state
.cpu_clock_offset
-= get_clock();
207 timers_state
.cpu_ticks_enabled
= 1;
211 /* disable cpu_get_ticks() : the clock is stopped. You must not call
212 cpu_get_ticks() after that. */
213 void cpu_disable_ticks(void)
215 if (timers_state
.cpu_ticks_enabled
) {
216 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
217 timers_state
.cpu_clock_offset
= cpu_get_clock();
218 timers_state
.cpu_ticks_enabled
= 0;
222 /***********************************************************/
225 #define QEMU_CLOCK_REALTIME 0
226 #define QEMU_CLOCK_VIRTUAL 1
227 #define QEMU_CLOCK_HOST 2
232 /* XXX: add frequency */
240 struct QEMUTimer
*next
;
243 struct qemu_alarm_timer
{
245 int (*start
)(struct qemu_alarm_timer
*t
);
246 void (*stop
)(struct qemu_alarm_timer
*t
);
247 void (*rearm
)(struct qemu_alarm_timer
*t
);
254 static struct qemu_alarm_timer
*alarm_timer
;
256 int qemu_alarm_pending(void)
258 return alarm_timer
->pending
;
261 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
266 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
268 if (!alarm_has_dynticks(t
))
274 /* TODO: MIN_TIMER_REARM_US should be optimized */
275 #define MIN_TIMER_REARM_US 250
279 struct qemu_alarm_win32
{
282 } alarm_win32_data
= {0, 0};
284 static int win32_start_timer(struct qemu_alarm_timer
*t
);
285 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
286 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
290 static int unix_start_timer(struct qemu_alarm_timer
*t
);
291 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
295 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
296 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
297 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
299 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
300 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
302 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
303 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
305 #endif /* __linux__ */
309 /* Correlation between real and virtual time is always going to be
310 fairly approximate, so ignore small variation.
311 When the guest is idle real and virtual time will be aligned in
313 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
315 static void icount_adjust(void)
320 static int64_t last_delta
;
321 /* If the VM is not running, then do nothing. */
325 cur_time
= cpu_get_clock();
326 cur_icount
= qemu_get_clock(vm_clock
);
327 delta
= cur_icount
- cur_time
;
328 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
330 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
331 && icount_time_shift
> 0) {
332 /* The guest is getting too far ahead. Slow time down. */
336 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
337 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
338 /* The guest is getting too far behind. Speed time up. */
342 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
345 static void icount_adjust_rt(void * opaque
)
347 qemu_mod_timer(icount_rt_timer
,
348 qemu_get_clock(rt_clock
) + 1000);
352 static void icount_adjust_vm(void * opaque
)
354 qemu_mod_timer(icount_vm_timer
,
355 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
359 int64_t qemu_icount_round(int64_t count
)
361 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
364 static struct qemu_alarm_timer alarm_timers
[] = {
367 {"dynticks", dynticks_start_timer
,
368 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
369 /* HPET - if available - is preferred */
370 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
371 /* ...otherwise try RTC */
372 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
374 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
376 {"dynticks", win32_start_timer
,
377 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
378 {"win32", win32_start_timer
,
379 win32_stop_timer
, NULL
, &alarm_win32_data
},
384 static void show_available_alarms(void)
388 printf("Available alarm timers, in order of precedence:\n");
389 for (i
= 0; alarm_timers
[i
].name
; i
++)
390 printf("%s\n", alarm_timers
[i
].name
);
393 void configure_alarms(char const *opt
)
397 int count
= ARRAY_SIZE(alarm_timers
) - 1;
400 struct qemu_alarm_timer tmp
;
402 if (!strcmp(opt
, "?")) {
403 show_available_alarms();
407 arg
= qemu_strdup(opt
);
409 /* Reorder the array */
410 name
= strtok(arg
, ",");
412 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
413 if (!strcmp(alarm_timers
[i
].name
, name
))
418 fprintf(stderr
, "Unknown clock %s\n", name
);
427 tmp
= alarm_timers
[i
];
428 alarm_timers
[i
] = alarm_timers
[cur
];
429 alarm_timers
[cur
] = tmp
;
433 name
= strtok(NULL
, ",");
439 /* Disable remaining timers */
440 for (i
= cur
; i
< count
; i
++)
441 alarm_timers
[i
].name
= NULL
;
443 show_available_alarms();
448 #define QEMU_NUM_CLOCKS 3
452 QEMUClock
*host_clock
;
454 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
456 static QEMUClock
*qemu_new_clock(int type
)
459 clock
= qemu_mallocz(sizeof(QEMUClock
));
465 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
467 clock
->enabled
= enabled
;
470 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
474 ts
= qemu_mallocz(sizeof(QEMUTimer
));
481 void qemu_free_timer(QEMUTimer
*ts
)
486 /* stop a timer, but do not dealloc it */
487 void qemu_del_timer(QEMUTimer
*ts
)
491 /* NOTE: this code must be signal safe because
492 qemu_timer_expired() can be called from a signal. */
493 pt
= &active_timers
[ts
->clock
->type
];
506 /* modify the current timer so that it will be fired when current_time
507 >= expire_time. The corresponding callback will be called. */
508 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
514 /* add the timer in the sorted list */
515 /* NOTE: this code must be signal safe because
516 qemu_timer_expired() can be called from a signal. */
517 pt
= &active_timers
[ts
->clock
->type
];
522 if (t
->expire_time
> expire_time
)
526 ts
->expire_time
= expire_time
;
530 /* Rearm if necessary */
531 if (pt
== &active_timers
[ts
->clock
->type
]) {
532 if (!alarm_timer
->pending
) {
533 qemu_rearm_alarm_timer(alarm_timer
);
535 /* Interrupt execution to force deadline recalculation. */
541 int qemu_timer_pending(QEMUTimer
*ts
)
544 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
551 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
555 return (timer_head
->expire_time
<= current_time
);
558 static void qemu_run_timers(QEMUClock
*clock
)
560 QEMUTimer
**ptimer_head
, *ts
;
561 int64_t current_time
;
566 current_time
= qemu_get_clock (clock
);
567 ptimer_head
= &active_timers
[clock
->type
];
570 if (!ts
|| ts
->expire_time
> current_time
)
572 /* remove timer from the list before calling the callback */
573 *ptimer_head
= ts
->next
;
576 /* run the callback (the timer list can be modified) */
581 int64_t qemu_get_clock(QEMUClock
*clock
)
583 switch(clock
->type
) {
584 case QEMU_CLOCK_REALTIME
:
585 return get_clock() / 1000000;
587 case QEMU_CLOCK_VIRTUAL
:
589 return cpu_get_icount();
591 return cpu_get_clock();
593 case QEMU_CLOCK_HOST
:
594 return get_clock_realtime();
598 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
600 switch(clock
->type
) {
601 case QEMU_CLOCK_REALTIME
:
604 case QEMU_CLOCK_VIRTUAL
:
606 return cpu_get_icount();
608 return cpu_get_clock();
610 case QEMU_CLOCK_HOST
:
611 return get_clock_realtime();
615 void init_clocks(void)
618 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
619 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
620 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
622 rtc_clock
= host_clock
;
626 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
628 uint64_t expire_time
;
630 if (qemu_timer_pending(ts
)) {
631 expire_time
= ts
->expire_time
;
635 qemu_put_be64(f
, expire_time
);
638 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
640 uint64_t expire_time
;
642 expire_time
= qemu_get_be64(f
);
643 if (expire_time
!= -1) {
644 qemu_mod_timer(ts
, expire_time
);
650 static const VMStateDescription vmstate_timers
= {
653 .minimum_version_id
= 1,
654 .minimum_version_id_old
= 1,
655 .fields
= (VMStateField
[]) {
656 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
657 VMSTATE_INT64(dummy
, TimersState
),
658 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
659 VMSTATE_END_OF_LIST()
663 void configure_icount(const char *option
)
665 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
669 if (strcmp(option
, "auto") != 0) {
670 icount_time_shift
= strtol(option
, NULL
, 0);
677 /* 125MIPS seems a reasonable initial guess at the guest speed.
678 It will be corrected fairly quickly anyway. */
679 icount_time_shift
= 3;
681 /* Have both realtime and virtual time triggers for speed adjustment.
682 The realtime trigger catches emulated time passing too slowly,
683 the virtual time trigger catches emulated time passing too fast.
684 Realtime triggers occur even when idle, so use them less frequently
686 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
687 qemu_mod_timer(icount_rt_timer
,
688 qemu_get_clock(rt_clock
) + 1000);
689 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
690 qemu_mod_timer(icount_vm_timer
,
691 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
694 void qemu_run_all_timers(void)
696 alarm_timer
->pending
= 0;
698 /* rearm timer, if not periodic */
699 if (alarm_timer
->expired
) {
700 alarm_timer
->expired
= 0;
701 qemu_rearm_alarm_timer(alarm_timer
);
706 qemu_run_timers(vm_clock
);
709 qemu_run_timers(rt_clock
);
710 qemu_run_timers(host_clock
);
714 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
715 DWORD_PTR dwUser
, DWORD_PTR dw1
,
718 static void host_alarm_handler(int host_signum
)
721 struct qemu_alarm_timer
*t
= alarm_timer
;
726 #define DISP_FREQ 1000
728 static int64_t delta_min
= INT64_MAX
;
729 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
731 ti
= qemu_get_clock(vm_clock
);
732 if (last_clock
!= 0) {
733 delta
= ti
- last_clock
;
734 if (delta
< delta_min
)
736 if (delta
> delta_max
)
739 if (++count
== DISP_FREQ
) {
740 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
741 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
742 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
743 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
744 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
746 delta_min
= INT64_MAX
;
754 if (alarm_has_dynticks(t
) ||
756 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
757 qemu_get_clock(vm_clock
))) ||
758 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
759 qemu_get_clock(rt_clock
)) ||
760 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
761 qemu_get_clock(host_clock
))) {
763 t
->expired
= alarm_has_dynticks(t
);
769 int64_t qemu_next_deadline(void)
771 /* To avoid problems with overflow limit this to 2^32. */
772 int64_t delta
= INT32_MAX
;
774 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
775 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
776 qemu_get_clock(vm_clock
);
778 if (active_timers
[QEMU_CLOCK_HOST
]) {
779 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
780 qemu_get_clock(host_clock
);
793 #if defined(__linux__)
795 #define RTC_FREQ 1024
797 static uint64_t qemu_next_deadline_dyntick(void)
805 delta
= (qemu_next_deadline() + 999) / 1000;
807 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
808 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
809 qemu_get_clock(rt_clock
))*1000;
814 if (delta
< MIN_TIMER_REARM_US
)
815 delta
= MIN_TIMER_REARM_US
;
820 static void enable_sigio_timer(int fd
)
822 struct sigaction act
;
825 sigfillset(&act
.sa_mask
);
827 act
.sa_handler
= host_alarm_handler
;
829 sigaction(SIGIO
, &act
, NULL
);
830 fcntl_setfl(fd
, O_ASYNC
);
831 fcntl(fd
, F_SETOWN
, getpid());
834 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
836 struct hpet_info info
;
839 fd
= qemu_open("/dev/hpet", O_RDONLY
);
844 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
846 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
847 "error, but for better emulation accuracy type:\n"
848 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
852 /* Check capabilities */
853 r
= ioctl(fd
, HPET_INFO
, &info
);
857 /* Enable periodic mode */
858 r
= ioctl(fd
, HPET_EPI
, 0);
859 if (info
.hi_flags
&& (r
< 0))
862 /* Enable interrupt */
863 r
= ioctl(fd
, HPET_IE_ON
, 0);
867 enable_sigio_timer(fd
);
868 t
->priv
= (void *)(long)fd
;
876 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
878 int fd
= (long)t
->priv
;
883 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
886 unsigned long current_rtc_freq
= 0;
888 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
891 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
892 if (current_rtc_freq
!= RTC_FREQ
&&
893 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
894 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
895 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
896 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
899 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
905 enable_sigio_timer(rtc_fd
);
907 t
->priv
= (void *)(long)rtc_fd
;
912 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
914 int rtc_fd
= (long)t
->priv
;
919 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
923 struct sigaction act
;
925 sigfillset(&act
.sa_mask
);
927 act
.sa_handler
= host_alarm_handler
;
929 sigaction(SIGALRM
, &act
, NULL
);
932 * Initialize ev struct to 0 to avoid valgrind complaining
933 * about uninitialized data in timer_create call
935 memset(&ev
, 0, sizeof(ev
));
936 ev
.sigev_value
.sival_int
= 0;
937 ev
.sigev_notify
= SIGEV_SIGNAL
;
938 ev
.sigev_signo
= SIGALRM
;
940 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
941 perror("timer_create");
943 /* disable dynticks */
944 fprintf(stderr
, "Dynamic Ticks disabled\n");
949 t
->priv
= (void *)(long)host_timer
;
954 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
956 timer_t host_timer
= (timer_t
)(long)t
->priv
;
958 timer_delete(host_timer
);
961 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
963 timer_t host_timer
= (timer_t
)(long)t
->priv
;
964 struct itimerspec timeout
;
965 int64_t nearest_delta_us
= INT64_MAX
;
968 assert(alarm_has_dynticks(t
));
969 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
970 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
971 !active_timers
[QEMU_CLOCK_HOST
])
974 nearest_delta_us
= qemu_next_deadline_dyntick();
976 /* check whether a timer is already running */
977 if (timer_gettime(host_timer
, &timeout
)) {
979 fprintf(stderr
, "Internal timer error: aborting\n");
982 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
983 if (current_us
&& current_us
<= nearest_delta_us
)
986 timeout
.it_interval
.tv_sec
= 0;
987 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
988 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
989 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
990 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
992 fprintf(stderr
, "Internal timer error: aborting\n");
997 #endif /* defined(__linux__) */
999 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1001 struct sigaction act
;
1002 struct itimerval itv
;
1006 sigfillset(&act
.sa_mask
);
1008 act
.sa_handler
= host_alarm_handler
;
1010 sigaction(SIGALRM
, &act
, NULL
);
1012 itv
.it_interval
.tv_sec
= 0;
1013 /* for i386 kernel 2.6 to get 1 ms */
1014 itv
.it_interval
.tv_usec
= 999;
1015 itv
.it_value
.tv_sec
= 0;
1016 itv
.it_value
.tv_usec
= 10 * 1000;
1018 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1025 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1027 struct itimerval itv
;
1029 memset(&itv
, 0, sizeof(itv
));
1030 setitimer(ITIMER_REAL
, &itv
, NULL
);
1033 #endif /* !defined(_WIN32) */
1038 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1041 struct qemu_alarm_win32
*data
= t
->priv
;
1044 memset(&tc
, 0, sizeof(tc
));
1045 timeGetDevCaps(&tc
, sizeof(tc
));
1047 data
->period
= tc
.wPeriodMin
;
1048 timeBeginPeriod(data
->period
);
1050 flags
= TIME_CALLBACK_FUNCTION
;
1051 if (alarm_has_dynticks(t
))
1052 flags
|= TIME_ONESHOT
;
1054 flags
|= TIME_PERIODIC
;
1056 data
->timerId
= timeSetEvent(1, // interval (ms)
1057 data
->period
, // resolution
1058 host_alarm_handler
, // function
1059 (DWORD
)t
, // parameter
1062 if (!data
->timerId
) {
1063 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1065 timeEndPeriod(data
->period
);
1072 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1074 struct qemu_alarm_win32
*data
= t
->priv
;
1076 timeKillEvent(data
->timerId
);
1077 timeEndPeriod(data
->period
);
1080 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1082 struct qemu_alarm_win32
*data
= t
->priv
;
1084 assert(alarm_has_dynticks(t
));
1085 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1086 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1087 !active_timers
[QEMU_CLOCK_HOST
])
1090 timeKillEvent(data
->timerId
);
1092 data
->timerId
= timeSetEvent(1,
1096 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1098 if (!data
->timerId
) {
1099 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1102 timeEndPeriod(data
->period
);
1109 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1112 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1115 int init_timer_alarm(void)
1117 struct qemu_alarm_timer
*t
= NULL
;
1120 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1121 t
= &alarm_timers
[i
];
1133 /* first event is at time 0 */
1136 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1144 void quit_timers(void)
1146 struct qemu_alarm_timer
*t
= alarm_timer
;
1151 int qemu_calculate_timeout(void)
1153 #ifndef CONFIG_IOTHREAD
1159 /* XXX: use timeout computed from timers */
1162 /* Advance virtual time to the next event. */
1163 delta
= qemu_icount_delta();
1165 /* If virtual time is ahead of real time then just
1167 timeout
= (delta
+ 999999) / 1000000;
1169 /* Wait for either IO to occur or the next
1171 add
= qemu_next_deadline();
1172 /* We advance the timer before checking for IO.
1173 Limit the amount we advance so that early IO
1174 activity won't get the guest too far ahead. */
1178 qemu_icount
+= qemu_icount_round (add
);
1179 timeout
= delta
/ 1000000;
1186 #else /* CONFIG_IOTHREAD */