Statistics
| Branch: | Revision:

root / qemu-timer.c @ 68c23e55

History | View | Annotate | Download (27.6 kB)

1
/*
2
 * QEMU System Emulator
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24

    
25
#include "sysemu.h"
26
#include "net.h"
27
#include "monitor.h"
28
#include "console.h"
29

    
30
#include "hw/hw.h"
31

    
32
#include <unistd.h>
33
#include <fcntl.h>
34
#include <time.h>
35
#include <errno.h>
36
#include <sys/time.h>
37
#include <signal.h>
38
#ifdef __FreeBSD__
39
#include <sys/param.h>
40
#endif
41

    
42
#ifdef __linux__
43
#include <sys/ioctl.h>
44
#include <linux/rtc.h>
45
/* For the benefit of older linux systems which don't supply it,
46
   we use a local copy of hpet.h. */
47
/* #include <linux/hpet.h> */
48
#include "hpet.h"
49
#endif
50

    
51
#ifdef _WIN32
52
#include <windows.h>
53
#include <mmsystem.h>
54
#endif
55

    
56
#include "qemu-timer.h"
57

    
58
/* Conversion factor from emulated instructions to virtual clock ticks.  */
59
int icount_time_shift;
60
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
61
#define MAX_ICOUNT_SHIFT 10
62
/* Compensate for varying guest execution speed.  */
63
int64_t qemu_icount_bias;
64
static QEMUTimer *icount_rt_timer;
65
static QEMUTimer *icount_vm_timer;
66

    
67
/***********************************************************/
68
/* guest cycle counter */
69

    
70
typedef struct TimersState {
71
    int64_t cpu_ticks_prev;
72
    int64_t cpu_ticks_offset;
73
    int64_t cpu_clock_offset;
74
    int32_t cpu_ticks_enabled;
75
    int64_t dummy;
76
} TimersState;
77

    
78
TimersState timers_state;
79

    
80
/* return the host CPU cycle counter and handle stop/restart */
81
int64_t cpu_get_ticks(void)
82
{
83
    if (use_icount) {
84
        return cpu_get_icount();
85
    }
86
    if (!timers_state.cpu_ticks_enabled) {
87
        return timers_state.cpu_ticks_offset;
88
    } else {
89
        int64_t ticks;
90
        ticks = cpu_get_real_ticks();
91
        if (timers_state.cpu_ticks_prev > ticks) {
92
            /* Note: non increasing ticks may happen if the host uses
93
               software suspend */
94
            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
95
        }
96
        timers_state.cpu_ticks_prev = ticks;
97
        return ticks + timers_state.cpu_ticks_offset;
98
    }
99
}
100

    
101
/* return the host CPU monotonic timer and handle stop/restart */
102
static int64_t cpu_get_clock(void)
103
{
104
    int64_t ti;
105
    if (!timers_state.cpu_ticks_enabled) {
106
        return timers_state.cpu_clock_offset;
107
    } else {
108
        ti = get_clock();
109
        return ti + timers_state.cpu_clock_offset;
110
    }
111
}
112

    
113
static int64_t qemu_icount_delta(void)
114
{
115
    if (use_icount == 1) {
116
        /* When not using an adaptive execution frequency
117
           we tend to get badly out of sync with real time,
118
           so just delay for a reasonable amount of time.  */
119
        return 0;
120
    } else {
121
        return cpu_get_icount() - cpu_get_clock();
122
    }
123
}
124

    
125
/* enable cpu_get_ticks() */
126
void cpu_enable_ticks(void)
127
{
128
    if (!timers_state.cpu_ticks_enabled) {
129
        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
130
        timers_state.cpu_clock_offset -= get_clock();
131
        timers_state.cpu_ticks_enabled = 1;
132
    }
133
}
134

    
135
/* disable cpu_get_ticks() : the clock is stopped. You must not call
136
   cpu_get_ticks() after that.  */
137
void cpu_disable_ticks(void)
138
{
139
    if (timers_state.cpu_ticks_enabled) {
140
        timers_state.cpu_ticks_offset = cpu_get_ticks();
141
        timers_state.cpu_clock_offset = cpu_get_clock();
142
        timers_state.cpu_ticks_enabled = 0;
143
    }
144
}
145

    
146
/***********************************************************/
147
/* timers */
148

    
149
#define QEMU_CLOCK_REALTIME 0
150
#define QEMU_CLOCK_VIRTUAL  1
151
#define QEMU_CLOCK_HOST     2
152

    
153
struct QEMUClock {
154
    int type;
155
    int enabled;
156
    /* XXX: add frequency */
157
};
158

    
159
struct QEMUTimer {
160
    QEMUClock *clock;
161
    int64_t expire_time;
162
    QEMUTimerCB *cb;
163
    void *opaque;
164
    struct QEMUTimer *next;
165
};
166

    
167
struct 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

    
178
static struct qemu_alarm_timer *alarm_timer;
179

    
180
int qemu_alarm_pending(void)
181
{
182
    return alarm_timer->pending;
183
}
184

    
185
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
186
{
187
    return !!t->rearm;
188
}
189

    
190
static 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

    
198
/* TODO: MIN_TIMER_REARM_NS should be optimized */
199
#define MIN_TIMER_REARM_NS 250000
200

    
201
#ifdef _WIN32
202

    
203
static int win32_start_timer(struct qemu_alarm_timer *t);
204
static void win32_stop_timer(struct qemu_alarm_timer *t);
205
static void win32_rearm_timer(struct qemu_alarm_timer *t);
206

    
207
#else
208

    
209
static int unix_start_timer(struct qemu_alarm_timer *t);
210
static void unix_stop_timer(struct qemu_alarm_timer *t);
211

    
212
#ifdef __linux__
213

    
214
static int dynticks_start_timer(struct qemu_alarm_timer *t);
215
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
216
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
217

    
218
static int hpet_start_timer(struct qemu_alarm_timer *t);
219
static void hpet_stop_timer(struct qemu_alarm_timer *t);
220

    
221
static int rtc_start_timer(struct qemu_alarm_timer *t);
222
static 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

    
234
static 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();
245
    cur_icount = qemu_get_clock(vm_clock);
246
    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

    
264
static void icount_adjust_rt(void * opaque)
265
{
266
    qemu_mod_timer(icount_rt_timer,
267
                   qemu_get_clock(rt_clock) + 1000);
268
    icount_adjust();
269
}
270

    
271
static void icount_adjust_vm(void * opaque)
272
{
273
    qemu_mod_timer(icount_vm_timer,
274
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
275
    icount_adjust();
276
}
277

    
278
int64_t qemu_icount_round(int64_t count)
279
{
280
    return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
281
}
282

    
283
static 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,
296
     win32_stop_timer, win32_rearm_timer, NULL},
297
    {"win32", win32_start_timer,
298
     win32_stop_timer, NULL, NULL},
299
#endif
300
    {NULL, }
301
};
302

    
303
static 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

    
312
void 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++;
351
next:
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

    
369
QEMUClock *rt_clock;
370
QEMUClock *vm_clock;
371
QEMUClock *host_clock;
372

    
373
static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
374

    
375
static 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

    
384
void qemu_clock_enable(QEMUClock *clock, int enabled)
385
{
386
    clock->enabled = enabled;
387
}
388

    
389
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
390
{
391
    QEMUTimer *ts;
392

    
393
    ts = qemu_mallocz(sizeof(QEMUTimer));
394
    ts->clock = clock;
395
    ts->cb = cb;
396
    ts->opaque = opaque;
397
    return ts;
398
}
399

    
400
void qemu_free_timer(QEMUTimer *ts)
401
{
402
    qemu_free(ts);
403
}
404

    
405
/* stop a timer, but do not dealloc it */
406
void qemu_del_timer(QEMUTimer *ts)
407
{
408
    QEMUTimer **pt, *t;
409

    
410
    /* NOTE: this code must be signal safe because
411
       qemu_timer_expired() can be called from a signal. */
412
    pt = &active_timers[ts->clock->type];
413
    for(;;) {
414
        t = *pt;
415
        if (!t)
416
            break;
417
        if (t == ts) {
418
            *pt = t->next;
419
            break;
420
        }
421
        pt = &t->next;
422
    }
423
}
424

    
425
/* modify the current timer so that it will be fired when current_time
426
   >= expire_time. The corresponding callback will be called. */
427
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
428
{
429
    QEMUTimer **pt, *t;
430

    
431
    qemu_del_timer(ts);
432

    
433
    /* add the timer in the sorted list */
434
    /* NOTE: this code must be signal safe because
435
       qemu_timer_expired() can be called from a signal. */
436
    pt = &active_timers[ts->clock->type];
437
    for(;;) {
438
        t = *pt;
439
        if (!t)
440
            break;
441
        if (t->expire_time > expire_time)
442
            break;
443
        pt = &t->next;
444
    }
445
    ts->expire_time = expire_time;
446
    ts->next = *pt;
447
    *pt = ts;
448

    
449
    /* Rearm if necessary  */
450
    if (pt == &active_timers[ts->clock->type]) {
451
        if (!alarm_timer->pending) {
452
            qemu_rearm_alarm_timer(alarm_timer);
453
        }
454
        /* Interrupt execution to force deadline recalculation.  */
455
        if (use_icount)
456
            qemu_notify_event();
457
    }
458
}
459

    
460
int qemu_timer_pending(QEMUTimer *ts)
461
{
462
    QEMUTimer *t;
463
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
464
        if (t == ts)
465
            return 1;
466
    }
467
    return 0;
468
}
469

    
470
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
471
{
472
    if (!timer_head)
473
        return 0;
474
    return (timer_head->expire_time <= current_time);
475
}
476

    
477
static void qemu_run_timers(QEMUClock *clock)
478
{
479
    QEMUTimer **ptimer_head, *ts;
480
    int64_t current_time;
481
   
482
    if (!clock->enabled)
483
        return;
484

    
485
    current_time = qemu_get_clock (clock);
486
    ptimer_head = &active_timers[clock->type];
487
    for(;;) {
488
        ts = *ptimer_head;
489
        if (!ts || ts->expire_time > current_time)
490
            break;
491
        /* remove timer from the list before calling the callback */
492
        *ptimer_head = ts->next;
493
        ts->next = NULL;
494

    
495
        /* run the callback (the timer list can be modified) */
496
        ts->cb(ts->opaque);
497
    }
498
}
499

    
500
int64_t qemu_get_clock(QEMUClock *clock)
501
{
502
    switch(clock->type) {
503
    case QEMU_CLOCK_REALTIME:
504
        return get_clock() / 1000000;
505
    default:
506
    case QEMU_CLOCK_VIRTUAL:
507
        if (use_icount) {
508
            return cpu_get_icount();
509
        } else {
510
            return cpu_get_clock();
511
        }
512
    case QEMU_CLOCK_HOST:
513
        return get_clock_realtime();
514
    }
515
}
516

    
517
int64_t qemu_get_clock_ns(QEMUClock *clock)
518
{
519
    switch(clock->type) {
520
    case QEMU_CLOCK_REALTIME:
521
        return get_clock();
522
    default:
523
    case QEMU_CLOCK_VIRTUAL:
524
        if (use_icount) {
525
            return cpu_get_icount();
526
        } else {
527
            return cpu_get_clock();
528
        }
529
    case QEMU_CLOCK_HOST:
530
        return get_clock_realtime();
531
    }
532
}
533

    
534
void init_clocks(void)
535
{
536
    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
537
    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
538
    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
539

    
540
    rtc_clock = host_clock;
541
}
542

    
543
/* save a timer */
544
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
545
{
546
    uint64_t expire_time;
547

    
548
    if (qemu_timer_pending(ts)) {
549
        expire_time = ts->expire_time;
550
    } else {
551
        expire_time = -1;
552
    }
553
    qemu_put_be64(f, expire_time);
554
}
555

    
556
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
557
{
558
    uint64_t expire_time;
559

    
560
    expire_time = qemu_get_be64(f);
561
    if (expire_time != -1) {
562
        qemu_mod_timer(ts, expire_time);
563
    } else {
564
        qemu_del_timer(ts);
565
    }
566
}
567

    
568
static const VMStateDescription vmstate_timers = {
569
    .name = "timer",
570
    .version_id = 2,
571
    .minimum_version_id = 1,
572
    .minimum_version_id_old = 1,
573
    .fields      = (VMStateField []) {
574
        VMSTATE_INT64(cpu_ticks_offset, TimersState),
575
        VMSTATE_INT64(dummy, TimersState),
576
        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
577
        VMSTATE_END_OF_LIST()
578
    }
579
};
580

    
581
void configure_icount(const char *option)
582
{
583
    vmstate_register(NULL, 0, &vmstate_timers, &timers_state);
584
    if (!option)
585
        return;
586

    
587
    if (strcmp(option, "auto") != 0) {
588
        icount_time_shift = strtol(option, NULL, 0);
589
        use_icount = 1;
590
        return;
591
    }
592

    
593
    use_icount = 2;
594

    
595
    /* 125MIPS seems a reasonable initial guess at the guest speed.
596
       It will be corrected fairly quickly anyway.  */
597
    icount_time_shift = 3;
598

    
599
    /* Have both realtime and virtual time triggers for speed adjustment.
600
       The realtime trigger catches emulated time passing too slowly,
601
       the virtual time trigger catches emulated time passing too fast.
602
       Realtime triggers occur even when idle, so use them less frequently
603
       than VM triggers.  */
604
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
605
    qemu_mod_timer(icount_rt_timer,
606
                   qemu_get_clock(rt_clock) + 1000);
607
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
608
    qemu_mod_timer(icount_vm_timer,
609
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
610
}
611

    
612
void qemu_run_all_timers(void)
613
{
614
    alarm_timer->pending = 0;
615

    
616
    /* rearm timer, if not periodic */
617
    if (alarm_timer->expired) {
618
        alarm_timer->expired = 0;
619
        qemu_rearm_alarm_timer(alarm_timer);
620
    }
621

    
622
    /* vm time timers */
623
    if (vm_running) {
624
        qemu_run_timers(vm_clock);
625
    }
626

    
627
    qemu_run_timers(rt_clock);
628
    qemu_run_timers(host_clock);
629
}
630

    
631
static int64_t qemu_next_alarm_deadline(void);
632

    
633
#ifdef _WIN32
634
static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused)
635
#else
636
static void host_alarm_handler(int host_signum)
637
#endif
638
{
639
    struct qemu_alarm_timer *t = alarm_timer;
640
    if (!t)
641
        return;
642

    
643
#if 0
644
#define DISP_FREQ 1000
645
    {
646
        static int64_t delta_min = INT64_MAX;
647
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
648
        static int count;
649
        ti = qemu_get_clock(vm_clock);
650
        if (last_clock != 0) {
651
            delta = ti - last_clock;
652
            if (delta < delta_min)
653
                delta_min = delta;
654
            if (delta > delta_max)
655
                delta_max = delta;
656
            delta_cum += delta;
657
            if (++count == DISP_FREQ) {
658
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
659
                       muldiv64(delta_min, 1000000, get_ticks_per_sec()),
660
                       muldiv64(delta_max, 1000000, get_ticks_per_sec()),
661
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
662
                       (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
663
                count = 0;
664
                delta_min = INT64_MAX;
665
                delta_max = 0;
666
                delta_cum = 0;
667
            }
668
        }
669
        last_clock = ti;
670
    }
671
#endif
672
    if (alarm_has_dynticks(t) ||
673
        qemu_next_alarm_deadline () <= 0) {
674
        t->expired = alarm_has_dynticks(t);
675
        t->pending = 1;
676
        qemu_notify_event();
677
    }
678
}
679

    
680
int64_t qemu_next_deadline(void)
681
{
682
    /* To avoid problems with overflow limit this to 2^32.  */
683
    int64_t delta = INT32_MAX;
684

    
685
    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
686
        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
687
                     qemu_get_clock_ns(vm_clock);
688
    }
689
    if (active_timers[QEMU_CLOCK_HOST]) {
690
        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
691
                 qemu_get_clock_ns(host_clock);
692
        if (hdelta < delta)
693
            delta = hdelta;
694
    }
695

    
696
    if (delta < 0)
697
        delta = 0;
698

    
699
    return delta;
700
}
701

    
702
static int64_t qemu_next_alarm_deadline(void)
703
{
704
    int64_t delta;
705
    int64_t rtdelta;
706

    
707
    if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) {
708
        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
709
                     qemu_get_clock(vm_clock);
710
    } else {
711
        delta = INT32_MAX;
712
    }
713
    if (active_timers[QEMU_CLOCK_HOST]) {
714
        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
715
                 qemu_get_clock_ns(host_clock);
716
        if (hdelta < delta)
717
            delta = hdelta;
718
    }
719
    if (active_timers[QEMU_CLOCK_REALTIME]) {
720
        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time * 1000000 -
721
                 qemu_get_clock_ns(rt_clock));
722
        if (rtdelta < delta)
723
            delta = rtdelta;
724
    }
725

    
726
    return delta;
727
}
728

    
729
#if defined(__linux__)
730

    
731
#define RTC_FREQ 1024
732

    
733
static void enable_sigio_timer(int fd)
734
{
735
    struct sigaction act;
736

    
737
    /* timer signal */
738
    sigfillset(&act.sa_mask);
739
    act.sa_flags = 0;
740
    act.sa_handler = host_alarm_handler;
741

    
742
    sigaction(SIGIO, &act, NULL);
743
    fcntl_setfl(fd, O_ASYNC);
744
    fcntl(fd, F_SETOWN, getpid());
745
}
746

    
747
static int hpet_start_timer(struct qemu_alarm_timer *t)
748
{
749
    struct hpet_info info;
750
    int r, fd;
751

    
752
    fd = qemu_open("/dev/hpet", O_RDONLY);
753
    if (fd < 0)
754
        return -1;
755

    
756
    /* Set frequency */
757
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
758
    if (r < 0) {
759
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
760
                "error, but for better emulation accuracy type:\n"
761
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
762
        goto fail;
763
    }
764

    
765
    /* Check capabilities */
766
    r = ioctl(fd, HPET_INFO, &info);
767
    if (r < 0)
768
        goto fail;
769

    
770
    /* Enable periodic mode */
771
    r = ioctl(fd, HPET_EPI, 0);
772
    if (info.hi_flags && (r < 0))
773
        goto fail;
774

    
775
    /* Enable interrupt */
776
    r = ioctl(fd, HPET_IE_ON, 0);
777
    if (r < 0)
778
        goto fail;
779

    
780
    enable_sigio_timer(fd);
781
    t->priv = (void *)(long)fd;
782

    
783
    return 0;
784
fail:
785
    close(fd);
786
    return -1;
787
}
788

    
789
static void hpet_stop_timer(struct qemu_alarm_timer *t)
790
{
791
    int fd = (long)t->priv;
792

    
793
    close(fd);
794
}
795

    
796
static int rtc_start_timer(struct qemu_alarm_timer *t)
797
{
798
    int rtc_fd;
799
    unsigned long current_rtc_freq = 0;
800

    
801
    TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
802
    if (rtc_fd < 0)
803
        return -1;
804
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
805
    if (current_rtc_freq != RTC_FREQ &&
806
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
807
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
808
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
809
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
810
        goto fail;
811
    }
812
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
813
    fail:
814
        close(rtc_fd);
815
        return -1;
816
    }
817

    
818
    enable_sigio_timer(rtc_fd);
819

    
820
    t->priv = (void *)(long)rtc_fd;
821

    
822
    return 0;
823
}
824

    
825
static void rtc_stop_timer(struct qemu_alarm_timer *t)
826
{
827
    int rtc_fd = (long)t->priv;
828

    
829
    close(rtc_fd);
830
}
831

    
832
static int dynticks_start_timer(struct qemu_alarm_timer *t)
833
{
834
    struct sigevent ev;
835
    timer_t host_timer;
836
    struct sigaction act;
837

    
838
    sigfillset(&act.sa_mask);
839
    act.sa_flags = 0;
840
    act.sa_handler = host_alarm_handler;
841

    
842
    sigaction(SIGALRM, &act, NULL);
843

    
844
    /* 
845
     * Initialize ev struct to 0 to avoid valgrind complaining
846
     * about uninitialized data in timer_create call
847
     */
848
    memset(&ev, 0, sizeof(ev));
849
    ev.sigev_value.sival_int = 0;
850
    ev.sigev_notify = SIGEV_SIGNAL;
851
    ev.sigev_signo = SIGALRM;
852

    
853
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
854
        perror("timer_create");
855

    
856
        /* disable dynticks */
857
        fprintf(stderr, "Dynamic Ticks disabled\n");
858

    
859
        return -1;
860
    }
861

    
862
    t->priv = (void *)(long)host_timer;
863

    
864
    return 0;
865
}
866

    
867
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
868
{
869
    timer_t host_timer = (timer_t)(long)t->priv;
870

    
871
    timer_delete(host_timer);
872
}
873

    
874
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
875
{
876
    timer_t host_timer = (timer_t)(long)t->priv;
877
    struct itimerspec timeout;
878
    int64_t nearest_delta_ns = INT64_MAX;
879
    int64_t current_ns;
880

    
881
    assert(alarm_has_dynticks(t));
882
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
883
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
884
        !active_timers[QEMU_CLOCK_HOST])
885
        return;
886

    
887
    nearest_delta_ns = qemu_next_alarm_deadline();
888
    if (nearest_delta_ns < MIN_TIMER_REARM_NS)
889
        nearest_delta_ns = MIN_TIMER_REARM_NS;
890

    
891
    /* check whether a timer is already running */
892
    if (timer_gettime(host_timer, &timeout)) {
893
        perror("gettime");
894
        fprintf(stderr, "Internal timer error: aborting\n");
895
        exit(1);
896
    }
897
    current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
898
    if (current_ns && current_ns <= nearest_delta_ns)
899
        return;
900

    
901
    timeout.it_interval.tv_sec = 0;
902
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
903
    timeout.it_value.tv_sec =  nearest_delta_ns / 1000000000;
904
    timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
905
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
906
        perror("settime");
907
        fprintf(stderr, "Internal timer error: aborting\n");
908
        exit(1);
909
    }
910
}
911

    
912
#endif /* defined(__linux__) */
913

    
914
#if !defined(_WIN32)
915

    
916
static int unix_start_timer(struct qemu_alarm_timer *t)
917
{
918
    struct sigaction act;
919
    struct itimerval itv;
920
    int err;
921

    
922
    /* timer signal */
923
    sigfillset(&act.sa_mask);
924
    act.sa_flags = 0;
925
    act.sa_handler = host_alarm_handler;
926

    
927
    sigaction(SIGALRM, &act, NULL);
928

    
929
    itv.it_interval.tv_sec = 0;
930
    /* for i386 kernel 2.6 to get 1 ms */
931
    itv.it_interval.tv_usec = 999;
932
    itv.it_value.tv_sec = 0;
933
    itv.it_value.tv_usec = 10 * 1000;
934

    
935
    err = setitimer(ITIMER_REAL, &itv, NULL);
936
    if (err)
937
        return -1;
938

    
939
    return 0;
940
}
941

    
942
static void unix_stop_timer(struct qemu_alarm_timer *t)
943
{
944
    struct itimerval itv;
945

    
946
    memset(&itv, 0, sizeof(itv));
947
    setitimer(ITIMER_REAL, &itv, NULL);
948
}
949

    
950
#endif /* !defined(_WIN32) */
951

    
952

    
953
#ifdef _WIN32
954

    
955
static int win32_start_timer(struct qemu_alarm_timer *t)
956
{
957
    HANDLE hTimer;
958
    BOOLEAN success;
959

    
960
    /* If you call ChangeTimerQueueTimer on a one-shot timer (its period
961
       is zero) that has already expired, the timer is not updated.  Since
962
       creating a new timer is relatively expensive, set a bogus one-hour
963
       interval in the dynticks case.  */
964
    success = CreateTimerQueueTimer(&hTimer,
965
                          NULL,
966
                          host_alarm_handler,
967
                          t,
968
                          1,
969
                          alarm_has_dynticks(t) ? 3600000 : 1,
970
                          WT_EXECUTEINTIMERTHREAD);
971

    
972
    if (!success) {
973
        fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
974
                GetLastError());
975
        return -1;
976
    }
977

    
978
    t->priv = (PVOID) hTimer;
979
    return 0;
980
}
981

    
982
static void win32_stop_timer(struct qemu_alarm_timer *t)
983
{
984
    HANDLE hTimer = t->priv;
985

    
986
    if (hTimer) {
987
        DeleteTimerQueueTimer(NULL, hTimer, NULL);
988
    }
989
}
990

    
991
static void win32_rearm_timer(struct qemu_alarm_timer *t)
992
{
993
    HANDLE hTimer = t->priv;
994
    int nearest_delta_ms;
995
    BOOLEAN success;
996

    
997
    assert(alarm_has_dynticks(t));
998
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
999
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
1000
        !active_timers[QEMU_CLOCK_HOST])
1001
        return;
1002

    
1003
    nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000;
1004
    if (nearest_delta_ms < 1) {
1005
        nearest_delta_ms = 1;
1006
    }
1007
    success = ChangeTimerQueueTimer(NULL,
1008
                                    hTimer,
1009
                                    nearest_delta_ms,
1010
                                    3600000);
1011

    
1012
    if (!success) {
1013
        fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
1014
                GetLastError());
1015
        exit(-1);
1016
    }
1017

    
1018
}
1019

    
1020
#endif /* _WIN32 */
1021

    
1022
static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1023
{
1024
    if (running)
1025
        qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1026
}
1027

    
1028
int init_timer_alarm(void)
1029
{
1030
    struct qemu_alarm_timer *t = NULL;
1031
    int i, err = -1;
1032

    
1033
    for (i = 0; alarm_timers[i].name; i++) {
1034
        t = &alarm_timers[i];
1035

    
1036
        err = t->start(t);
1037
        if (!err)
1038
            break;
1039
    }
1040

    
1041
    if (err) {
1042
        err = -ENOENT;
1043
        goto fail;
1044
    }
1045

    
1046
    /* first event is at time 0 */
1047
    t->pending = 1;
1048
    alarm_timer = t;
1049
    qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1050

    
1051
    return 0;
1052

    
1053
fail:
1054
    return err;
1055
}
1056

    
1057
void quit_timers(void)
1058
{
1059
    struct qemu_alarm_timer *t = alarm_timer;
1060
    alarm_timer = NULL;
1061
    t->stop(t);
1062
}
1063

    
1064
int qemu_calculate_timeout(void)
1065
{
1066
    int timeout;
1067
    int64_t add;
1068
    int64_t delta;
1069

    
1070
    /* When using icount, making forward progress with qemu_icount when the
1071
       guest CPU is idle is critical. We only use the static io-thread timeout
1072
       for non icount runs.  */
1073
    if (!use_icount || !vm_running) {
1074
        return 5000;
1075
    }
1076

    
1077
    /* Advance virtual time to the next event.  */
1078
    delta = qemu_icount_delta();
1079
    if (delta > 0) {
1080
        /* If virtual time is ahead of real time then just
1081
           wait for IO.  */
1082
        timeout = (delta + 999999) / 1000000;
1083
    } else {
1084
        /* Wait for either IO to occur or the next
1085
           timer event.  */
1086
        add = qemu_next_deadline();
1087
        /* We advance the timer before checking for IO.
1088
           Limit the amount we advance so that early IO
1089
           activity won't get the guest too far ahead.  */
1090
        if (add > 10000000)
1091
            add = 10000000;
1092
        delta += add;
1093
        qemu_icount += qemu_icount_round (add);
1094
        timeout = delta / 1000000;
1095
        if (timeout < 0)
1096
            timeout = 0;
1097
    }
1098

    
1099
    return timeout;
1100
}
1101