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