]> git.proxmox.com Git - qemu.git/blob - qemu-timer.c
enable vm_clock to "warp" in the iothread+icount case
[qemu.git] / qemu-timer.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "sysemu.h"
26 #include "net.h"
27 #include "monitor.h"
28 #include "console.h"
29
30 #include "hw/hw.h"
31
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
41
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
50
51 #ifdef _WIN32
52 #include <windows.h>
53 #include <mmsystem.h>
54 #endif
55
56 #include "qemu-timer.h"
57
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;
66
67 /***********************************************************/
68 /* guest cycle counter */
69
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;
77
78 TimersState timers_state;
79
80 /* return the host CPU cycle counter and handle stop/restart */
81 int64_t cpu_get_ticks(void)
82 {
83 if (use_icount) {
84 return cpu_get_icount();
85 }
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;
95 }
96 timers_state.cpu_ticks_prev = ticks;
97 return ticks + timers_state.cpu_ticks_offset;
98 }
99 }
100
101 /* return the host CPU monotonic timer and handle stop/restart */
102 static int64_t cpu_get_clock(void)
103 {
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;
110 }
111 }
112
113 static int64_t qemu_icount_delta(void)
114 {
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. */
119 return 0;
120 } else {
121 return cpu_get_icount() - cpu_get_clock();
122 }
123 }
124
125 /* enable cpu_get_ticks() */
126 void cpu_enable_ticks(void)
127 {
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;
132 }
133 }
134
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)
138 {
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;
143 }
144 }
145
146 /***********************************************************/
147 /* timers */
148
149 #define QEMU_CLOCK_REALTIME 0
150 #define QEMU_CLOCK_VIRTUAL 1
151 #define QEMU_CLOCK_HOST 2
152
153 struct QEMUClock {
154 int type;
155 int enabled;
156
157 QEMUTimer *warp_timer;
158 };
159
160 struct QEMUTimer {
161 QEMUClock *clock;
162 int64_t expire_time; /* in nanoseconds */
163 int scale;
164 QEMUTimerCB *cb;
165 void *opaque;
166 struct QEMUTimer *next;
167 };
168
169 struct qemu_alarm_timer {
170 char const *name;
171 int (*start)(struct qemu_alarm_timer *t);
172 void (*stop)(struct qemu_alarm_timer *t);
173 void (*rearm)(struct qemu_alarm_timer *t);
174 void *priv;
175
176 char expired;
177 char pending;
178 };
179
180 static struct qemu_alarm_timer *alarm_timer;
181
182 int qemu_alarm_pending(void)
183 {
184 return alarm_timer->pending;
185 }
186
187 static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
188 {
189 return !!t->rearm;
190 }
191
192 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
193 {
194 if (!alarm_has_dynticks(t))
195 return;
196
197 t->rearm(t);
198 }
199
200 /* TODO: MIN_TIMER_REARM_NS should be optimized */
201 #define MIN_TIMER_REARM_NS 250000
202
203 #ifdef _WIN32
204
205 static int win32_start_timer(struct qemu_alarm_timer *t);
206 static void win32_stop_timer(struct qemu_alarm_timer *t);
207 static void win32_rearm_timer(struct qemu_alarm_timer *t);
208
209 #else
210
211 static int unix_start_timer(struct qemu_alarm_timer *t);
212 static void unix_stop_timer(struct qemu_alarm_timer *t);
213
214 #ifdef __linux__
215
216 static int dynticks_start_timer(struct qemu_alarm_timer *t);
217 static void dynticks_stop_timer(struct qemu_alarm_timer *t);
218 static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
219
220 static int hpet_start_timer(struct qemu_alarm_timer *t);
221 static void hpet_stop_timer(struct qemu_alarm_timer *t);
222
223 static int rtc_start_timer(struct qemu_alarm_timer *t);
224 static void rtc_stop_timer(struct qemu_alarm_timer *t);
225
226 #endif /* __linux__ */
227
228 #endif /* _WIN32 */
229
230 /* Correlation between real and virtual time is always going to be
231 fairly approximate, so ignore small variation.
232 When the guest is idle real and virtual time will be aligned in
233 the IO wait loop. */
234 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
235
236 static void icount_adjust(void)
237 {
238 int64_t cur_time;
239 int64_t cur_icount;
240 int64_t delta;
241 static int64_t last_delta;
242 /* If the VM is not running, then do nothing. */
243 if (!vm_running)
244 return;
245
246 cur_time = cpu_get_clock();
247 cur_icount = qemu_get_clock_ns(vm_clock);
248 delta = cur_icount - cur_time;
249 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
250 if (delta > 0
251 && last_delta + ICOUNT_WOBBLE < delta * 2
252 && icount_time_shift > 0) {
253 /* The guest is getting too far ahead. Slow time down. */
254 icount_time_shift--;
255 }
256 if (delta < 0
257 && last_delta - ICOUNT_WOBBLE > delta * 2
258 && icount_time_shift < MAX_ICOUNT_SHIFT) {
259 /* The guest is getting too far behind. Speed time up. */
260 icount_time_shift++;
261 }
262 last_delta = delta;
263 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
264 }
265
266 static void icount_adjust_rt(void * opaque)
267 {
268 qemu_mod_timer(icount_rt_timer,
269 qemu_get_clock_ms(rt_clock) + 1000);
270 icount_adjust();
271 }
272
273 static void icount_adjust_vm(void * opaque)
274 {
275 qemu_mod_timer(icount_vm_timer,
276 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
277 icount_adjust();
278 }
279
280 int64_t qemu_icount_round(int64_t count)
281 {
282 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
283 }
284
285 static struct qemu_alarm_timer alarm_timers[] = {
286 #ifndef _WIN32
287 #ifdef __linux__
288 {"dynticks", dynticks_start_timer,
289 dynticks_stop_timer, dynticks_rearm_timer, NULL},
290 /* HPET - if available - is preferred */
291 {"hpet", hpet_start_timer, hpet_stop_timer, NULL, NULL},
292 /* ...otherwise try RTC */
293 {"rtc", rtc_start_timer, rtc_stop_timer, NULL, NULL},
294 #endif
295 {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
296 #else
297 {"dynticks", win32_start_timer,
298 win32_stop_timer, win32_rearm_timer, NULL},
299 {"win32", win32_start_timer,
300 win32_stop_timer, NULL, NULL},
301 #endif
302 {NULL, }
303 };
304
305 static void show_available_alarms(void)
306 {
307 int i;
308
309 printf("Available alarm timers, in order of precedence:\n");
310 for (i = 0; alarm_timers[i].name; i++)
311 printf("%s\n", alarm_timers[i].name);
312 }
313
314 void configure_alarms(char const *opt)
315 {
316 int i;
317 int cur = 0;
318 int count = ARRAY_SIZE(alarm_timers) - 1;
319 char *arg;
320 char *name;
321 struct qemu_alarm_timer tmp;
322
323 if (!strcmp(opt, "?")) {
324 show_available_alarms();
325 exit(0);
326 }
327
328 arg = qemu_strdup(opt);
329
330 /* Reorder the array */
331 name = strtok(arg, ",");
332 while (name) {
333 for (i = 0; i < count && alarm_timers[i].name; i++) {
334 if (!strcmp(alarm_timers[i].name, name))
335 break;
336 }
337
338 if (i == count) {
339 fprintf(stderr, "Unknown clock %s\n", name);
340 goto next;
341 }
342
343 if (i < cur)
344 /* Ignore */
345 goto next;
346
347 /* Swap */
348 tmp = alarm_timers[i];
349 alarm_timers[i] = alarm_timers[cur];
350 alarm_timers[cur] = tmp;
351
352 cur++;
353 next:
354 name = strtok(NULL, ",");
355 }
356
357 qemu_free(arg);
358
359 if (cur) {
360 /* Disable remaining timers */
361 for (i = cur; i < count; i++)
362 alarm_timers[i].name = NULL;
363 } else {
364 show_available_alarms();
365 exit(1);
366 }
367 }
368
369 #define QEMU_NUM_CLOCKS 3
370
371 QEMUClock *rt_clock;
372 QEMUClock *vm_clock;
373 QEMUClock *host_clock;
374
375 static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
376
377 static QEMUClock *qemu_new_clock(int type)
378 {
379 QEMUClock *clock;
380 clock = qemu_mallocz(sizeof(QEMUClock));
381 clock->type = type;
382 clock->enabled = 1;
383 return clock;
384 }
385
386 void qemu_clock_enable(QEMUClock *clock, int enabled)
387 {
388 clock->enabled = enabled;
389 }
390
391 static int64_t vm_clock_warp_start;
392
393 static void icount_warp_rt(void *opaque)
394 {
395 if (vm_clock_warp_start == -1) {
396 return;
397 }
398
399 if (vm_running) {
400 int64_t clock = qemu_get_clock_ns(rt_clock);
401 int64_t warp_delta = clock - vm_clock_warp_start;
402 if (use_icount == 1) {
403 qemu_icount_bias += warp_delta;
404 } else {
405 /*
406 * In adaptive mode, do not let the vm_clock run too
407 * far ahead of real time.
408 */
409 int64_t cur_time = cpu_get_clock();
410 int64_t cur_icount = qemu_get_clock_ns(vm_clock);
411 int64_t delta = cur_time - cur_icount;
412 qemu_icount_bias += MIN(warp_delta, delta);
413 }
414 if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
415 qemu_get_clock_ns(vm_clock))) {
416 qemu_notify_event();
417 }
418 }
419 vm_clock_warp_start = -1;
420 }
421
422 void qemu_clock_warp(QEMUClock *clock)
423 {
424 int64_t deadline;
425
426 if (!clock->warp_timer) {
427 return;
428 }
429
430 /*
431 * There are too many global variables to make the "warp" behavior
432 * applicable to other clocks. But a clock argument removes the
433 * need for if statements all over the place.
434 */
435 assert(clock == vm_clock);
436
437 /*
438 * If the CPUs have been sleeping, advance the vm_clock timer now. This
439 * ensures that the deadline for the timer is computed correctly below.
440 * This also makes sure that the insn counter is synchronized before the
441 * CPU starts running, in case the CPU is woken by an event other than
442 * the earliest vm_clock timer.
443 */
444 icount_warp_rt(NULL);
445 if (!all_cpu_threads_idle() || !active_timers[clock->type]) {
446 qemu_del_timer(clock->warp_timer);
447 return;
448 }
449
450 vm_clock_warp_start = qemu_get_clock_ns(rt_clock);
451 deadline = qemu_next_deadline();
452 if (deadline > 0) {
453 /*
454 * Ensure the vm_clock proceeds even when the virtual CPU goes to
455 * sleep. Otherwise, the CPU might be waiting for a future timer
456 * interrupt to wake it up, but the interrupt never comes because
457 * the vCPU isn't running any insns and thus doesn't advance the
458 * vm_clock.
459 *
460 * An extreme solution for this problem would be to never let VCPUs
461 * sleep in icount mode if there is a pending vm_clock timer; rather
462 * time could just advance to the next vm_clock event. Instead, we
463 * do stop VCPUs and only advance vm_clock after some "real" time,
464 * (related to the time left until the next event) has passed. This
465 * rt_clock timer will do this. This avoids that the warps are too
466 * visible externally---for example, you will not be sending network
467 * packets continously instead of every 100ms.
468 */
469 qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline);
470 } else {
471 qemu_notify_event();
472 }
473 }
474
475 QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
476 QEMUTimerCB *cb, void *opaque)
477 {
478 QEMUTimer *ts;
479
480 ts = qemu_mallocz(sizeof(QEMUTimer));
481 ts->clock = clock;
482 ts->cb = cb;
483 ts->opaque = opaque;
484 ts->scale = scale;
485 return ts;
486 }
487
488 void qemu_free_timer(QEMUTimer *ts)
489 {
490 qemu_free(ts);
491 }
492
493 /* stop a timer, but do not dealloc it */
494 void qemu_del_timer(QEMUTimer *ts)
495 {
496 QEMUTimer **pt, *t;
497
498 /* NOTE: this code must be signal safe because
499 qemu_timer_expired() can be called from a signal. */
500 pt = &active_timers[ts->clock->type];
501 for(;;) {
502 t = *pt;
503 if (!t)
504 break;
505 if (t == ts) {
506 *pt = t->next;
507 break;
508 }
509 pt = &t->next;
510 }
511 }
512
513 /* modify the current timer so that it will be fired when current_time
514 >= expire_time. The corresponding callback will be called. */
515 static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
516 {
517 QEMUTimer **pt, *t;
518
519 qemu_del_timer(ts);
520
521 /* add the timer in the sorted list */
522 /* NOTE: this code must be signal safe because
523 qemu_timer_expired() can be called from a signal. */
524 pt = &active_timers[ts->clock->type];
525 for(;;) {
526 t = *pt;
527 if (!t)
528 break;
529 if (t->expire_time > expire_time)
530 break;
531 pt = &t->next;
532 }
533 ts->expire_time = expire_time;
534 ts->next = *pt;
535 *pt = ts;
536
537 /* Rearm if necessary */
538 if (pt == &active_timers[ts->clock->type]) {
539 if (!alarm_timer->pending) {
540 qemu_rearm_alarm_timer(alarm_timer);
541 }
542 /* Interrupt execution to force deadline recalculation. */
543 qemu_clock_warp(ts->clock);
544 if (use_icount) {
545 qemu_notify_event();
546 }
547 }
548 }
549
550 /* modify the current timer so that it will be fired when current_time
551 >= expire_time. The corresponding callback will be called. */
552 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
553 {
554 qemu_mod_timer_ns(ts, expire_time * ts->scale);
555 }
556
557 int qemu_timer_pending(QEMUTimer *ts)
558 {
559 QEMUTimer *t;
560 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
561 if (t == ts)
562 return 1;
563 }
564 return 0;
565 }
566
567 int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
568 {
569 if (!timer_head)
570 return 0;
571 return (timer_head->expire_time <= current_time * timer_head->scale);
572 }
573
574 static void qemu_run_timers(QEMUClock *clock)
575 {
576 QEMUTimer **ptimer_head, *ts;
577 int64_t current_time;
578
579 if (!clock->enabled)
580 return;
581
582 current_time = qemu_get_clock_ns(clock);
583 ptimer_head = &active_timers[clock->type];
584 for(;;) {
585 ts = *ptimer_head;
586 if (!ts || ts->expire_time > current_time)
587 break;
588 /* remove timer from the list before calling the callback */
589 *ptimer_head = ts->next;
590 ts->next = NULL;
591
592 /* run the callback (the timer list can be modified) */
593 ts->cb(ts->opaque);
594 }
595 }
596
597 int64_t qemu_get_clock_ns(QEMUClock *clock)
598 {
599 switch(clock->type) {
600 case QEMU_CLOCK_REALTIME:
601 return get_clock();
602 default:
603 case QEMU_CLOCK_VIRTUAL:
604 if (use_icount) {
605 return cpu_get_icount();
606 } else {
607 return cpu_get_clock();
608 }
609 case QEMU_CLOCK_HOST:
610 return get_clock_realtime();
611 }
612 }
613
614 void init_clocks(void)
615 {
616 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
617 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
618 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
619
620 rtc_clock = host_clock;
621 }
622
623 /* save a timer */
624 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
625 {
626 uint64_t expire_time;
627
628 if (qemu_timer_pending(ts)) {
629 expire_time = ts->expire_time;
630 } else {
631 expire_time = -1;
632 }
633 qemu_put_be64(f, expire_time);
634 }
635
636 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
637 {
638 uint64_t expire_time;
639
640 expire_time = qemu_get_be64(f);
641 if (expire_time != -1) {
642 qemu_mod_timer_ns(ts, expire_time);
643 } else {
644 qemu_del_timer(ts);
645 }
646 }
647
648 static const VMStateDescription vmstate_timers = {
649 .name = "timer",
650 .version_id = 2,
651 .minimum_version_id = 1,
652 .minimum_version_id_old = 1,
653 .fields = (VMStateField []) {
654 VMSTATE_INT64(cpu_ticks_offset, TimersState),
655 VMSTATE_INT64(dummy, TimersState),
656 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
657 VMSTATE_END_OF_LIST()
658 }
659 };
660
661 void configure_icount(const char *option)
662 {
663 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
664 if (!option)
665 return;
666
667 #ifdef CONFIG_IOTHREAD
668 vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL);
669 #endif
670
671 if (strcmp(option, "auto") != 0) {
672 icount_time_shift = strtol(option, NULL, 0);
673 use_icount = 1;
674 return;
675 }
676
677 use_icount = 2;
678
679 /* 125MIPS seems a reasonable initial guess at the guest speed.
680 It will be corrected fairly quickly anyway. */
681 icount_time_shift = 3;
682
683 /* Have both realtime and virtual time triggers for speed adjustment.
684 The realtime trigger catches emulated time passing too slowly,
685 the virtual time trigger catches emulated time passing too fast.
686 Realtime triggers occur even when idle, so use them less frequently
687 than VM triggers. */
688 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
689 qemu_mod_timer(icount_rt_timer,
690 qemu_get_clock_ms(rt_clock) + 1000);
691 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
692 qemu_mod_timer(icount_vm_timer,
693 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
694 }
695
696 void qemu_run_all_timers(void)
697 {
698 alarm_timer->pending = 0;
699
700 /* rearm timer, if not periodic */
701 if (alarm_timer->expired) {
702 alarm_timer->expired = 0;
703 qemu_rearm_alarm_timer(alarm_timer);
704 }
705
706 /* vm time timers */
707 if (vm_running) {
708 qemu_run_timers(vm_clock);
709 }
710
711 qemu_run_timers(rt_clock);
712 qemu_run_timers(host_clock);
713 }
714
715 static int64_t qemu_next_alarm_deadline(void);
716
717 #ifdef _WIN32
718 static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
719 #else
720 static void host_alarm_handler(int host_signum)
721 #endif
722 {
723 struct qemu_alarm_timer *t = alarm_timer;
724 if (!t)
725 return;
726
727 #if 0
728 #define DISP_FREQ 1000
729 {
730 static int64_t delta_min = INT64_MAX;
731 static int64_t delta_max, delta_cum, last_clock, delta, ti;
732 static int count;
733 ti = qemu_get_clock_ns(vm_clock);
734 if (last_clock != 0) {
735 delta = ti - last_clock;
736 if (delta < delta_min)
737 delta_min = delta;
738 if (delta > delta_max)
739 delta_max = delta;
740 delta_cum += delta;
741 if (++count == DISP_FREQ) {
742 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
743 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
744 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
745 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
746 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
747 count = 0;
748 delta_min = INT64_MAX;
749 delta_max = 0;
750 delta_cum = 0;
751 }
752 }
753 last_clock = ti;
754 }
755 #endif
756 if (alarm_has_dynticks(t) ||
757 qemu_next_alarm_deadline () <= 0) {
758 t->expired = alarm_has_dynticks(t);
759 t->pending = 1;
760 qemu_notify_event();
761 }
762 }
763
764 int64_t qemu_next_deadline(void)
765 {
766 /* To avoid problems with overflow limit this to 2^32. */
767 int64_t delta = INT32_MAX;
768
769 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
770 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
771 qemu_get_clock_ns(vm_clock);
772 }
773 if (active_timers[QEMU_CLOCK_HOST]) {
774 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
775 qemu_get_clock_ns(host_clock);
776 if (hdelta < delta)
777 delta = hdelta;
778 }
779
780 if (delta < 0)
781 delta = 0;
782
783 return delta;
784 }
785
786 static int64_t qemu_next_alarm_deadline(void)
787 {
788 int64_t delta;
789 int64_t rtdelta;
790
791 if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
792 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
793 qemu_get_clock_ns(vm_clock);
794 } else {
795 delta = INT32_MAX;
796 }
797 if (active_timers[QEMU_CLOCK_HOST]) {
798 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
799 qemu_get_clock_ns(host_clock);
800 if (hdelta < delta)
801 delta = hdelta;
802 }
803 if (active_timers[QEMU_CLOCK_REALTIME]) {
804 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
805 qemu_get_clock_ns(rt_clock));
806 if (rtdelta < delta)
807 delta = rtdelta;
808 }
809
810 return delta;
811 }
812
813 #if defined(__linux__)
814
815 #define RTC_FREQ 1024
816
817 static void enable_sigio_timer(int fd)
818 {
819 struct sigaction act;
820
821 /* timer signal */
822 sigfillset(&act.sa_mask);
823 act.sa_flags = 0;
824 act.sa_handler = host_alarm_handler;
825
826 sigaction(SIGIO, &act, NULL);
827 fcntl_setfl(fd, O_ASYNC);
828 fcntl(fd, F_SETOWN, getpid());
829 }
830
831 static int hpet_start_timer(struct qemu_alarm_timer *t)
832 {
833 struct hpet_info info;
834 int r, fd;
835
836 fd = qemu_open("/dev/hpet", O_RDONLY);
837 if (fd < 0)
838 return -1;
839
840 /* Set frequency */
841 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
842 if (r < 0) {
843 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
844 "error, but for better emulation accuracy type:\n"
845 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
846 goto fail;
847 }
848
849 /* Check capabilities */
850 r = ioctl(fd, HPET_INFO, &info);
851 if (r < 0)
852 goto fail;
853
854 /* Enable periodic mode */
855 r = ioctl(fd, HPET_EPI, 0);
856 if (info.hi_flags && (r < 0))
857 goto fail;
858
859 /* Enable interrupt */
860 r = ioctl(fd, HPET_IE_ON, 0);
861 if (r < 0)
862 goto fail;
863
864 enable_sigio_timer(fd);
865 t->priv = (void *)(long)fd;
866
867 return 0;
868 fail:
869 close(fd);
870 return -1;
871 }
872
873 static void hpet_stop_timer(struct qemu_alarm_timer *t)
874 {
875 int fd = (long)t->priv;
876
877 close(fd);
878 }
879
880 static int rtc_start_timer(struct qemu_alarm_timer *t)
881 {
882 int rtc_fd;
883 unsigned long current_rtc_freq = 0;
884
885 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
886 if (rtc_fd < 0)
887 return -1;
888 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
889 if (current_rtc_freq != RTC_FREQ &&
890 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
891 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
892 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
893 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
894 goto fail;
895 }
896 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
897 fail:
898 close(rtc_fd);
899 return -1;
900 }
901
902 enable_sigio_timer(rtc_fd);
903
904 t->priv = (void *)(long)rtc_fd;
905
906 return 0;
907 }
908
909 static void rtc_stop_timer(struct qemu_alarm_timer *t)
910 {
911 int rtc_fd = (long)t->priv;
912
913 close(rtc_fd);
914 }
915
916 static int dynticks_start_timer(struct qemu_alarm_timer *t)
917 {
918 struct sigevent ev;
919 timer_t host_timer;
920 struct sigaction act;
921
922 sigfillset(&act.sa_mask);
923 act.sa_flags = 0;
924 act.sa_handler = host_alarm_handler;
925
926 sigaction(SIGALRM, &act, NULL);
927
928 /*
929 * Initialize ev struct to 0 to avoid valgrind complaining
930 * about uninitialized data in timer_create call
931 */
932 memset(&ev, 0, sizeof(ev));
933 ev.sigev_value.sival_int = 0;
934 ev.sigev_notify = SIGEV_SIGNAL;
935 ev.sigev_signo = SIGALRM;
936
937 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
938 perror("timer_create");
939
940 /* disable dynticks */
941 fprintf(stderr, "Dynamic Ticks disabled\n");
942
943 return -1;
944 }
945
946 t->priv = (void *)(long)host_timer;
947
948 return 0;
949 }
950
951 static void dynticks_stop_timer(struct qemu_alarm_timer *t)
952 {
953 timer_t host_timer = (timer_t)(long)t->priv;
954
955 timer_delete(host_timer);
956 }
957
958 static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
959 {
960 timer_t host_timer = (timer_t)(long)t->priv;
961 struct itimerspec timeout;
962 int64_t nearest_delta_ns = INT64_MAX;
963 int64_t current_ns;
964
965 assert(alarm_has_dynticks(t));
966 if (!active_timers[QEMU_CLOCK_REALTIME] &&
967 !active_timers[QEMU_CLOCK_VIRTUAL] &&
968 !active_timers[QEMU_CLOCK_HOST])
969 return;
970
971 nearest_delta_ns = qemu_next_alarm_deadline();
972 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
973 nearest_delta_ns = MIN_TIMER_REARM_NS;
974
975 /* check whether a timer is already running */
976 if (timer_gettime(host_timer, &timeout)) {
977 perror("gettime");
978 fprintf(stderr, "Internal timer error: aborting\n");
979 exit(1);
980 }
981 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
982 if (current_ns && current_ns <= nearest_delta_ns)
983 return;
984
985 timeout.it_interval.tv_sec = 0;
986 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
987 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
988 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
989 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
990 perror("settime");
991 fprintf(stderr, "Internal timer error: aborting\n");
992 exit(1);
993 }
994 }
995
996 #endif /* defined(__linux__) */
997
998 #if !defined(_WIN32)
999
1000 static int unix_start_timer(struct qemu_alarm_timer *t)
1001 {
1002 struct sigaction act;
1003 struct itimerval itv;
1004 int err;
1005
1006 /* timer signal */
1007 sigfillset(&act.sa_mask);
1008 act.sa_flags = 0;
1009 act.sa_handler = host_alarm_handler;
1010
1011 sigaction(SIGALRM, &act, NULL);
1012
1013 itv.it_interval.tv_sec = 0;
1014 /* for i386 kernel 2.6 to get 1 ms */
1015 itv.it_interval.tv_usec = 999;
1016 itv.it_value.tv_sec = 0;
1017 itv.it_value.tv_usec = 10 * 1000;
1018
1019 err = setitimer(ITIMER_REAL, &itv, NULL);
1020 if (err)
1021 return -1;
1022
1023 return 0;
1024 }
1025
1026 static void unix_stop_timer(struct qemu_alarm_timer *t)
1027 {
1028 struct itimerval itv;
1029
1030 memset(&itv, 0, sizeof(itv));
1031 setitimer(ITIMER_REAL, &itv, NULL);
1032 }
1033
1034 #endif /* !defined(_WIN32) */
1035
1036
1037 #ifdef _WIN32
1038
1039 static int win32_start_timer(struct qemu_alarm_timer *t)
1040 {
1041 HANDLE hTimer;
1042 BOOLEAN success;
1043
1044 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
1045 is zero) that has already expired, the timer is not updated. Since
1046 creating a new timer is relatively expensive, set a bogus one-hour
1047 interval in the dynticks case. */
1048 success = CreateTimerQueueTimer(&hTimer,
1049 NULL,
1050 host_alarm_handler,
1051 t,
1052 1,
1053 alarm_has_dynticks(t) ? 3600000 : 1,
1054 WT_EXECUTEINTIMERTHREAD);
1055
1056 if (!success) {
1057 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1058 GetLastError());
1059 return -1;
1060 }
1061
1062 t->priv = (PVOID) hTimer;
1063 return 0;
1064 }
1065
1066 static void win32_stop_timer(struct qemu_alarm_timer *t)
1067 {
1068 HANDLE hTimer = t->priv;
1069
1070 if (hTimer) {
1071 DeleteTimerQueueTimer(NULL, hTimer, NULL);
1072 }
1073 }
1074
1075 static void win32_rearm_timer(struct qemu_alarm_timer *t)
1076 {
1077 HANDLE hTimer = t->priv;
1078 int nearest_delta_ms;
1079 BOOLEAN success;
1080
1081 assert(alarm_has_dynticks(t));
1082 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1083 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1084 !active_timers[QEMU_CLOCK_HOST])
1085 return;
1086
1087 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1088 if (nearest_delta_ms < 1) {
1089 nearest_delta_ms = 1;
1090 }
1091 success = ChangeTimerQueueTimer(NULL,
1092 hTimer,
1093 nearest_delta_ms,
1094 3600000);
1095
1096 if (!success) {
1097 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
1098 GetLastError());
1099 exit(-1);
1100 }
1101
1102 }
1103
1104 #endif /* _WIN32 */
1105
1106 static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1107 {
1108 if (running)
1109 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1110 }
1111
1112 int init_timer_alarm(void)
1113 {
1114 struct qemu_alarm_timer *t = NULL;
1115 int i, err = -1;
1116
1117 for (i = 0; alarm_timers[i].name; i++) {
1118 t = &alarm_timers[i];
1119
1120 err = t->start(t);
1121 if (!err)
1122 break;
1123 }
1124
1125 if (err) {
1126 err = -ENOENT;
1127 goto fail;
1128 }
1129
1130 /* first event is at time 0 */
1131 t->pending = 1;
1132 alarm_timer = t;
1133 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1134
1135 return 0;
1136
1137 fail:
1138 return err;
1139 }
1140
1141 void quit_timers(void)
1142 {
1143 struct qemu_alarm_timer *t = alarm_timer;
1144 alarm_timer = NULL;
1145 t->stop(t);
1146 }
1147
1148 int qemu_calculate_timeout(void)
1149 {
1150 int timeout;
1151 int64_t add;
1152 int64_t delta;
1153
1154 /* When using icount, making forward progress with qemu_icount when the
1155 guest CPU is idle is critical. We only use the static io-thread timeout
1156 for non icount runs. */
1157 if (!use_icount || !vm_running) {
1158 return 5000;
1159 }
1160
1161 /* Advance virtual time to the next event. */
1162 delta = qemu_icount_delta();
1163 if (delta > 0) {
1164 /* If virtual time is ahead of real time then just
1165 wait for IO. */
1166 timeout = (delta + 999999) / 1000000;
1167 } else {
1168 /* Wait for either IO to occur or the next
1169 timer event. */
1170 add = qemu_next_deadline();
1171 /* We advance the timer before checking for IO.
1172 Limit the amount we advance so that early IO
1173 activity won't get the guest too far ahead. */
1174 if (add > 10000000)
1175 add = 10000000;
1176 delta += add;
1177 qemu_icount += qemu_icount_round (add);
1178 timeout = delta / 1000000;
1179 if (timeout < 0)
1180 timeout = 0;
1181 }
1182
1183 return timeout;
1184 }
1185