blob: ec8b4466dabd70d5ab3616492a7f4f64853950b5 [file] [log] [blame]
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001/*
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>
Juergen Lock44459342010-03-25 22:35:03 +010038#ifdef __FreeBSD__
39#include <sys/param.h>
40#endif
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010041
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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010056#include "qemu-timer.h"
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010057
58/* Conversion factor from emulated instructions to virtual clock ticks. */
Blue Swirl29e922b2010-03-29 19:24:00 +000059int icount_time_shift;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010060/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
61#define MAX_ICOUNT_SHIFT 10
62/* Compensate for varying guest execution speed. */
Blue Swirl29e922b2010-03-29 19:24:00 +000063int64_t qemu_icount_bias;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010064static QEMUTimer *icount_rt_timer;
65static QEMUTimer *icount_vm_timer;
66
Paolo Bonzinidb1a4972010-03-10 11:38:55 +010067/***********************************************************/
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
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100113static int64_t qemu_icount_delta(void)
114{
Paolo Bonzinic9f73832011-02-21 09:51:23 +0100115 if (use_icount == 1) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100116 /* 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}
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100124
125/* enable cpu_get_ticks() */
126void 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. */
137void 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
153struct QEMUClock {
154 int type;
155 int enabled;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100156};
157
158struct QEMUTimer {
159 QEMUClock *clock;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100160 int64_t expire_time; /* in nanoseconds */
161 int scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100162 QEMUTimerCB *cb;
163 void *opaque;
164 struct QEMUTimer *next;
165};
166
167struct qemu_alarm_timer {
168 char const *name;
169 int (*start)(struct qemu_alarm_timer *t);
170 void (*stop)(struct qemu_alarm_timer *t);
171 void (*rearm)(struct qemu_alarm_timer *t);
172 void *priv;
173
174 char expired;
175 char pending;
176};
177
178static struct qemu_alarm_timer *alarm_timer;
179
180int qemu_alarm_pending(void)
181{
182 return alarm_timer->pending;
183}
184
185static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
186{
187 return !!t->rearm;
188}
189
190static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
191{
192 if (!alarm_has_dynticks(t))
193 return;
194
195 t->rearm(t);
196}
197
Paolo Bonzini9c132462011-02-03 14:48:59 +0100198/* TODO: MIN_TIMER_REARM_NS should be optimized */
199#define MIN_TIMER_REARM_NS 250000
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100200
201#ifdef _WIN32
202
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100203static int win32_start_timer(struct qemu_alarm_timer *t);
204static void win32_stop_timer(struct qemu_alarm_timer *t);
205static void win32_rearm_timer(struct qemu_alarm_timer *t);
206
207#else
208
209static int unix_start_timer(struct qemu_alarm_timer *t);
210static void unix_stop_timer(struct qemu_alarm_timer *t);
211
212#ifdef __linux__
213
214static int dynticks_start_timer(struct qemu_alarm_timer *t);
215static void dynticks_stop_timer(struct qemu_alarm_timer *t);
216static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
217
218static int hpet_start_timer(struct qemu_alarm_timer *t);
219static void hpet_stop_timer(struct qemu_alarm_timer *t);
220
221static int rtc_start_timer(struct qemu_alarm_timer *t);
222static void rtc_stop_timer(struct qemu_alarm_timer *t);
223
224#endif /* __linux__ */
225
226#endif /* _WIN32 */
227
228/* Correlation between real and virtual time is always going to be
229 fairly approximate, so ignore small variation.
230 When the guest is idle real and virtual time will be aligned in
231 the IO wait loop. */
232#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
233
234static void icount_adjust(void)
235{
236 int64_t cur_time;
237 int64_t cur_icount;
238 int64_t delta;
239 static int64_t last_delta;
240 /* If the VM is not running, then do nothing. */
241 if (!vm_running)
242 return;
243
244 cur_time = cpu_get_clock();
Paolo Bonzini74475452011-03-11 16:47:48 +0100245 cur_icount = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100246 delta = cur_icount - cur_time;
247 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
248 if (delta > 0
249 && last_delta + ICOUNT_WOBBLE < delta * 2
250 && icount_time_shift > 0) {
251 /* The guest is getting too far ahead. Slow time down. */
252 icount_time_shift--;
253 }
254 if (delta < 0
255 && last_delta - ICOUNT_WOBBLE > delta * 2
256 && icount_time_shift < MAX_ICOUNT_SHIFT) {
257 /* The guest is getting too far behind. Speed time up. */
258 icount_time_shift++;
259 }
260 last_delta = delta;
261 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
262}
263
264static void icount_adjust_rt(void * opaque)
265{
266 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100267 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100268 icount_adjust();
269}
270
271static void icount_adjust_vm(void * opaque)
272{
273 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100274 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100275 icount_adjust();
276}
277
278int64_t qemu_icount_round(int64_t count)
279{
280 return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
281}
282
283static struct qemu_alarm_timer alarm_timers[] = {
284#ifndef _WIN32
285#ifdef __linux__
286 {"dynticks", dynticks_start_timer,
287 dynticks_stop_timer, dynticks_rearm_timer, NULL},
288 /* HPET - if available - is preferred */
289 {"hpet", hpet_start_timer, hpet_stop_timer, NULL, NULL},
290 /* ...otherwise try RTC */
291 {"rtc", rtc_start_timer, rtc_stop_timer, NULL, NULL},
292#endif
293 {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
294#else
295 {"dynticks", win32_start_timer,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100296 win32_stop_timer, win32_rearm_timer, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100297 {"win32", win32_start_timer,
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100298 win32_stop_timer, NULL, NULL},
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100299#endif
300 {NULL, }
301};
302
303static void show_available_alarms(void)
304{
305 int i;
306
307 printf("Available alarm timers, in order of precedence:\n");
308 for (i = 0; alarm_timers[i].name; i++)
309 printf("%s\n", alarm_timers[i].name);
310}
311
312void configure_alarms(char const *opt)
313{
314 int i;
315 int cur = 0;
316 int count = ARRAY_SIZE(alarm_timers) - 1;
317 char *arg;
318 char *name;
319 struct qemu_alarm_timer tmp;
320
321 if (!strcmp(opt, "?")) {
322 show_available_alarms();
323 exit(0);
324 }
325
326 arg = qemu_strdup(opt);
327
328 /* Reorder the array */
329 name = strtok(arg, ",");
330 while (name) {
331 for (i = 0; i < count && alarm_timers[i].name; i++) {
332 if (!strcmp(alarm_timers[i].name, name))
333 break;
334 }
335
336 if (i == count) {
337 fprintf(stderr, "Unknown clock %s\n", name);
338 goto next;
339 }
340
341 if (i < cur)
342 /* Ignore */
343 goto next;
344
345 /* Swap */
346 tmp = alarm_timers[i];
347 alarm_timers[i] = alarm_timers[cur];
348 alarm_timers[cur] = tmp;
349
350 cur++;
351next:
352 name = strtok(NULL, ",");
353 }
354
355 qemu_free(arg);
356
357 if (cur) {
358 /* Disable remaining timers */
359 for (i = cur; i < count; i++)
360 alarm_timers[i].name = NULL;
361 } else {
362 show_available_alarms();
363 exit(1);
364 }
365}
366
367#define QEMU_NUM_CLOCKS 3
368
369QEMUClock *rt_clock;
370QEMUClock *vm_clock;
371QEMUClock *host_clock;
372
373static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
374
375static QEMUClock *qemu_new_clock(int type)
376{
377 QEMUClock *clock;
378 clock = qemu_mallocz(sizeof(QEMUClock));
379 clock->type = type;
380 clock->enabled = 1;
381 return clock;
382}
383
384void qemu_clock_enable(QEMUClock *clock, int enabled)
385{
386 clock->enabled = enabled;
387}
388
Paolo Bonzini4a998742011-03-11 16:33:58 +0100389QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
390 QEMUTimerCB *cb, void *opaque)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100391{
392 QEMUTimer *ts;
393
394 ts = qemu_mallocz(sizeof(QEMUTimer));
395 ts->clock = clock;
396 ts->cb = cb;
397 ts->opaque = opaque;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100398 ts->scale = scale;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100399 return ts;
400}
401
402void qemu_free_timer(QEMUTimer *ts)
403{
404 qemu_free(ts);
405}
406
407/* stop a timer, but do not dealloc it */
408void qemu_del_timer(QEMUTimer *ts)
409{
410 QEMUTimer **pt, *t;
411
412 /* NOTE: this code must be signal safe because
413 qemu_timer_expired() can be called from a signal. */
414 pt = &active_timers[ts->clock->type];
415 for(;;) {
416 t = *pt;
417 if (!t)
418 break;
419 if (t == ts) {
420 *pt = t->next;
421 break;
422 }
423 pt = &t->next;
424 }
425}
426
427/* modify the current timer so that it will be fired when current_time
428 >= expire_time. The corresponding callback will be called. */
Paolo Bonzini4a998742011-03-11 16:33:58 +0100429static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100430{
431 QEMUTimer **pt, *t;
432
433 qemu_del_timer(ts);
434
435 /* add the timer in the sorted list */
436 /* NOTE: this code must be signal safe because
437 qemu_timer_expired() can be called from a signal. */
438 pt = &active_timers[ts->clock->type];
439 for(;;) {
440 t = *pt;
441 if (!t)
442 break;
443 if (t->expire_time > expire_time)
444 break;
445 pt = &t->next;
446 }
447 ts->expire_time = expire_time;
448 ts->next = *pt;
449 *pt = ts;
450
451 /* Rearm if necessary */
452 if (pt == &active_timers[ts->clock->type]) {
453 if (!alarm_timer->pending) {
454 qemu_rearm_alarm_timer(alarm_timer);
455 }
456 /* Interrupt execution to force deadline recalculation. */
457 if (use_icount)
458 qemu_notify_event();
459 }
460}
461
Paolo Bonzini4a998742011-03-11 16:33:58 +0100462/* modify the current timer so that it will be fired when current_time
463 >= expire_time. The corresponding callback will be called. */
464void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
465{
466 qemu_mod_timer_ns(ts, expire_time * ts->scale);
467}
468
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100469int qemu_timer_pending(QEMUTimer *ts)
470{
471 QEMUTimer *t;
472 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
473 if (t == ts)
474 return 1;
475 }
476 return 0;
477}
478
479int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
480{
481 if (!timer_head)
482 return 0;
Paolo Bonzini4a998742011-03-11 16:33:58 +0100483 return (timer_head->expire_time <= current_time * timer_head->scale);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100484}
485
486static void qemu_run_timers(QEMUClock *clock)
487{
488 QEMUTimer **ptimer_head, *ts;
489 int64_t current_time;
490
491 if (!clock->enabled)
492 return;
493
Paolo Bonzini4a998742011-03-11 16:33:58 +0100494 current_time = qemu_get_clock_ns(clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100495 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;
503
504 /* run the callback (the timer list can be modified) */
505 ts->cb(ts->opaque);
506 }
507}
508
509int64_t qemu_get_clock(QEMUClock *clock)
510{
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();
520 }
521 case QEMU_CLOCK_HOST:
522 return get_clock_realtime();
523 }
524}
525
526int64_t qemu_get_clock_ns(QEMUClock *clock)
527{
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();
537 }
538 case QEMU_CLOCK_HOST:
539 return get_clock_realtime();
540 }
541}
542
543void init_clocks(void)
544{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100545 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);
548
549 rtc_clock = host_clock;
550}
551
552/* save a timer */
553void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
554{
555 uint64_t expire_time;
556
557 if (qemu_timer_pending(ts)) {
558 expire_time = ts->expire_time;
559 } else {
560 expire_time = -1;
561 }
562 qemu_put_be64(f, expire_time);
563}
564
565void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
566{
567 uint64_t expire_time;
568
569 expire_time = qemu_get_be64(f);
570 if (expire_time != -1) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100571 qemu_mod_timer_ns(ts, expire_time);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100572 } else {
573 qemu_del_timer(ts);
574 }
575}
576
577static 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()
587 }
588};
589
590void configure_icount(const char *option)
591{
Alex Williamson0be71e32010-06-25 11:09:07 -0600592 vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100593 if (!option)
594 return;
595
596 if (strcmp(option, "auto") != 0) {
597 icount_time_shift = strtol(option, NULL, 0);
598 use_icount = 1;
599 return;
600 }
601
602 use_icount = 2;
603
604 /* 125MIPS seems a reasonable initial guess at the guest speed.
605 It will be corrected fairly quickly anyway. */
606 icount_time_shift = 3;
607
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. */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100613 icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100614 qemu_mod_timer(icount_rt_timer,
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100615 qemu_get_clock_ms(rt_clock) + 1000);
Paolo Bonzini74475452011-03-11 16:47:48 +0100616 icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100617 qemu_mod_timer(icount_vm_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100618 qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100619}
620
621void qemu_run_all_timers(void)
622{
Paolo Bonzinica5a2a42010-03-19 11:30:35 +0100623 alarm_timer->pending = 0;
624
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100625 /* rearm timer, if not periodic */
626 if (alarm_timer->expired) {
627 alarm_timer->expired = 0;
628 qemu_rearm_alarm_timer(alarm_timer);
629 }
630
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100631 /* vm time timers */
632 if (vm_running) {
633 qemu_run_timers(vm_clock);
634 }
635
636 qemu_run_timers(rt_clock);
637 qemu_run_timers(host_clock);
638}
639
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100640static int64_t qemu_next_alarm_deadline(void);
641
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100642#ifdef _WIN32
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100643static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100644#else
645static void host_alarm_handler(int host_signum)
646#endif
647{
648 struct qemu_alarm_timer *t = alarm_timer;
649 if (!t)
650 return;
651
652#if 0
653#define DISP_FREQ 1000
654 {
655 static int64_t delta_min = INT64_MAX;
656 static int64_t delta_max, delta_cum, last_clock, delta, ti;
657 static int count;
Paolo Bonzini74475452011-03-11 16:47:48 +0100658 ti = qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100659 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;
676 }
677 }
678 last_clock = ti;
679 }
680#endif
681 if (alarm_has_dynticks(t) ||
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100682 qemu_next_alarm_deadline () <= 0) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100683 t->expired = alarm_has_dynticks(t);
684 t->pending = 1;
685 qemu_notify_event();
686 }
687}
688
689int64_t qemu_next_deadline(void)
690{
691 /* To avoid problems with overflow limit this to 2^32. */
692 int64_t delta = INT32_MAX;
693
694 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
695 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100696 qemu_get_clock_ns(vm_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100697 }
698 if (active_timers[QEMU_CLOCK_HOST]) {
699 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100700 qemu_get_clock_ns(host_clock);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100701 if (hdelta < delta)
702 delta = hdelta;
703 }
704
705 if (delta < 0)
706 delta = 0;
707
708 return delta;
709}
710
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100711static int64_t qemu_next_alarm_deadline(void)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100712{
713 int64_t delta;
714 int64_t rtdelta;
715
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100716 if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
717 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
Paolo Bonzini74475452011-03-11 16:47:48 +0100718 qemu_get_clock_ns(vm_clock);
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100719 } else {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100720 delta = INT32_MAX;
Paolo Bonzini6ad0a1e2011-02-03 14:49:00 +0100721 }
722 if (active_timers[QEMU_CLOCK_HOST]) {
723 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
724 qemu_get_clock_ns(host_clock);
725 if (hdelta < delta)
726 delta = hdelta;
727 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100728 if (active_timers[QEMU_CLOCK_REALTIME]) {
Paolo Bonzini4a998742011-03-11 16:33:58 +0100729 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
Paolo Bonzini9c132462011-02-03 14:48:59 +0100730 qemu_get_clock_ns(rt_clock));
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100731 if (rtdelta < delta)
732 delta = rtdelta;
733 }
734
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100735 return delta;
736}
737
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100738#if defined(__linux__)
739
740#define RTC_FREQ 1024
741
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100742static void enable_sigio_timer(int fd)
743{
744 struct sigaction act;
745
746 /* timer signal */
747 sigfillset(&act.sa_mask);
748 act.sa_flags = 0;
749 act.sa_handler = host_alarm_handler;
750
751 sigaction(SIGIO, &act, NULL);
752 fcntl_setfl(fd, O_ASYNC);
753 fcntl(fd, F_SETOWN, getpid());
754}
755
756static int hpet_start_timer(struct qemu_alarm_timer *t)
757{
758 struct hpet_info info;
759 int r, fd;
760
761 fd = qemu_open("/dev/hpet", O_RDONLY);
762 if (fd < 0)
763 return -1;
764
765 /* Set frequency */
766 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
767 if (r < 0) {
768 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
769 "error, but for better emulation accuracy type:\n"
770 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
771 goto fail;
772 }
773
774 /* Check capabilities */
775 r = ioctl(fd, HPET_INFO, &info);
776 if (r < 0)
777 goto fail;
778
779 /* Enable periodic mode */
780 r = ioctl(fd, HPET_EPI, 0);
781 if (info.hi_flags && (r < 0))
782 goto fail;
783
784 /* Enable interrupt */
785 r = ioctl(fd, HPET_IE_ON, 0);
786 if (r < 0)
787 goto fail;
788
789 enable_sigio_timer(fd);
790 t->priv = (void *)(long)fd;
791
792 return 0;
793fail:
794 close(fd);
795 return -1;
796}
797
798static void hpet_stop_timer(struct qemu_alarm_timer *t)
799{
800 int fd = (long)t->priv;
801
802 close(fd);
803}
804
805static int rtc_start_timer(struct qemu_alarm_timer *t)
806{
807 int rtc_fd;
808 unsigned long current_rtc_freq = 0;
809
810 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
811 if (rtc_fd < 0)
812 return -1;
813 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
814 if (current_rtc_freq != RTC_FREQ &&
815 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
816 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
817 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
818 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
819 goto fail;
820 }
821 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
822 fail:
823 close(rtc_fd);
824 return -1;
825 }
826
827 enable_sigio_timer(rtc_fd);
828
829 t->priv = (void *)(long)rtc_fd;
830
831 return 0;
832}
833
834static void rtc_stop_timer(struct qemu_alarm_timer *t)
835{
836 int rtc_fd = (long)t->priv;
837
838 close(rtc_fd);
839}
840
841static int dynticks_start_timer(struct qemu_alarm_timer *t)
842{
843 struct sigevent ev;
844 timer_t host_timer;
845 struct sigaction act;
846
847 sigfillset(&act.sa_mask);
848 act.sa_flags = 0;
849 act.sa_handler = host_alarm_handler;
850
851 sigaction(SIGALRM, &act, NULL);
852
853 /*
854 * Initialize ev struct to 0 to avoid valgrind complaining
855 * about uninitialized data in timer_create call
856 */
857 memset(&ev, 0, sizeof(ev));
858 ev.sigev_value.sival_int = 0;
859 ev.sigev_notify = SIGEV_SIGNAL;
860 ev.sigev_signo = SIGALRM;
861
862 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
863 perror("timer_create");
864
865 /* disable dynticks */
866 fprintf(stderr, "Dynamic Ticks disabled\n");
867
868 return -1;
869 }
870
871 t->priv = (void *)(long)host_timer;
872
873 return 0;
874}
875
876static void dynticks_stop_timer(struct qemu_alarm_timer *t)
877{
878 timer_t host_timer = (timer_t)(long)t->priv;
879
880 timer_delete(host_timer);
881}
882
883static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
884{
885 timer_t host_timer = (timer_t)(long)t->priv;
886 struct itimerspec timeout;
Paolo Bonzini9c132462011-02-03 14:48:59 +0100887 int64_t nearest_delta_ns = INT64_MAX;
888 int64_t current_ns;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100889
890 assert(alarm_has_dynticks(t));
891 if (!active_timers[QEMU_CLOCK_REALTIME] &&
892 !active_timers[QEMU_CLOCK_VIRTUAL] &&
893 !active_timers[QEMU_CLOCK_HOST])
894 return;
895
Paolo Bonzini4c3d45e2011-02-03 14:49:01 +0100896 nearest_delta_ns = qemu_next_alarm_deadline();
897 if (nearest_delta_ns < MIN_TIMER_REARM_NS)
898 nearest_delta_ns = MIN_TIMER_REARM_NS;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100899
900 /* check whether a timer is already running */
901 if (timer_gettime(host_timer, &timeout)) {
902 perror("gettime");
903 fprintf(stderr, "Internal timer error: aborting\n");
904 exit(1);
905 }
Paolo Bonzini9c132462011-02-03 14:48:59 +0100906 current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
907 if (current_ns && current_ns <= nearest_delta_ns)
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100908 return;
909
910 timeout.it_interval.tv_sec = 0;
911 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
Paolo Bonzini9c132462011-02-03 14:48:59 +0100912 timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
913 timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100914 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
915 perror("settime");
916 fprintf(stderr, "Internal timer error: aborting\n");
917 exit(1);
918 }
919}
920
921#endif /* defined(__linux__) */
922
Stefan Weilf26e5a52011-02-04 22:01:32 +0100923#if !defined(_WIN32)
924
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100925static int unix_start_timer(struct qemu_alarm_timer *t)
926{
927 struct sigaction act;
928 struct itimerval itv;
929 int err;
930
931 /* timer signal */
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 itv.it_interval.tv_sec = 0;
939 /* for i386 kernel 2.6 to get 1 ms */
940 itv.it_interval.tv_usec = 999;
941 itv.it_value.tv_sec = 0;
942 itv.it_value.tv_usec = 10 * 1000;
943
944 err = setitimer(ITIMER_REAL, &itv, NULL);
945 if (err)
946 return -1;
947
948 return 0;
949}
950
951static void unix_stop_timer(struct qemu_alarm_timer *t)
952{
953 struct itimerval itv;
954
955 memset(&itv, 0, sizeof(itv));
956 setitimer(ITIMER_REAL, &itv, NULL);
957}
958
959#endif /* !defined(_WIN32) */
960
961
962#ifdef _WIN32
963
964static int win32_start_timer(struct qemu_alarm_timer *t)
965{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100966 HANDLE hTimer;
967 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100968
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100969 /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
970 is zero) that has already expired, the timer is not updated. Since
971 creating a new timer is relatively expensive, set a bogus one-hour
972 interval in the dynticks case. */
973 success = CreateTimerQueueTimer(&hTimer,
974 NULL,
975 host_alarm_handler,
976 t,
977 1,
978 alarm_has_dynticks(t) ? 3600000 : 1,
979 WT_EXECUTEINTIMERTHREAD);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100980
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100981 if (!success) {
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100982 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
983 GetLastError());
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100984 return -1;
985 }
986
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100987 t->priv = (PVOID) hTimer;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100988 return 0;
989}
990
991static void win32_stop_timer(struct qemu_alarm_timer *t)
992{
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100993 HANDLE hTimer = t->priv;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100994
Paolo Bonzini68c23e52011-03-12 17:43:50 +0100995 if (hTimer) {
996 DeleteTimerQueueTimer(NULL, hTimer, NULL);
997 }
Paolo Bonzinidb1a4972010-03-10 11:38:55 +0100998}
999
1000static void win32_rearm_timer(struct qemu_alarm_timer *t)
1001{
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001002 HANDLE hTimer = t->priv;
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001003 int nearest_delta_ms;
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001004 BOOLEAN success;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001005
1006 assert(alarm_has_dynticks(t));
1007 if (!active_timers[QEMU_CLOCK_REALTIME] &&
1008 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1009 !active_timers[QEMU_CLOCK_HOST])
1010 return;
1011
Paolo Bonzinicfced5b2011-03-12 17:43:49 +01001012 nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1013 if (nearest_delta_ms < 1) {
1014 nearest_delta_ms = 1;
1015 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001016 success = ChangeTimerQueueTimer(NULL,
1017 hTimer,
1018 nearest_delta_ms,
1019 3600000);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001020
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001021 if (!success) {
1022 fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001023 GetLastError());
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001024 exit(-1);
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001025 }
Paolo Bonzini68c23e52011-03-12 17:43:50 +01001026
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001027}
1028
1029#endif /* _WIN32 */
1030
1031static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1032{
1033 if (running)
1034 qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1035}
1036
1037int init_timer_alarm(void)
1038{
1039 struct qemu_alarm_timer *t = NULL;
1040 int i, err = -1;
1041
1042 for (i = 0; alarm_timers[i].name; i++) {
1043 t = &alarm_timers[i];
1044
1045 err = t->start(t);
1046 if (!err)
1047 break;
1048 }
1049
1050 if (err) {
1051 err = -ENOENT;
1052 goto fail;
1053 }
1054
1055 /* first event is at time 0 */
1056 t->pending = 1;
1057 alarm_timer = t;
1058 qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1059
1060 return 0;
1061
1062fail:
1063 return err;
1064}
1065
1066void quit_timers(void)
1067{
1068 struct qemu_alarm_timer *t = alarm_timer;
1069 alarm_timer = NULL;
1070 t->stop(t);
1071}
1072
1073int qemu_calculate_timeout(void)
1074{
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001075 int timeout;
Paolo Bonzinic9f73832011-02-21 09:51:23 +01001076 int64_t add;
1077 int64_t delta;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001078
Edgar E. Iglesias225d02c2011-01-23 04:44:51 +01001079 /* When using icount, making forward progress with qemu_icount when the
1080 guest CPU is idle is critical. We only use the static io-thread timeout
1081 for non icount runs. */
Paolo Bonzinic9f73832011-02-21 09:51:23 +01001082 if (!use_icount || !vm_running) {
1083 return 5000;
Edgar E. Iglesias225d02c2011-01-23 04:44:51 +01001084 }
Edgar E. Iglesias225d02c2011-01-23 04:44:51 +01001085
Paolo Bonzinic9f73832011-02-21 09:51:23 +01001086 /* Advance virtual time to the next event. */
1087 delta = qemu_icount_delta();
1088 if (delta > 0) {
1089 /* If virtual time is ahead of real time then just
1090 wait for IO. */
1091 timeout = (delta + 999999) / 1000000;
1092 } else {
1093 /* Wait for either IO to occur or the next
1094 timer event. */
1095 add = qemu_next_deadline();
1096 /* We advance the timer before checking for IO.
1097 Limit the amount we advance so that early IO
1098 activity won't get the guest too far ahead. */
1099 if (add > 10000000)
1100 add = 10000000;
1101 delta += add;
1102 qemu_icount += qemu_icount_round (add);
1103 timeout = delta / 1000000;
1104 if (timeout < 0)
1105 timeout = 0;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001106 }
1107
1108 return timeout;
Paolo Bonzinidb1a4972010-03-10 11:38:55 +01001109}
1110