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