scsi-disk: Remove duplicate cdb parsing
[qemu/ar7.git] / qemu-timer.c
blob95814af798392a77486677da19c3092b78387f60
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
30 #include "hw/hw.h"
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <time.h>
35 #include <errno.h>
36 #include <sys/time.h>
37 #include <signal.h>
38 #ifdef __FreeBSD__
39 #include <sys/param.h>
40 #endif
42 #ifdef __linux__
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> */
48 #include "hpet.h"
49 #endif
51 #ifdef _WIN32
52 #include <windows.h>
53 #include <mmsystem.h>
54 #endif
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;
75 int64_t dummy;
76 } TimersState;
78 TimersState timers_state;
80 /* return the host CPU cycle counter and handle stop/restart */
81 int64_t cpu_get_ticks(void)
83 if (use_icount) {
84 return cpu_get_icount();
86 if (!timers_state.cpu_ticks_enabled) {
87 return timers_state.cpu_ticks_offset;
88 } else {
89 int64_t ticks;
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
93 software suspend */
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)
104 int64_t ti;
105 if (!timers_state.cpu_ticks_enabled) {
106 return timers_state.cpu_clock_offset;
107 } else {
108 ti = get_clock();
109 return ti + timers_state.cpu_clock_offset;
113 #ifndef CONFIG_IOTHREAD
114 static int64_t qemu_icount_delta(void)
116 if (!use_icount) {
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. */
122 return 0;
123 } else {
124 return cpu_get_icount() - cpu_get_clock();
127 #endif
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 /***********************************************************/
151 /* timers */
153 #define QEMU_CLOCK_REALTIME 0
154 #define QEMU_CLOCK_VIRTUAL 1
155 #define QEMU_CLOCK_HOST 2
157 struct QEMUClock {
158 int type;
159 int enabled;
160 /* XXX: add frequency */
163 struct QEMUTimer {
164 QEMUClock *clock;
165 int64_t expire_time;
166 QEMUTimerCB *cb;
167 void *opaque;
168 struct QEMUTimer *next;
171 struct qemu_alarm_timer {
172 char const *name;
173 int (*start)(struct qemu_alarm_timer *t);
174 void (*stop)(struct qemu_alarm_timer *t);
175 void (*rearm)(struct qemu_alarm_timer *t);
176 void *priv;
178 char expired;
179 char pending;
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)
191 return !!t->rearm;
194 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
196 if (!alarm_has_dynticks(t))
197 return;
199 t->rearm(t);
202 /* TODO: MIN_TIMER_REARM_US should be optimized */
203 #define MIN_TIMER_REARM_US 250
205 #ifdef _WIN32
207 struct qemu_alarm_win32 {
208 MMRESULT timerId;
209 unsigned int period;
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);
216 #else
218 static int unix_start_timer(struct qemu_alarm_timer *t);
219 static void unix_stop_timer(struct qemu_alarm_timer *t);
221 #ifdef __linux__
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__ */
235 #endif /* _WIN32 */
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
240 the IO wait loop. */
241 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
243 static void icount_adjust(void)
245 int64_t cur_time;
246 int64_t cur_icount;
247 int64_t delta;
248 static int64_t last_delta;
249 /* If the VM is not running, then do nothing. */
250 if (!vm_running)
251 return;
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. */
257 if (delta > 0
258 && last_delta + ICOUNT_WOBBLE < delta * 2
259 && icount_time_shift > 0) {
260 /* The guest is getting too far ahead. Slow time down. */
261 icount_time_shift--;
263 if (delta < 0
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. */
267 icount_time_shift++;
269 last_delta = delta;
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);
277 icount_adjust();
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);
284 icount_adjust();
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[] = {
293 #ifndef _WIN32
294 #ifdef __linux__
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},
301 #endif
302 {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
303 #else
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},
308 #endif
309 {NULL, }
312 static void show_available_alarms(void)
314 int i;
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)
323 int i;
324 int cur = 0;
325 int count = ARRAY_SIZE(alarm_timers) - 1;
326 char *arg;
327 char *name;
328 struct qemu_alarm_timer tmp;
330 if (!strcmp(opt, "?")) {
331 show_available_alarms();
332 exit(0);
335 arg = qemu_strdup(opt);
337 /* Reorder the array */
338 name = strtok(arg, ",");
339 while (name) {
340 for (i = 0; i < count && alarm_timers[i].name; i++) {
341 if (!strcmp(alarm_timers[i].name, name))
342 break;
345 if (i == count) {
346 fprintf(stderr, "Unknown clock %s\n", name);
347 goto next;
350 if (i < cur)
351 /* Ignore */
352 goto next;
354 /* Swap */
355 tmp = alarm_timers[i];
356 alarm_timers[i] = alarm_timers[cur];
357 alarm_timers[cur] = tmp;
359 cur++;
360 next:
361 name = strtok(NULL, ",");
364 qemu_free(arg);
366 if (cur) {
367 /* Disable remaining timers */
368 for (i = cur; i < count; i++)
369 alarm_timers[i].name = NULL;
370 } else {
371 show_available_alarms();
372 exit(1);
376 #define QEMU_NUM_CLOCKS 3
378 QEMUClock *rt_clock;
379 QEMUClock *vm_clock;
380 QEMUClock *host_clock;
382 static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
384 static QEMUClock *qemu_new_clock(int type)
386 QEMUClock *clock;
387 clock = qemu_mallocz(sizeof(QEMUClock));
388 clock->type = type;
389 clock->enabled = 1;
390 return clock;
393 void qemu_clock_enable(QEMUClock *clock, int enabled)
395 clock->enabled = enabled;
398 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
400 QEMUTimer *ts;
402 ts = qemu_mallocz(sizeof(QEMUTimer));
403 ts->clock = clock;
404 ts->cb = cb;
405 ts->opaque = opaque;
406 return ts;
409 void qemu_free_timer(QEMUTimer *ts)
411 qemu_free(ts);
414 /* stop a timer, but do not dealloc it */
415 void qemu_del_timer(QEMUTimer *ts)
417 QEMUTimer **pt, *t;
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];
422 for(;;) {
423 t = *pt;
424 if (!t)
425 break;
426 if (t == ts) {
427 *pt = t->next;
428 break;
430 pt = &t->next;
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)
438 QEMUTimer **pt, *t;
440 qemu_del_timer(ts);
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];
446 for(;;) {
447 t = *pt;
448 if (!t)
449 break;
450 if (t->expire_time > expire_time)
451 break;
452 pt = &t->next;
454 ts->expire_time = expire_time;
455 ts->next = *pt;
456 *pt = ts;
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. */
464 if (use_icount)
465 qemu_notify_event();
469 int qemu_timer_pending(QEMUTimer *ts)
471 QEMUTimer *t;
472 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
473 if (t == ts)
474 return 1;
476 return 0;
479 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
481 if (!timer_head)
482 return 0;
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;
491 if (!clock->enabled)
492 return;
494 current_time = qemu_get_clock (clock);
495 ptimer_head = &active_timers[clock->type];
496 for(;;) {
497 ts = *ptimer_head;
498 if (!ts || ts->expire_time > current_time)
499 break;
500 /* remove timer from the list before calling the callback */
501 *ptimer_head = ts->next;
502 ts->next = NULL;
504 /* run the callback (the timer list can be modified) */
505 ts->cb(ts->opaque);
509 int64_t qemu_get_clock(QEMUClock *clock)
511 switch(clock->type) {
512 case QEMU_CLOCK_REALTIME:
513 return get_clock() / 1000000;
514 default:
515 case QEMU_CLOCK_VIRTUAL:
516 if (use_icount) {
517 return cpu_get_icount();
518 } else {
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:
530 return get_clock();
531 default:
532 case QEMU_CLOCK_VIRTUAL:
533 if (use_icount) {
534 return cpu_get_icount();
535 } else {
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;
552 /* save a timer */
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;
559 } else {
560 expire_time = -1;
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);
572 } else {
573 qemu_del_timer(ts);
577 static const VMStateDescription vmstate_timers = {
578 .name = "timer",
579 .version_id = 2,
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);
593 if (!option)
594 return;
596 if (strcmp(option, "auto") != 0) {
597 icount_time_shift = strtol(option, NULL, 0);
598 use_icount = 1;
599 return;
602 use_icount = 2;
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
612 than VM triggers. */
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);
631 /* vm time timers */
632 if (vm_running) {
633 qemu_run_timers(vm_clock);
636 qemu_run_timers(rt_clock);
637 qemu_run_timers(host_clock);
640 #ifdef _WIN32
641 static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
642 DWORD_PTR dwUser, DWORD_PTR dw1,
643 DWORD_PTR dw2)
644 #else
645 static void host_alarm_handler(int host_signum)
646 #endif
648 struct qemu_alarm_timer *t = alarm_timer;
649 if (!t)
650 return;
652 #if 0
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;
657 static int count;
658 ti = qemu_get_clock(vm_clock);
659 if (last_clock != 0) {
660 delta = ti - last_clock;
661 if (delta < delta_min)
662 delta_min = delta;
663 if (delta > delta_max)
664 delta_max = delta;
665 delta_cum += delta;
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));
672 count = 0;
673 delta_min = INT64_MAX;
674 delta_max = 0;
675 delta_cum = 0;
678 last_clock = ti;
680 #endif
681 if (alarm_has_dynticks(t) ||
682 (!use_icount &&
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);
691 t->pending = 1;
692 qemu_notify_event();
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);
708 if (hdelta < delta)
709 delta = hdelta;
712 if (delta < 0)
713 delta = 0;
715 return delta;
718 #ifndef _WIN32
720 #if defined(__linux__)
722 #define RTC_FREQ 1024
724 static uint64_t qemu_next_deadline_dyntick(void)
726 int64_t delta;
727 int64_t rtdelta;
729 if (use_icount)
730 delta = INT32_MAX;
731 else
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;
737 if (rtdelta < delta)
738 delta = rtdelta;
741 if (delta < MIN_TIMER_REARM_US)
742 delta = MIN_TIMER_REARM_US;
744 return delta;
747 static void enable_sigio_timer(int fd)
749 struct sigaction act;
751 /* timer signal */
752 sigfillset(&act.sa_mask);
753 act.sa_flags = 0;
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;
764 int r, fd;
766 fd = qemu_open("/dev/hpet", O_RDONLY);
767 if (fd < 0)
768 return -1;
770 /* Set frequency */
771 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
772 if (r < 0) {
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");
776 goto fail;
779 /* Check capabilities */
780 r = ioctl(fd, HPET_INFO, &info);
781 if (r < 0)
782 goto fail;
784 /* Enable periodic mode */
785 r = ioctl(fd, HPET_EPI, 0);
786 if (info.hi_flags && (r < 0))
787 goto fail;
789 /* Enable interrupt */
790 r = ioctl(fd, HPET_IE_ON, 0);
791 if (r < 0)
792 goto fail;
794 enable_sigio_timer(fd);
795 t->priv = (void *)(long)fd;
797 return 0;
798 fail:
799 close(fd);
800 return -1;
803 static void hpet_stop_timer(struct qemu_alarm_timer *t)
805 int fd = (long)t->priv;
807 close(fd);
810 static int rtc_start_timer(struct qemu_alarm_timer *t)
812 int rtc_fd;
813 unsigned long current_rtc_freq = 0;
815 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
816 if (rtc_fd < 0)
817 return -1;
818 ioctl(rtc_fd, RTC_IRQP_READ, &current_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");
824 goto fail;
826 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
827 fail:
828 close(rtc_fd);
829 return -1;
832 enable_sigio_timer(rtc_fd);
834 t->priv = (void *)(long)rtc_fd;
836 return 0;
839 static void rtc_stop_timer(struct qemu_alarm_timer *t)
841 int rtc_fd = (long)t->priv;
843 close(rtc_fd);
846 static int dynticks_start_timer(struct qemu_alarm_timer *t)
848 struct sigevent ev;
849 timer_t host_timer;
850 struct sigaction act;
852 sigfillset(&act.sa_mask);
853 act.sa_flags = 0;
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");
873 return -1;
876 t->priv = (void *)(long)host_timer;
878 return 0;
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;
893 int64_t current_us;
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])
899 return;
901 nearest_delta_us = qemu_next_deadline_dyntick();
903 /* check whether a timer is already running */
904 if (timer_gettime(host_timer, &timeout)) {
905 perror("gettime");
906 fprintf(stderr, "Internal timer error: aborting\n");
907 exit(1);
909 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
910 if (current_us && current_us <= nearest_delta_us)
911 return;
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)) {
918 perror("settime");
919 fprintf(stderr, "Internal timer error: aborting\n");
920 exit(1);
924 #endif /* defined(__linux__) */
926 static int unix_start_timer(struct qemu_alarm_timer *t)
928 struct sigaction act;
929 struct itimerval itv;
930 int err;
932 /* timer signal */
933 sigfillset(&act.sa_mask);
934 act.sa_flags = 0;
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);
946 if (err)
947 return -1;
949 return 0;
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) */
963 #ifdef _WIN32
965 static int win32_start_timer(struct qemu_alarm_timer *t)
967 TIMECAPS tc;
968 struct qemu_alarm_win32 *data = t->priv;
969 UINT flags;
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;
980 else
981 flags |= TIME_PERIODIC;
983 data->timerId = timeSetEvent(1, // interval (ms)
984 data->period, // resolution
985 host_alarm_handler, // function
986 (DWORD)t, // parameter
987 flags);
989 if (!data->timerId) {
990 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
991 GetLastError());
992 timeEndPeriod(data->period);
993 return -1;
996 return 0;
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])
1015 return;
1017 timeKillEvent(data->timerId);
1019 data->timerId = timeSetEvent(1,
1020 data->period,
1021 host_alarm_handler,
1022 (DWORD)t,
1023 TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1025 if (!data->timerId) {
1026 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1027 GetLastError());
1029 timeEndPeriod(data->period);
1030 exit(1);
1034 #endif /* _WIN32 */
1036 static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1038 if (running)
1039 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1042 int init_timer_alarm(void)
1044 struct qemu_alarm_timer *t = NULL;
1045 int i, err = -1;
1047 for (i = 0; alarm_timers[i].name; i++) {
1048 t = &alarm_timers[i];
1050 err = t->start(t);
1051 if (!err)
1052 break;
1055 if (err) {
1056 err = -ENOENT;
1057 goto fail;
1060 /* first event is at time 0 */
1061 t->pending = 1;
1062 alarm_timer = t;
1063 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1065 return 0;
1067 fail:
1068 return err;
1071 void quit_timers(void)
1073 struct qemu_alarm_timer *t = alarm_timer;
1074 alarm_timer = NULL;
1075 t->stop(t);
1078 int qemu_calculate_timeout(void)
1080 #ifndef CONFIG_IOTHREAD
1081 int timeout;
1083 if (!vm_running)
1084 timeout = 5000;
1085 else {
1086 /* XXX: use timeout computed from timers */
1087 int64_t add;
1088 int64_t delta;
1089 /* Advance virtual time to the next event. */
1090 delta = qemu_icount_delta();
1091 if (delta > 0) {
1092 /* If virtual time is ahead of real time then just
1093 wait for IO. */
1094 timeout = (delta + 999999) / 1000000;
1095 } else {
1096 /* Wait for either IO to occur or the next
1097 timer event. */
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. */
1102 if (add > 10000000)
1103 add = 10000000;
1104 delta += add;
1105 qemu_icount += qemu_icount_round (add);
1106 timeout = delta / 1000000;
1107 if (timeout < 0)
1108 timeout = 0;
1112 return timeout;
1113 #else /* CONFIG_IOTHREAD */
1114 return 1000;
1115 #endif