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 #ifndef CONFIG_IOTHREAD
114 static int64_t qemu_icount_delta(void)
117 return 5000 * (int64_t) 1000000;
118 } else if (use_icount
== 1) {
119 /* When not using an adaptive execution frequency
120 we tend to get badly out of sync with real time,
121 so just delay for a reasonable amount of time. */
124 return cpu_get_icount() - cpu_get_clock();
129 /* enable cpu_get_ticks() */
130 void cpu_enable_ticks(void)
132 if (!timers_state
.cpu_ticks_enabled
) {
133 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
134 timers_state
.cpu_clock_offset
-= get_clock();
135 timers_state
.cpu_ticks_enabled
= 1;
139 /* disable cpu_get_ticks() : the clock is stopped. You must not call
140 cpu_get_ticks() after that. */
141 void cpu_disable_ticks(void)
143 if (timers_state
.cpu_ticks_enabled
) {
144 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
145 timers_state
.cpu_clock_offset
= cpu_get_clock();
146 timers_state
.cpu_ticks_enabled
= 0;
150 /***********************************************************/
153 #define QEMU_CLOCK_REALTIME 0
154 #define QEMU_CLOCK_VIRTUAL 1
155 #define QEMU_CLOCK_HOST 2
160 /* XXX: add frequency */
168 struct QEMUTimer
*next
;
171 struct qemu_alarm_timer
{
173 int (*start
)(struct qemu_alarm_timer
*t
);
174 void (*stop
)(struct qemu_alarm_timer
*t
);
175 void (*rearm
)(struct qemu_alarm_timer
*t
);
182 static struct qemu_alarm_timer
*alarm_timer
;
184 int qemu_alarm_pending(void)
186 return alarm_timer
->pending
;
189 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
194 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
196 if (!alarm_has_dynticks(t
))
202 /* TODO: MIN_TIMER_REARM_US should be optimized */
203 #define MIN_TIMER_REARM_US 250
207 struct qemu_alarm_win32
{
210 } alarm_win32_data
= {0, 0};
212 static int win32_start_timer(struct qemu_alarm_timer
*t
);
213 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
214 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
218 static int unix_start_timer(struct qemu_alarm_timer
*t
);
219 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
223 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
224 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
225 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
227 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
228 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
230 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
231 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
233 #endif /* __linux__ */
237 /* Correlation between real and virtual time is always going to be
238 fairly approximate, so ignore small variation.
239 When the guest is idle real and virtual time will be aligned in
241 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
243 static void icount_adjust(void)
248 static int64_t last_delta
;
249 /* If the VM is not running, then do nothing. */
253 cur_time
= cpu_get_clock();
254 cur_icount
= qemu_get_clock(vm_clock
);
255 delta
= cur_icount
- cur_time
;
256 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
258 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
259 && icount_time_shift
> 0) {
260 /* The guest is getting too far ahead. Slow time down. */
264 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
265 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
266 /* The guest is getting too far behind. Speed time up. */
270 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
273 static void icount_adjust_rt(void * opaque
)
275 qemu_mod_timer(icount_rt_timer
,
276 qemu_get_clock(rt_clock
) + 1000);
280 static void icount_adjust_vm(void * opaque
)
282 qemu_mod_timer(icount_vm_timer
,
283 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
287 int64_t qemu_icount_round(int64_t count
)
289 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
292 static struct qemu_alarm_timer alarm_timers
[] = {
295 {"dynticks", dynticks_start_timer
,
296 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
297 /* HPET - if available - is preferred */
298 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
299 /* ...otherwise try RTC */
300 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
302 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
304 {"dynticks", win32_start_timer
,
305 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
306 {"win32", win32_start_timer
,
307 win32_stop_timer
, NULL
, &alarm_win32_data
},
312 static void show_available_alarms(void)
316 printf("Available alarm timers, in order of precedence:\n");
317 for (i
= 0; alarm_timers
[i
].name
; i
++)
318 printf("%s\n", alarm_timers
[i
].name
);
321 void configure_alarms(char const *opt
)
325 int count
= ARRAY_SIZE(alarm_timers
) - 1;
328 struct qemu_alarm_timer tmp
;
330 if (!strcmp(opt
, "?")) {
331 show_available_alarms();
335 arg
= qemu_strdup(opt
);
337 /* Reorder the array */
338 name
= strtok(arg
, ",");
340 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
341 if (!strcmp(alarm_timers
[i
].name
, name
))
346 fprintf(stderr
, "Unknown clock %s\n", name
);
355 tmp
= alarm_timers
[i
];
356 alarm_timers
[i
] = alarm_timers
[cur
];
357 alarm_timers
[cur
] = tmp
;
361 name
= strtok(NULL
, ",");
367 /* Disable remaining timers */
368 for (i
= cur
; i
< count
; i
++)
369 alarm_timers
[i
].name
= NULL
;
371 show_available_alarms();
376 #define QEMU_NUM_CLOCKS 3
380 QEMUClock
*host_clock
;
382 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
384 static QEMUClock
*qemu_new_clock(int type
)
387 clock
= qemu_mallocz(sizeof(QEMUClock
));
393 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
395 clock
->enabled
= enabled
;
398 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
402 ts
= qemu_mallocz(sizeof(QEMUTimer
));
409 void qemu_free_timer(QEMUTimer
*ts
)
414 /* stop a timer, but do not dealloc it */
415 void qemu_del_timer(QEMUTimer
*ts
)
419 /* NOTE: this code must be signal safe because
420 qemu_timer_expired() can be called from a signal. */
421 pt
= &active_timers
[ts
->clock
->type
];
434 /* modify the current timer so that it will be fired when current_time
435 >= expire_time. The corresponding callback will be called. */
436 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
442 /* add the timer in the sorted list */
443 /* NOTE: this code must be signal safe because
444 qemu_timer_expired() can be called from a signal. */
445 pt
= &active_timers
[ts
->clock
->type
];
450 if (t
->expire_time
> expire_time
)
454 ts
->expire_time
= expire_time
;
458 /* Rearm if necessary */
459 if (pt
== &active_timers
[ts
->clock
->type
]) {
460 if (!alarm_timer
->pending
) {
461 qemu_rearm_alarm_timer(alarm_timer
);
463 /* Interrupt execution to force deadline recalculation. */
469 int qemu_timer_pending(QEMUTimer
*ts
)
472 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
479 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
483 return (timer_head
->expire_time
<= current_time
);
486 static void qemu_run_timers(QEMUClock
*clock
)
488 QEMUTimer
**ptimer_head
, *ts
;
489 int64_t current_time
;
494 current_time
= qemu_get_clock (clock
);
495 ptimer_head
= &active_timers
[clock
->type
];
498 if (!ts
|| ts
->expire_time
> current_time
)
500 /* remove timer from the list before calling the callback */
501 *ptimer_head
= ts
->next
;
504 /* run the callback (the timer list can be modified) */
509 int64_t qemu_get_clock(QEMUClock
*clock
)
511 switch(clock
->type
) {
512 case QEMU_CLOCK_REALTIME
:
513 return get_clock() / 1000000;
515 case QEMU_CLOCK_VIRTUAL
:
517 return cpu_get_icount();
519 return cpu_get_clock();
521 case QEMU_CLOCK_HOST
:
522 return get_clock_realtime();
526 int64_t qemu_get_clock_ns(QEMUClock
*clock
)
528 switch(clock
->type
) {
529 case QEMU_CLOCK_REALTIME
:
532 case QEMU_CLOCK_VIRTUAL
:
534 return cpu_get_icount();
536 return cpu_get_clock();
538 case QEMU_CLOCK_HOST
:
539 return get_clock_realtime();
543 void init_clocks(void)
545 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
546 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
547 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
549 rtc_clock
= host_clock
;
553 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
555 uint64_t expire_time
;
557 if (qemu_timer_pending(ts
)) {
558 expire_time
= ts
->expire_time
;
562 qemu_put_be64(f
, expire_time
);
565 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
567 uint64_t expire_time
;
569 expire_time
= qemu_get_be64(f
);
570 if (expire_time
!= -1) {
571 qemu_mod_timer(ts
, expire_time
);
577 static const VMStateDescription vmstate_timers
= {
580 .minimum_version_id
= 1,
581 .minimum_version_id_old
= 1,
582 .fields
= (VMStateField
[]) {
583 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
584 VMSTATE_INT64(dummy
, TimersState
),
585 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
586 VMSTATE_END_OF_LIST()
590 void configure_icount(const char *option
)
592 vmstate_register(NULL
, 0, &vmstate_timers
, &timers_state
);
596 if (strcmp(option
, "auto") != 0) {
597 icount_time_shift
= strtol(option
, NULL
, 0);
604 /* 125MIPS seems a reasonable initial guess at the guest speed.
605 It will be corrected fairly quickly anyway. */
606 icount_time_shift
= 3;
608 /* Have both realtime and virtual time triggers for speed adjustment.
609 The realtime trigger catches emulated time passing too slowly,
610 the virtual time trigger catches emulated time passing too fast.
611 Realtime triggers occur even when idle, so use them less frequently
613 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
614 qemu_mod_timer(icount_rt_timer
,
615 qemu_get_clock(rt_clock
) + 1000);
616 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
617 qemu_mod_timer(icount_vm_timer
,
618 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
621 void qemu_run_all_timers(void)
623 alarm_timer
->pending
= 0;
625 /* rearm timer, if not periodic */
626 if (alarm_timer
->expired
) {
627 alarm_timer
->expired
= 0;
628 qemu_rearm_alarm_timer(alarm_timer
);
633 qemu_run_timers(vm_clock
);
636 qemu_run_timers(rt_clock
);
637 qemu_run_timers(host_clock
);
641 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
642 DWORD_PTR dwUser
, DWORD_PTR dw1
,
645 static void host_alarm_handler(int host_signum
)
648 struct qemu_alarm_timer
*t
= alarm_timer
;
653 #define DISP_FREQ 1000
655 static int64_t delta_min
= INT64_MAX
;
656 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
658 ti
= qemu_get_clock(vm_clock
);
659 if (last_clock
!= 0) {
660 delta
= ti
- last_clock
;
661 if (delta
< delta_min
)
663 if (delta
> delta_max
)
666 if (++count
== DISP_FREQ
) {
667 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
668 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
669 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
670 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
671 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
673 delta_min
= INT64_MAX
;
681 if (alarm_has_dynticks(t
) ||
683 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
684 qemu_get_clock(vm_clock
))) ||
685 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
686 qemu_get_clock(rt_clock
)) ||
687 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
688 qemu_get_clock(host_clock
))) {
690 t
->expired
= alarm_has_dynticks(t
);
696 int64_t qemu_next_deadline(void)
698 /* To avoid problems with overflow limit this to 2^32. */
699 int64_t delta
= INT32_MAX
;
701 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
702 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
703 qemu_get_clock(vm_clock
);
705 if (active_timers
[QEMU_CLOCK_HOST
]) {
706 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
707 qemu_get_clock(host_clock
);
720 #if defined(__linux__)
722 #define RTC_FREQ 1024
724 static uint64_t qemu_next_deadline_dyntick(void)
732 delta
= (qemu_next_deadline() + 999) / 1000;
734 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
735 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
736 qemu_get_clock(rt_clock
))*1000;
741 if (delta
< MIN_TIMER_REARM_US
)
742 delta
= MIN_TIMER_REARM_US
;
747 static void enable_sigio_timer(int fd
)
749 struct sigaction act
;
752 sigfillset(&act
.sa_mask
);
754 act
.sa_handler
= host_alarm_handler
;
756 sigaction(SIGIO
, &act
, NULL
);
757 fcntl_setfl(fd
, O_ASYNC
);
758 fcntl(fd
, F_SETOWN
, getpid());
761 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
763 struct hpet_info info
;
766 fd
= qemu_open("/dev/hpet", O_RDONLY
);
771 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
773 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
774 "error, but for better emulation accuracy type:\n"
775 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
779 /* Check capabilities */
780 r
= ioctl(fd
, HPET_INFO
, &info
);
784 /* Enable periodic mode */
785 r
= ioctl(fd
, HPET_EPI
, 0);
786 if (info
.hi_flags
&& (r
< 0))
789 /* Enable interrupt */
790 r
= ioctl(fd
, HPET_IE_ON
, 0);
794 enable_sigio_timer(fd
);
795 t
->priv
= (void *)(long)fd
;
803 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
805 int fd
= (long)t
->priv
;
810 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
813 unsigned long current_rtc_freq
= 0;
815 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
818 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
819 if (current_rtc_freq
!= RTC_FREQ
&&
820 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
821 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
822 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
823 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
826 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
832 enable_sigio_timer(rtc_fd
);
834 t
->priv
= (void *)(long)rtc_fd
;
839 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
841 int rtc_fd
= (long)t
->priv
;
846 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
850 struct sigaction act
;
852 sigfillset(&act
.sa_mask
);
854 act
.sa_handler
= host_alarm_handler
;
856 sigaction(SIGALRM
, &act
, NULL
);
859 * Initialize ev struct to 0 to avoid valgrind complaining
860 * about uninitialized data in timer_create call
862 memset(&ev
, 0, sizeof(ev
));
863 ev
.sigev_value
.sival_int
= 0;
864 ev
.sigev_notify
= SIGEV_SIGNAL
;
865 ev
.sigev_signo
= SIGALRM
;
867 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
868 perror("timer_create");
870 /* disable dynticks */
871 fprintf(stderr
, "Dynamic Ticks disabled\n");
876 t
->priv
= (void *)(long)host_timer
;
881 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
883 timer_t host_timer
= (timer_t
)(long)t
->priv
;
885 timer_delete(host_timer
);
888 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
890 timer_t host_timer
= (timer_t
)(long)t
->priv
;
891 struct itimerspec timeout
;
892 int64_t nearest_delta_us
= INT64_MAX
;
895 assert(alarm_has_dynticks(t
));
896 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
897 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
898 !active_timers
[QEMU_CLOCK_HOST
])
901 nearest_delta_us
= qemu_next_deadline_dyntick();
903 /* check whether a timer is already running */
904 if (timer_gettime(host_timer
, &timeout
)) {
906 fprintf(stderr
, "Internal timer error: aborting\n");
909 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
910 if (current_us
&& current_us
<= nearest_delta_us
)
913 timeout
.it_interval
.tv_sec
= 0;
914 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
915 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
916 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
917 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
919 fprintf(stderr
, "Internal timer error: aborting\n");
924 #endif /* defined(__linux__) */
926 static int unix_start_timer(struct qemu_alarm_timer
*t
)
928 struct sigaction act
;
929 struct itimerval itv
;
933 sigfillset(&act
.sa_mask
);
935 act
.sa_handler
= host_alarm_handler
;
937 sigaction(SIGALRM
, &act
, NULL
);
939 itv
.it_interval
.tv_sec
= 0;
940 /* for i386 kernel 2.6 to get 1 ms */
941 itv
.it_interval
.tv_usec
= 999;
942 itv
.it_value
.tv_sec
= 0;
943 itv
.it_value
.tv_usec
= 10 * 1000;
945 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
952 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
954 struct itimerval itv
;
956 memset(&itv
, 0, sizeof(itv
));
957 setitimer(ITIMER_REAL
, &itv
, NULL
);
960 #endif /* !defined(_WIN32) */
965 static int win32_start_timer(struct qemu_alarm_timer
*t
)
968 struct qemu_alarm_win32
*data
= t
->priv
;
971 memset(&tc
, 0, sizeof(tc
));
972 timeGetDevCaps(&tc
, sizeof(tc
));
974 data
->period
= tc
.wPeriodMin
;
975 timeBeginPeriod(data
->period
);
977 flags
= TIME_CALLBACK_FUNCTION
;
978 if (alarm_has_dynticks(t
))
979 flags
|= TIME_ONESHOT
;
981 flags
|= TIME_PERIODIC
;
983 data
->timerId
= timeSetEvent(1, // interval (ms)
984 data
->period
, // resolution
985 host_alarm_handler
, // function
986 (DWORD
)t
, // parameter
989 if (!data
->timerId
) {
990 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
992 timeEndPeriod(data
->period
);
999 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1001 struct qemu_alarm_win32
*data
= t
->priv
;
1003 timeKillEvent(data
->timerId
);
1004 timeEndPeriod(data
->period
);
1007 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1009 struct qemu_alarm_win32
*data
= t
->priv
;
1011 assert(alarm_has_dynticks(t
));
1012 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1013 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1014 !active_timers
[QEMU_CLOCK_HOST
])
1017 timeKillEvent(data
->timerId
);
1019 data
->timerId
= timeSetEvent(1,
1023 TIME_ONESHOT
| TIME_CALLBACK_FUNCTION
);
1025 if (!data
->timerId
) {
1026 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1029 timeEndPeriod(data
->period
);
1036 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1039 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1042 int init_timer_alarm(void)
1044 struct qemu_alarm_timer
*t
= NULL
;
1047 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1048 t
= &alarm_timers
[i
];
1060 /* first event is at time 0 */
1063 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1071 void quit_timers(void)
1073 struct qemu_alarm_timer
*t
= alarm_timer
;
1078 int qemu_calculate_timeout(void)
1080 #ifndef CONFIG_IOTHREAD
1086 /* XXX: use timeout computed from timers */
1089 /* Advance virtual time to the next event. */
1090 delta
= qemu_icount_delta();
1092 /* If virtual time is ahead of real time then just
1094 timeout
= (delta
+ 999999) / 1000000;
1096 /* Wait for either IO to occur or the next
1098 add
= qemu_next_deadline();
1099 /* We advance the timer before checking for IO.
1100 Limit the amount we advance so that early IO
1101 activity won't get the guest too far ahead. */
1105 qemu_icount
+= qemu_icount_round (add
);
1106 timeout
= delta
/ 1000000;
1113 #else /* CONFIG_IOTHREAD */