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)
115 if (use_icount
== 1) {
116 /* When not using an adaptive execution frequency
117 we tend to get badly out of sync with real time,
118 so just delay for a reasonable amount of time. */
121 return cpu_get_icount() - cpu_get_clock();
125 /* enable cpu_get_ticks() */
126 void cpu_enable_ticks(void)
128 if (!timers_state
.cpu_ticks_enabled
) {
129 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
130 timers_state
.cpu_clock_offset
-= get_clock();
131 timers_state
.cpu_ticks_enabled
= 1;
135 /* disable cpu_get_ticks() : the clock is stopped. You must not call
136 cpu_get_ticks() after that. */
137 void cpu_disable_ticks(void)
139 if (timers_state
.cpu_ticks_enabled
) {
140 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
141 timers_state
.cpu_clock_offset
= cpu_get_clock();
142 timers_state
.cpu_ticks_enabled
= 0;
146 /***********************************************************/
149 #define QEMU_CLOCK_REALTIME 0
150 #define QEMU_CLOCK_VIRTUAL 1
151 #define QEMU_CLOCK_HOST 2
160 int64_t expire_time
; /* in nanoseconds */
164 struct QEMUTimer
*next
;
167 struct qemu_alarm_timer
{
169 int (*start
)(struct qemu_alarm_timer
*t
);
170 void (*stop
)(struct qemu_alarm_timer
*t
);
171 void (*rearm
)(struct qemu_alarm_timer
*t
);
178 static struct qemu_alarm_timer
*alarm_timer
;
180 int qemu_alarm_pending(void)
182 return alarm_timer
->pending
;
185 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
190 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
192 if (!alarm_has_dynticks(t
))
198 /* TODO: MIN_TIMER_REARM_NS should be optimized */
199 #define MIN_TIMER_REARM_NS 250000
203 static int win32_start_timer(struct qemu_alarm_timer
*t
);
204 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
205 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
209 static int unix_start_timer(struct qemu_alarm_timer
*t
);
210 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
214 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
215 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
216 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
218 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
219 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
221 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
222 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
224 #endif /* __linux__ */
228 /* Correlation between real and virtual time is always going to be
229 fairly approximate, so ignore small variation.
230 When the guest is idle real and virtual time will be aligned in
232 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
234 static void icount_adjust(void)
239 static int64_t last_delta
;
240 /* If the VM is not running, then do nothing. */
244 cur_time
= cpu_get_clock();
245 cur_icount
= qemu_get_clock_ns(vm_clock
);
246 delta
= cur_icount
- cur_time
;
247 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
249 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
250 && icount_time_shift
> 0) {
251 /* The guest is getting too far ahead. Slow time down. */
255 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
256 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
257 /* The guest is getting too far behind. Speed time up. */
261 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
264 static void icount_adjust_rt(void * opaque
)
266 qemu_mod_timer(icount_rt_timer
,
267 qemu_get_clock_ms(rt_clock
) + 1000);
271 static void icount_adjust_vm(void * opaque
)
273 qemu_mod_timer(icount_vm_timer
,
274 qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 10);
278 int64_t qemu_icount_round(int64_t count
)
280 return (count
+ (1 << icount_time_shift
) - 1) >> icount_time_shift
;
283 static struct qemu_alarm_timer alarm_timers
[] = {
286 {"dynticks", dynticks_start_timer
,
287 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
288 /* HPET - if available - is preferred */
289 {"hpet", hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
290 /* ...otherwise try RTC */
291 {"rtc", rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
293 {"unix", unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
295 {"dynticks", win32_start_timer
,
296 win32_stop_timer
, win32_rearm_timer
, NULL
},
297 {"win32", win32_start_timer
,
298 win32_stop_timer
, NULL
, NULL
},
303 static void show_available_alarms(void)
307 printf("Available alarm timers, in order of precedence:\n");
308 for (i
= 0; alarm_timers
[i
].name
; i
++)
309 printf("%s\n", alarm_timers
[i
].name
);
312 void configure_alarms(char const *opt
)
316 int count
= ARRAY_SIZE(alarm_timers
) - 1;
319 struct qemu_alarm_timer tmp
;
321 if (!strcmp(opt
, "?")) {
322 show_available_alarms();
326 arg
= qemu_strdup(opt
);
328 /* Reorder the array */
329 name
= strtok(arg
, ",");
331 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
332 if (!strcmp(alarm_timers
[i
].name
, name
))
337 fprintf(stderr
, "Unknown clock %s\n", name
);
346 tmp
= alarm_timers
[i
];
347 alarm_timers
[i
] = alarm_timers
[cur
];
348 alarm_timers
[cur
] = tmp
;
352 name
= strtok(NULL
, ",");
358 /* Disable remaining timers */
359 for (i
= cur
; i
< count
; i
++)
360 alarm_timers
[i
].name
= NULL
;
362 show_available_alarms();
367 #define QEMU_NUM_CLOCKS 3
371 QEMUClock
*host_clock
;
373 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
375 static QEMUClock
*qemu_new_clock(int type
)
378 clock
= qemu_mallocz(sizeof(QEMUClock
));
384 void qemu_clock_enable(QEMUClock
*clock
, int enabled
)
386 clock
->enabled
= enabled
;
389 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
390 QEMUTimerCB
*cb
, void *opaque
)
394 ts
= qemu_mallocz(sizeof(QEMUTimer
));
402 void qemu_free_timer(QEMUTimer
*ts
)
407 /* stop a timer, but do not dealloc it */
408 void qemu_del_timer(QEMUTimer
*ts
)
412 /* NOTE: this code must be signal safe because
413 qemu_timer_expired() can be called from a signal. */
414 pt
= &active_timers
[ts
->clock
->type
];
427 /* modify the current timer so that it will be fired when current_time
428 >= expire_time. The corresponding callback will be called. */
429 static void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
)
435 /* add the timer in the sorted list */
436 /* NOTE: this code must be signal safe because
437 qemu_timer_expired() can be called from a signal. */
438 pt
= &active_timers
[ts
->clock
->type
];
443 if (t
->expire_time
> expire_time
)
447 ts
->expire_time
= expire_time
;
451 /* Rearm if necessary */
452 if (pt
== &active_timers
[ts
->clock
->type
]) {
453 if (!alarm_timer
->pending
) {
454 qemu_rearm_alarm_timer(alarm_timer
);
456 /* Interrupt execution to force deadline recalculation. */
462 /* modify the current timer so that it will be fired when current_time
463 >= expire_time. The corresponding callback will be called. */
464 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
466 qemu_mod_timer_ns(ts
, expire_time
* ts
->scale
);
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
* timer_head
->scale
);
486 static void qemu_run_timers(QEMUClock
*clock
)
488 QEMUTimer
**ptimer_head
, *ts
;
489 int64_t current_time
;
494 current_time
= qemu_get_clock_ns(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_ns(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_ms(rt_clock
, icount_adjust_rt
, NULL
);
614 qemu_mod_timer(icount_rt_timer
,
615 qemu_get_clock_ms(rt_clock
) + 1000);
616 icount_vm_timer
= qemu_new_timer_ns(vm_clock
, icount_adjust_vm
, NULL
);
617 qemu_mod_timer(icount_vm_timer
,
618 qemu_get_clock_ns(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
);
640 static int64_t qemu_next_alarm_deadline(void);
643 static void CALLBACK
host_alarm_handler(PVOID lpParam
, BOOLEAN unused
)
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_ns(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
) ||
682 qemu_next_alarm_deadline () <= 0) {
683 t
->expired
= alarm_has_dynticks(t
);
689 int64_t qemu_next_deadline(void)
691 /* To avoid problems with overflow limit this to 2^32. */
692 int64_t delta
= INT32_MAX
;
694 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
695 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
696 qemu_get_clock_ns(vm_clock
);
698 if (active_timers
[QEMU_CLOCK_HOST
]) {
699 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
700 qemu_get_clock_ns(host_clock
);
711 static int64_t qemu_next_alarm_deadline(void)
716 if (!use_icount
&& active_timers
[QEMU_CLOCK_VIRTUAL
]) {
717 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
718 qemu_get_clock_ns(vm_clock
);
722 if (active_timers
[QEMU_CLOCK_HOST
]) {
723 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
724 qemu_get_clock_ns(host_clock
);
728 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
729 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
730 qemu_get_clock_ns(rt_clock
));
738 #if defined(__linux__)
740 #define RTC_FREQ 1024
742 static void enable_sigio_timer(int fd
)
744 struct sigaction act
;
747 sigfillset(&act
.sa_mask
);
749 act
.sa_handler
= host_alarm_handler
;
751 sigaction(SIGIO
, &act
, NULL
);
752 fcntl_setfl(fd
, O_ASYNC
);
753 fcntl(fd
, F_SETOWN
, getpid());
756 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
758 struct hpet_info info
;
761 fd
= qemu_open("/dev/hpet", O_RDONLY
);
766 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
768 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
769 "error, but for better emulation accuracy type:\n"
770 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
774 /* Check capabilities */
775 r
= ioctl(fd
, HPET_INFO
, &info
);
779 /* Enable periodic mode */
780 r
= ioctl(fd
, HPET_EPI
, 0);
781 if (info
.hi_flags
&& (r
< 0))
784 /* Enable interrupt */
785 r
= ioctl(fd
, HPET_IE_ON
, 0);
789 enable_sigio_timer(fd
);
790 t
->priv
= (void *)(long)fd
;
798 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
800 int fd
= (long)t
->priv
;
805 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
808 unsigned long current_rtc_freq
= 0;
810 TFR(rtc_fd
= qemu_open("/dev/rtc", O_RDONLY
));
813 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
814 if (current_rtc_freq
!= RTC_FREQ
&&
815 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
816 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
817 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
818 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
821 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
827 enable_sigio_timer(rtc_fd
);
829 t
->priv
= (void *)(long)rtc_fd
;
834 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
836 int rtc_fd
= (long)t
->priv
;
841 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
845 struct sigaction act
;
847 sigfillset(&act
.sa_mask
);
849 act
.sa_handler
= host_alarm_handler
;
851 sigaction(SIGALRM
, &act
, NULL
);
854 * Initialize ev struct to 0 to avoid valgrind complaining
855 * about uninitialized data in timer_create call
857 memset(&ev
, 0, sizeof(ev
));
858 ev
.sigev_value
.sival_int
= 0;
859 ev
.sigev_notify
= SIGEV_SIGNAL
;
860 ev
.sigev_signo
= SIGALRM
;
862 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
863 perror("timer_create");
865 /* disable dynticks */
866 fprintf(stderr
, "Dynamic Ticks disabled\n");
871 t
->priv
= (void *)(long)host_timer
;
876 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
878 timer_t host_timer
= (timer_t
)(long)t
->priv
;
880 timer_delete(host_timer
);
883 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
885 timer_t host_timer
= (timer_t
)(long)t
->priv
;
886 struct itimerspec timeout
;
887 int64_t nearest_delta_ns
= INT64_MAX
;
890 assert(alarm_has_dynticks(t
));
891 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
892 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
893 !active_timers
[QEMU_CLOCK_HOST
])
896 nearest_delta_ns
= qemu_next_alarm_deadline();
897 if (nearest_delta_ns
< MIN_TIMER_REARM_NS
)
898 nearest_delta_ns
= MIN_TIMER_REARM_NS
;
900 /* check whether a timer is already running */
901 if (timer_gettime(host_timer
, &timeout
)) {
903 fprintf(stderr
, "Internal timer error: aborting\n");
906 current_ns
= timeout
.it_value
.tv_sec
* 1000000000LL + timeout
.it_value
.tv_nsec
;
907 if (current_ns
&& current_ns
<= nearest_delta_ns
)
910 timeout
.it_interval
.tv_sec
= 0;
911 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
912 timeout
.it_value
.tv_sec
= nearest_delta_ns
/ 1000000000;
913 timeout
.it_value
.tv_nsec
= nearest_delta_ns
% 1000000000;
914 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
916 fprintf(stderr
, "Internal timer error: aborting\n");
921 #endif /* defined(__linux__) */
925 static int unix_start_timer(struct qemu_alarm_timer
*t
)
927 struct sigaction act
;
928 struct itimerval itv
;
932 sigfillset(&act
.sa_mask
);
934 act
.sa_handler
= host_alarm_handler
;
936 sigaction(SIGALRM
, &act
, NULL
);
938 itv
.it_interval
.tv_sec
= 0;
939 /* for i386 kernel 2.6 to get 1 ms */
940 itv
.it_interval
.tv_usec
= 999;
941 itv
.it_value
.tv_sec
= 0;
942 itv
.it_value
.tv_usec
= 10 * 1000;
944 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
951 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
953 struct itimerval itv
;
955 memset(&itv
, 0, sizeof(itv
));
956 setitimer(ITIMER_REAL
, &itv
, NULL
);
959 #endif /* !defined(_WIN32) */
964 static int win32_start_timer(struct qemu_alarm_timer
*t
)
969 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
970 is zero) that has already expired, the timer is not updated. Since
971 creating a new timer is relatively expensive, set a bogus one-hour
972 interval in the dynticks case. */
973 success
= CreateTimerQueueTimer(&hTimer
,
978 alarm_has_dynticks(t
) ? 3600000 : 1,
979 WT_EXECUTEINTIMERTHREAD
);
982 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
987 t
->priv
= (PVOID
) hTimer
;
991 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
993 HANDLE hTimer
= t
->priv
;
996 DeleteTimerQueueTimer(NULL
, hTimer
, NULL
);
1000 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1002 HANDLE hTimer
= t
->priv
;
1003 int nearest_delta_ms
;
1006 assert(alarm_has_dynticks(t
));
1007 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1008 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1009 !active_timers
[QEMU_CLOCK_HOST
])
1012 nearest_delta_ms
= (qemu_next_alarm_deadline() + 999999) / 1000000;
1013 if (nearest_delta_ms
< 1) {
1014 nearest_delta_ms
= 1;
1016 success
= ChangeTimerQueueTimer(NULL
,
1022 fprintf(stderr
, "Failed to rearm win32 alarm timer: %ld\n",
1031 static void alarm_timer_on_change_state_rearm(void *opaque
, int running
, int reason
)
1034 qemu_rearm_alarm_timer((struct qemu_alarm_timer
*) opaque
);
1037 int init_timer_alarm(void)
1039 struct qemu_alarm_timer
*t
= NULL
;
1042 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1043 t
= &alarm_timers
[i
];
1055 /* first event is at time 0 */
1058 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm
, t
);
1066 void quit_timers(void)
1068 struct qemu_alarm_timer
*t
= alarm_timer
;
1073 int qemu_calculate_timeout(void)
1079 /* When using icount, making forward progress with qemu_icount when the
1080 guest CPU is idle is critical. We only use the static io-thread timeout
1081 for non icount runs. */
1082 if (!use_icount
|| !vm_running
) {
1086 /* Advance virtual time to the next event. */
1087 delta
= qemu_icount_delta();
1089 /* If virtual time is ahead of real time then just
1091 timeout
= (delta
+ 999999) / 1000000;
1093 /* Wait for either IO to occur or the next
1095 add
= qemu_next_deadline();
1096 /* We advance the timer before checking for IO.
1097 Limit the amount we advance so that early IO
1098 activity won't get the guest too far ahead. */
1102 qemu_icount
+= qemu_icount_round (add
);
1103 timeout
= delta
/ 1000000;