Statistics
| Branch: | Revision:

root / qemu-timer.c @ ca5a2a4b

History | View | Annotate | Download (29.9 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

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

    
48
#ifdef _WIN32
49
#include <windows.h>
50
#include <mmsystem.h>
51
#endif
52

    
53
#include "cpu-defs.h"
54
#include "qemu-timer.h"
55
#include "exec-all.h"
56

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

    
66

    
67
/***********************************************************/
68
/* real time host monotonic timer */
69

    
70

    
71
static int64_t get_clock_realtime(void)
72
{
73
    struct timeval tv;
74

    
75
    gettimeofday(&tv, NULL);
76
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
77
}
78

    
79
#ifdef WIN32
80

    
81
static int64_t clock_freq;
82

    
83
static void init_get_clock(void)
84
{
85
    LARGE_INTEGER freq;
86
    int ret;
87
    ret = QueryPerformanceFrequency(&freq);
88
    if (ret == 0) {
89
        fprintf(stderr, "Could not calibrate ticks\n");
90
        exit(1);
91
    }
92
    clock_freq = freq.QuadPart;
93
}
94

    
95
static int64_t get_clock(void)
96
{
97
    LARGE_INTEGER ti;
98
    QueryPerformanceCounter(&ti);
99
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
100
}
101

    
102
#else
103

    
104
static int use_rt_clock;
105

    
106
static void init_get_clock(void)
107
{
108
    use_rt_clock = 0;
109
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
110
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
111
    {
112
        struct timespec ts;
113
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
114
            use_rt_clock = 1;
115
        }
116
    }
117
#endif
118
}
119

    
120
static int64_t get_clock(void)
121
{
122
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
123
        || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
124
    if (use_rt_clock) {
125
        struct timespec ts;
126
        clock_gettime(CLOCK_MONOTONIC, &ts);
127
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
128
    } else
129
#endif
130
    {
131
        /* XXX: using gettimeofday leads to problems if the date
132
           changes, so it should be avoided. */
133
        return get_clock_realtime();
134
    }
135
}
136
#endif
137

    
138
/* Return the virtual CPU time, based on the instruction counter.  */
139
static int64_t cpu_get_icount(void)
140
{
141
    int64_t icount;
142
    CPUState *env = cpu_single_env;;
143
    icount = qemu_icount;
144
    if (env) {
145
        if (!can_do_io(env))
146
            fprintf(stderr, "Bad clock read\n");
147
        icount -= (env->icount_decr.u16.low + env->icount_extra);
148
    }
149
    return qemu_icount_bias + (icount << icount_time_shift);
150
}
151

    
152
/***********************************************************/
153
/* guest cycle counter */
154

    
155
typedef struct TimersState {
156
    int64_t cpu_ticks_prev;
157
    int64_t cpu_ticks_offset;
158
    int64_t cpu_clock_offset;
159
    int32_t cpu_ticks_enabled;
160
    int64_t dummy;
161
} TimersState;
162

    
163
TimersState timers_state;
164

    
165
/* return the host CPU cycle counter and handle stop/restart */
166
int64_t cpu_get_ticks(void)
167
{
168
    if (use_icount) {
169
        return cpu_get_icount();
170
    }
171
    if (!timers_state.cpu_ticks_enabled) {
172
        return timers_state.cpu_ticks_offset;
173
    } else {
174
        int64_t ticks;
175
        ticks = cpu_get_real_ticks();
176
        if (timers_state.cpu_ticks_prev > ticks) {
177
            /* Note: non increasing ticks may happen if the host uses
178
               software suspend */
179
            timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
180
        }
181
        timers_state.cpu_ticks_prev = ticks;
182
        return ticks + timers_state.cpu_ticks_offset;
183
    }
184
}
185

    
186
/* return the host CPU monotonic timer and handle stop/restart */
187
static int64_t cpu_get_clock(void)
188
{
189
    int64_t ti;
190
    if (!timers_state.cpu_ticks_enabled) {
191
        return timers_state.cpu_clock_offset;
192
    } else {
193
        ti = get_clock();
194
        return ti + timers_state.cpu_clock_offset;
195
    }
196
}
197

    
198
#ifndef CONFIG_IOTHREAD
199
static int64_t qemu_icount_delta(void)
200
{
201
    if (!use_icount) {
202
        return 5000 * (int64_t) 1000000;
203
    } else if (use_icount == 1) {
204
        /* When not using an adaptive execution frequency
205
           we tend to get badly out of sync with real time,
206
           so just delay for a reasonable amount of time.  */
207
        return 0;
208
    } else {
209
        return cpu_get_icount() - cpu_get_clock();
210
    }
211
}
212
#endif
213

    
214
/* enable cpu_get_ticks() */
215
void cpu_enable_ticks(void)
216
{
217
    if (!timers_state.cpu_ticks_enabled) {
218
        timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
219
        timers_state.cpu_clock_offset -= get_clock();
220
        timers_state.cpu_ticks_enabled = 1;
221
    }
222
}
223

    
224
/* disable cpu_get_ticks() : the clock is stopped. You must not call
225
   cpu_get_ticks() after that.  */
226
void cpu_disable_ticks(void)
227
{
228
    if (timers_state.cpu_ticks_enabled) {
229
        timers_state.cpu_ticks_offset = cpu_get_ticks();
230
        timers_state.cpu_clock_offset = cpu_get_clock();
231
        timers_state.cpu_ticks_enabled = 0;
232
    }
233
}
234

    
235
/***********************************************************/
236
/* timers */
237

    
238
#define QEMU_CLOCK_REALTIME 0
239
#define QEMU_CLOCK_VIRTUAL  1
240
#define QEMU_CLOCK_HOST     2
241

    
242
struct QEMUClock {
243
    int type;
244
    int enabled;
245
    /* XXX: add frequency */
246
};
247

    
248
struct QEMUTimer {
249
    QEMUClock *clock;
250
    int64_t expire_time;
251
    QEMUTimerCB *cb;
252
    void *opaque;
253
    struct QEMUTimer *next;
254
};
255

    
256
struct qemu_alarm_timer {
257
    char const *name;
258
    int (*start)(struct qemu_alarm_timer *t);
259
    void (*stop)(struct qemu_alarm_timer *t);
260
    void (*rearm)(struct qemu_alarm_timer *t);
261
    void *priv;
262

    
263
    char expired;
264
    char pending;
265
};
266

    
267
static struct qemu_alarm_timer *alarm_timer;
268

    
269
int qemu_alarm_pending(void)
270
{
271
    return alarm_timer->pending;
272
}
273

    
274
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
275
{
276
    return !!t->rearm;
277
}
278

    
279
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
280
{
281
    if (!alarm_has_dynticks(t))
282
        return;
283

    
284
    t->rearm(t);
285
}
286

    
287
/* TODO: MIN_TIMER_REARM_US should be optimized */
288
#define MIN_TIMER_REARM_US 250
289

    
290
#ifdef _WIN32
291

    
292
struct qemu_alarm_win32 {
293
    MMRESULT timerId;
294
    unsigned int period;
295
} alarm_win32_data = {0, 0};
296

    
297
static int win32_start_timer(struct qemu_alarm_timer *t);
298
static void win32_stop_timer(struct qemu_alarm_timer *t);
299
static void win32_rearm_timer(struct qemu_alarm_timer *t);
300

    
301
#else
302

    
303
static int unix_start_timer(struct qemu_alarm_timer *t);
304
static void unix_stop_timer(struct qemu_alarm_timer *t);
305

    
306
#ifdef __linux__
307

    
308
static int dynticks_start_timer(struct qemu_alarm_timer *t);
309
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
310
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
311

    
312
static int hpet_start_timer(struct qemu_alarm_timer *t);
313
static void hpet_stop_timer(struct qemu_alarm_timer *t);
314

    
315
static int rtc_start_timer(struct qemu_alarm_timer *t);
316
static void rtc_stop_timer(struct qemu_alarm_timer *t);
317

    
318
#endif /* __linux__ */
319

    
320
#endif /* _WIN32 */
321

    
322
/* Correlation between real and virtual time is always going to be
323
   fairly approximate, so ignore small variation.
324
   When the guest is idle real and virtual time will be aligned in
325
   the IO wait loop.  */
326
#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
327

    
328
static void icount_adjust(void)
329
{
330
    int64_t cur_time;
331
    int64_t cur_icount;
332
    int64_t delta;
333
    static int64_t last_delta;
334
    /* If the VM is not running, then do nothing.  */
335
    if (!vm_running)
336
        return;
337

    
338
    cur_time = cpu_get_clock();
339
    cur_icount = qemu_get_clock(vm_clock);
340
    delta = cur_icount - cur_time;
341
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
342
    if (delta > 0
343
        && last_delta + ICOUNT_WOBBLE < delta * 2
344
        && icount_time_shift > 0) {
345
        /* The guest is getting too far ahead.  Slow time down.  */
346
        icount_time_shift--;
347
    }
348
    if (delta < 0
349
        && last_delta - ICOUNT_WOBBLE > delta * 2
350
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
351
        /* The guest is getting too far behind.  Speed time up.  */
352
        icount_time_shift++;
353
    }
354
    last_delta = delta;
355
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
356
}
357

    
358
static void icount_adjust_rt(void * opaque)
359
{
360
    qemu_mod_timer(icount_rt_timer,
361
                   qemu_get_clock(rt_clock) + 1000);
362
    icount_adjust();
363
}
364

    
365
static void icount_adjust_vm(void * opaque)
366
{
367
    qemu_mod_timer(icount_vm_timer,
368
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
369
    icount_adjust();
370
}
371

    
372
int64_t qemu_icount_round(int64_t count)
373
{
374
    return (count + (1 << icount_time_shift) - 1) >> icount_time_shift;
375
}
376

    
377
static struct qemu_alarm_timer alarm_timers[] = {
378
#ifndef _WIN32
379
#ifdef __linux__
380
    {"dynticks", dynticks_start_timer,
381
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
382
    /* HPET - if available - is preferred */
383
    {"hpet", hpet_start_timer, hpet_stop_timer, NULL, NULL},
384
    /* ...otherwise try RTC */
385
    {"rtc", rtc_start_timer, rtc_stop_timer, NULL, NULL},
386
#endif
387
    {"unix", unix_start_timer, unix_stop_timer, NULL, NULL},
388
#else
389
    {"dynticks", win32_start_timer,
390
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
391
    {"win32", win32_start_timer,
392
     win32_stop_timer, NULL, &alarm_win32_data},
393
#endif
394
    {NULL, }
395
};
396

    
397
static void show_available_alarms(void)
398
{
399
    int i;
400

    
401
    printf("Available alarm timers, in order of precedence:\n");
402
    for (i = 0; alarm_timers[i].name; i++)
403
        printf("%s\n", alarm_timers[i].name);
404
}
405

    
406
void configure_alarms(char const *opt)
407
{
408
    int i;
409
    int cur = 0;
410
    int count = ARRAY_SIZE(alarm_timers) - 1;
411
    char *arg;
412
    char *name;
413
    struct qemu_alarm_timer tmp;
414

    
415
    if (!strcmp(opt, "?")) {
416
        show_available_alarms();
417
        exit(0);
418
    }
419

    
420
    arg = qemu_strdup(opt);
421

    
422
    /* Reorder the array */
423
    name = strtok(arg, ",");
424
    while (name) {
425
        for (i = 0; i < count && alarm_timers[i].name; i++) {
426
            if (!strcmp(alarm_timers[i].name, name))
427
                break;
428
        }
429

    
430
        if (i == count) {
431
            fprintf(stderr, "Unknown clock %s\n", name);
432
            goto next;
433
        }
434

    
435
        if (i < cur)
436
            /* Ignore */
437
            goto next;
438

    
439
        /* Swap */
440
        tmp = alarm_timers[i];
441
        alarm_timers[i] = alarm_timers[cur];
442
        alarm_timers[cur] = tmp;
443

    
444
        cur++;
445
next:
446
        name = strtok(NULL, ",");
447
    }
448

    
449
    qemu_free(arg);
450

    
451
    if (cur) {
452
        /* Disable remaining timers */
453
        for (i = cur; i < count; i++)
454
            alarm_timers[i].name = NULL;
455
    } else {
456
        show_available_alarms();
457
        exit(1);
458
    }
459
}
460

    
461
#define QEMU_NUM_CLOCKS 3
462

    
463
QEMUClock *rt_clock;
464
QEMUClock *vm_clock;
465
QEMUClock *host_clock;
466

    
467
static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
468

    
469
static QEMUClock *qemu_new_clock(int type)
470
{
471
    QEMUClock *clock;
472
    clock = qemu_mallocz(sizeof(QEMUClock));
473
    clock->type = type;
474
    clock->enabled = 1;
475
    return clock;
476
}
477

    
478
void qemu_clock_enable(QEMUClock *clock, int enabled)
479
{
480
    clock->enabled = enabled;
481
}
482

    
483
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
484
{
485
    QEMUTimer *ts;
486

    
487
    ts = qemu_mallocz(sizeof(QEMUTimer));
488
    ts->clock = clock;
489
    ts->cb = cb;
490
    ts->opaque = opaque;
491
    return ts;
492
}
493

    
494
void qemu_free_timer(QEMUTimer *ts)
495
{
496
    qemu_free(ts);
497
}
498

    
499
/* stop a timer, but do not dealloc it */
500
void qemu_del_timer(QEMUTimer *ts)
501
{
502
    QEMUTimer **pt, *t;
503

    
504
    /* NOTE: this code must be signal safe because
505
       qemu_timer_expired() can be called from a signal. */
506
    pt = &active_timers[ts->clock->type];
507
    for(;;) {
508
        t = *pt;
509
        if (!t)
510
            break;
511
        if (t == ts) {
512
            *pt = t->next;
513
            break;
514
        }
515
        pt = &t->next;
516
    }
517
}
518

    
519
/* modify the current timer so that it will be fired when current_time
520
   >= expire_time. The corresponding callback will be called. */
521
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
522
{
523
    QEMUTimer **pt, *t;
524

    
525
    qemu_del_timer(ts);
526

    
527
    /* add the timer in the sorted list */
528
    /* NOTE: this code must be signal safe because
529
       qemu_timer_expired() can be called from a signal. */
530
    pt = &active_timers[ts->clock->type];
531
    for(;;) {
532
        t = *pt;
533
        if (!t)
534
            break;
535
        if (t->expire_time > expire_time)
536
            break;
537
        pt = &t->next;
538
    }
539
    ts->expire_time = expire_time;
540
    ts->next = *pt;
541
    *pt = ts;
542

    
543
    /* Rearm if necessary  */
544
    if (pt == &active_timers[ts->clock->type]) {
545
        if (!alarm_timer->pending) {
546
            qemu_rearm_alarm_timer(alarm_timer);
547
        }
548
        /* Interrupt execution to force deadline recalculation.  */
549
        if (use_icount)
550
            qemu_notify_event();
551
    }
552
}
553

    
554
int qemu_timer_pending(QEMUTimer *ts)
555
{
556
    QEMUTimer *t;
557
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
558
        if (t == ts)
559
            return 1;
560
    }
561
    return 0;
562
}
563

    
564
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
565
{
566
    if (!timer_head)
567
        return 0;
568
    return (timer_head->expire_time <= current_time);
569
}
570

    
571
static void qemu_run_timers(QEMUClock *clock)
572
{
573
    QEMUTimer **ptimer_head, *ts;
574
    int64_t current_time;
575
   
576
    if (!clock->enabled)
577
        return;
578

    
579
    current_time = qemu_get_clock (clock);
580
    ptimer_head = &active_timers[clock->type];
581
    for(;;) {
582
        ts = *ptimer_head;
583
        if (!ts || ts->expire_time > current_time)
584
            break;
585
        /* remove timer from the list before calling the callback */
586
        *ptimer_head = ts->next;
587
        ts->next = NULL;
588

    
589
        /* run the callback (the timer list can be modified) */
590
        ts->cb(ts->opaque);
591
    }
592
}
593

    
594
int64_t qemu_get_clock(QEMUClock *clock)
595
{
596
    switch(clock->type) {
597
    case QEMU_CLOCK_REALTIME:
598
        return get_clock() / 1000000;
599
    default:
600
    case QEMU_CLOCK_VIRTUAL:
601
        if (use_icount) {
602
            return cpu_get_icount();
603
        } else {
604
            return cpu_get_clock();
605
        }
606
    case QEMU_CLOCK_HOST:
607
        return get_clock_realtime();
608
    }
609
}
610

    
611
int64_t qemu_get_clock_ns(QEMUClock *clock)
612
{
613
    switch(clock->type) {
614
    case QEMU_CLOCK_REALTIME:
615
        return get_clock();
616
    default:
617
    case QEMU_CLOCK_VIRTUAL:
618
        if (use_icount) {
619
            return cpu_get_icount();
620
        } else {
621
            return cpu_get_clock();
622
        }
623
    case QEMU_CLOCK_HOST:
624
        return get_clock_realtime();
625
    }
626
}
627

    
628
void init_clocks(void)
629
{
630
    init_get_clock();
631
    rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
632
    vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
633
    host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
634

    
635
    rtc_clock = host_clock;
636
}
637

    
638
/* save a timer */
639
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
640
{
641
    uint64_t expire_time;
642

    
643
    if (qemu_timer_pending(ts)) {
644
        expire_time = ts->expire_time;
645
    } else {
646
        expire_time = -1;
647
    }
648
    qemu_put_be64(f, expire_time);
649
}
650

    
651
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
652
{
653
    uint64_t expire_time;
654

    
655
    expire_time = qemu_get_be64(f);
656
    if (expire_time != -1) {
657
        qemu_mod_timer(ts, expire_time);
658
    } else {
659
        qemu_del_timer(ts);
660
    }
661
}
662

    
663
static const VMStateDescription vmstate_timers = {
664
    .name = "timer",
665
    .version_id = 2,
666
    .minimum_version_id = 1,
667
    .minimum_version_id_old = 1,
668
    .fields      = (VMStateField []) {
669
        VMSTATE_INT64(cpu_ticks_offset, TimersState),
670
        VMSTATE_INT64(dummy, TimersState),
671
        VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
672
        VMSTATE_END_OF_LIST()
673
    }
674
};
675

    
676
void configure_icount(const char *option)
677
{
678
    vmstate_register(0, &vmstate_timers, &timers_state);
679
    if (!option)
680
        return;
681

    
682
    if (strcmp(option, "auto") != 0) {
683
        icount_time_shift = strtol(option, NULL, 0);
684
        use_icount = 1;
685
        return;
686
    }
687

    
688
    use_icount = 2;
689

    
690
    /* 125MIPS seems a reasonable initial guess at the guest speed.
691
       It will be corrected fairly quickly anyway.  */
692
    icount_time_shift = 3;
693

    
694
    /* Have both realtime and virtual time triggers for speed adjustment.
695
       The realtime trigger catches emulated time passing too slowly,
696
       the virtual time trigger catches emulated time passing too fast.
697
       Realtime triggers occur even when idle, so use them less frequently
698
       than VM triggers.  */
699
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
700
    qemu_mod_timer(icount_rt_timer,
701
                   qemu_get_clock(rt_clock) + 1000);
702
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
703
    qemu_mod_timer(icount_vm_timer,
704
                   qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
705
}
706

    
707
void qemu_run_all_timers(void)
708
{
709
    alarm_timer->pending = 0;
710

    
711
    /* rearm timer, if not periodic */
712
    if (alarm_timer->expired) {
713
        alarm_timer->expired = 0;
714
        qemu_rearm_alarm_timer(alarm_timer);
715
    }
716

    
717
    /* vm time timers */
718
    if (vm_running) {
719
        qemu_run_timers(vm_clock);
720
    }
721

    
722
    qemu_run_timers(rt_clock);
723
    qemu_run_timers(host_clock);
724
}
725

    
726
#ifdef _WIN32
727
static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
728
                                        DWORD_PTR dwUser, DWORD_PTR dw1,
729
                                        DWORD_PTR dw2)
730
#else
731
static void host_alarm_handler(int host_signum)
732
#endif
733
{
734
    struct qemu_alarm_timer *t = alarm_timer;
735
    if (!t)
736
        return;
737

    
738
#if 0
739
#define DISP_FREQ 1000
740
    {
741
        static int64_t delta_min = INT64_MAX;
742
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
743
        static int count;
744
        ti = qemu_get_clock(vm_clock);
745
        if (last_clock != 0) {
746
            delta = ti - last_clock;
747
            if (delta < delta_min)
748
                delta_min = delta;
749
            if (delta > delta_max)
750
                delta_max = delta;
751
            delta_cum += delta;
752
            if (++count == DISP_FREQ) {
753
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
754
                       muldiv64(delta_min, 1000000, get_ticks_per_sec()),
755
                       muldiv64(delta_max, 1000000, get_ticks_per_sec()),
756
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
757
                       (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
758
                count = 0;
759
                delta_min = INT64_MAX;
760
                delta_max = 0;
761
                delta_cum = 0;
762
            }
763
        }
764
        last_clock = ti;
765
    }
766
#endif
767
    if (alarm_has_dynticks(t) ||
768
        (!use_icount &&
769
            qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
770
                               qemu_get_clock(vm_clock))) ||
771
        qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
772
                           qemu_get_clock(rt_clock)) ||
773
        qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
774
                           qemu_get_clock(host_clock))) {
775

    
776
        t->expired = alarm_has_dynticks(t);
777
        t->pending = 1;
778
        qemu_notify_event();
779
    }
780
}
781

    
782
int64_t qemu_next_deadline(void)
783
{
784
    /* To avoid problems with overflow limit this to 2^32.  */
785
    int64_t delta = INT32_MAX;
786

    
787
    if (active_timers[QEMU_CLOCK_VIRTUAL]) {
788
        delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
789
                     qemu_get_clock(vm_clock);
790
    }
791
    if (active_timers[QEMU_CLOCK_HOST]) {
792
        int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
793
                 qemu_get_clock(host_clock);
794
        if (hdelta < delta)
795
            delta = hdelta;
796
    }
797

    
798
    if (delta < 0)
799
        delta = 0;
800

    
801
    return delta;
802
}
803

    
804
#ifndef _WIN32
805

    
806
#if defined(__linux__)
807

    
808
#define RTC_FREQ 1024
809

    
810
static uint64_t qemu_next_deadline_dyntick(void)
811
{
812
    int64_t delta;
813
    int64_t rtdelta;
814

    
815
    if (use_icount)
816
        delta = INT32_MAX;
817
    else
818
        delta = (qemu_next_deadline() + 999) / 1000;
819

    
820
    if (active_timers[QEMU_CLOCK_REALTIME]) {
821
        rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
822
                 qemu_get_clock(rt_clock))*1000;
823
        if (rtdelta < delta)
824
            delta = rtdelta;
825
    }
826

    
827
    if (delta < MIN_TIMER_REARM_US)
828
        delta = MIN_TIMER_REARM_US;
829

    
830
    return delta;
831
}
832

    
833
static void enable_sigio_timer(int fd)
834
{
835
    struct sigaction act;
836

    
837
    /* timer signal */
838
    sigfillset(&act.sa_mask);
839
    act.sa_flags = 0;
840
    act.sa_handler = host_alarm_handler;
841

    
842
    sigaction(SIGIO, &act, NULL);
843
    fcntl_setfl(fd, O_ASYNC);
844
    fcntl(fd, F_SETOWN, getpid());
845
}
846

    
847
static int hpet_start_timer(struct qemu_alarm_timer *t)
848
{
849
    struct hpet_info info;
850
    int r, fd;
851

    
852
    fd = qemu_open("/dev/hpet", O_RDONLY);
853
    if (fd < 0)
854
        return -1;
855

    
856
    /* Set frequency */
857
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
858
    if (r < 0) {
859
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
860
                "error, but for better emulation accuracy type:\n"
861
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
862
        goto fail;
863
    }
864

    
865
    /* Check capabilities */
866
    r = ioctl(fd, HPET_INFO, &info);
867
    if (r < 0)
868
        goto fail;
869

    
870
    /* Enable periodic mode */
871
    r = ioctl(fd, HPET_EPI, 0);
872
    if (info.hi_flags && (r < 0))
873
        goto fail;
874

    
875
    /* Enable interrupt */
876
    r = ioctl(fd, HPET_IE_ON, 0);
877
    if (r < 0)
878
        goto fail;
879

    
880
    enable_sigio_timer(fd);
881
    t->priv = (void *)(long)fd;
882

    
883
    return 0;
884
fail:
885
    close(fd);
886
    return -1;
887
}
888

    
889
static void hpet_stop_timer(struct qemu_alarm_timer *t)
890
{
891
    int fd = (long)t->priv;
892

    
893
    close(fd);
894
}
895

    
896
static int rtc_start_timer(struct qemu_alarm_timer *t)
897
{
898
    int rtc_fd;
899
    unsigned long current_rtc_freq = 0;
900

    
901
    TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
902
    if (rtc_fd < 0)
903
        return -1;
904
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
905
    if (current_rtc_freq != RTC_FREQ &&
906
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
907
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
908
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
909
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
910
        goto fail;
911
    }
912
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
913
    fail:
914
        close(rtc_fd);
915
        return -1;
916
    }
917

    
918
    enable_sigio_timer(rtc_fd);
919

    
920
    t->priv = (void *)(long)rtc_fd;
921

    
922
    return 0;
923
}
924

    
925
static void rtc_stop_timer(struct qemu_alarm_timer *t)
926
{
927
    int rtc_fd = (long)t->priv;
928

    
929
    close(rtc_fd);
930
}
931

    
932
static int dynticks_start_timer(struct qemu_alarm_timer *t)
933
{
934
    struct sigevent ev;
935
    timer_t host_timer;
936
    struct sigaction act;
937

    
938
    sigfillset(&act.sa_mask);
939
    act.sa_flags = 0;
940
    act.sa_handler = host_alarm_handler;
941

    
942
    sigaction(SIGALRM, &act, NULL);
943

    
944
    /* 
945
     * Initialize ev struct to 0 to avoid valgrind complaining
946
     * about uninitialized data in timer_create call
947
     */
948
    memset(&ev, 0, sizeof(ev));
949
    ev.sigev_value.sival_int = 0;
950
    ev.sigev_notify = SIGEV_SIGNAL;
951
    ev.sigev_signo = SIGALRM;
952

    
953
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
954
        perror("timer_create");
955

    
956
        /* disable dynticks */
957
        fprintf(stderr, "Dynamic Ticks disabled\n");
958

    
959
        return -1;
960
    }
961

    
962
    t->priv = (void *)(long)host_timer;
963

    
964
    return 0;
965
}
966

    
967
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
968
{
969
    timer_t host_timer = (timer_t)(long)t->priv;
970

    
971
    timer_delete(host_timer);
972
}
973

    
974
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
975
{
976
    timer_t host_timer = (timer_t)(long)t->priv;
977
    struct itimerspec timeout;
978
    int64_t nearest_delta_us = INT64_MAX;
979
    int64_t current_us;
980

    
981
    assert(alarm_has_dynticks(t));
982
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
983
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
984
        !active_timers[QEMU_CLOCK_HOST])
985
        return;
986

    
987
    nearest_delta_us = qemu_next_deadline_dyntick();
988

    
989
    /* check whether a timer is already running */
990
    if (timer_gettime(host_timer, &timeout)) {
991
        perror("gettime");
992
        fprintf(stderr, "Internal timer error: aborting\n");
993
        exit(1);
994
    }
995
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
996
    if (current_us && current_us <= nearest_delta_us)
997
        return;
998

    
999
    timeout.it_interval.tv_sec = 0;
1000
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1001
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1002
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1003
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1004
        perror("settime");
1005
        fprintf(stderr, "Internal timer error: aborting\n");
1006
        exit(1);
1007
    }
1008
}
1009

    
1010
#endif /* defined(__linux__) */
1011

    
1012
static int unix_start_timer(struct qemu_alarm_timer *t)
1013
{
1014
    struct sigaction act;
1015
    struct itimerval itv;
1016
    int err;
1017

    
1018
    /* timer signal */
1019
    sigfillset(&act.sa_mask);
1020
    act.sa_flags = 0;
1021
    act.sa_handler = host_alarm_handler;
1022

    
1023
    sigaction(SIGALRM, &act, NULL);
1024

    
1025
    itv.it_interval.tv_sec = 0;
1026
    /* for i386 kernel 2.6 to get 1 ms */
1027
    itv.it_interval.tv_usec = 999;
1028
    itv.it_value.tv_sec = 0;
1029
    itv.it_value.tv_usec = 10 * 1000;
1030

    
1031
    err = setitimer(ITIMER_REAL, &itv, NULL);
1032
    if (err)
1033
        return -1;
1034

    
1035
    return 0;
1036
}
1037

    
1038
static void unix_stop_timer(struct qemu_alarm_timer *t)
1039
{
1040
    struct itimerval itv;
1041

    
1042
    memset(&itv, 0, sizeof(itv));
1043
    setitimer(ITIMER_REAL, &itv, NULL);
1044
}
1045

    
1046
#endif /* !defined(_WIN32) */
1047

    
1048

    
1049
#ifdef _WIN32
1050

    
1051
static int win32_start_timer(struct qemu_alarm_timer *t)
1052
{
1053
    TIMECAPS tc;
1054
    struct qemu_alarm_win32 *data = t->priv;
1055
    UINT flags;
1056

    
1057
    memset(&tc, 0, sizeof(tc));
1058
    timeGetDevCaps(&tc, sizeof(tc));
1059

    
1060
    data->period = tc.wPeriodMin;
1061
    timeBeginPeriod(data->period);
1062

    
1063
    flags = TIME_CALLBACK_FUNCTION;
1064
    if (alarm_has_dynticks(t))
1065
        flags |= TIME_ONESHOT;
1066
    else
1067
        flags |= TIME_PERIODIC;
1068

    
1069
    data->timerId = timeSetEvent(1,         // interval (ms)
1070
                        data->period,       // resolution
1071
                        host_alarm_handler, // function
1072
                        (DWORD)t,           // parameter
1073
                        flags);
1074

    
1075
    if (!data->timerId) {
1076
        fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
1077
                GetLastError());
1078
        timeEndPeriod(data->period);
1079
        return -1;
1080
    }
1081

    
1082
    return 0;
1083
}
1084

    
1085
static void win32_stop_timer(struct qemu_alarm_timer *t)
1086
{
1087
    struct qemu_alarm_win32 *data = t->priv;
1088

    
1089
    timeKillEvent(data->timerId);
1090
    timeEndPeriod(data->period);
1091
}
1092

    
1093
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1094
{
1095
    struct qemu_alarm_win32 *data = t->priv;
1096

    
1097
    assert(alarm_has_dynticks(t));
1098
    if (!active_timers[QEMU_CLOCK_REALTIME] &&
1099
        !active_timers[QEMU_CLOCK_VIRTUAL] &&
1100
        !active_timers[QEMU_CLOCK_HOST])
1101
        return;
1102

    
1103
    timeKillEvent(data->timerId);
1104

    
1105
    data->timerId = timeSetEvent(1,
1106
                        data->period,
1107
                        host_alarm_handler,
1108
                        (DWORD)t,
1109
                        TIME_ONESHOT | TIME_CALLBACK_FUNCTION);
1110

    
1111
    if (!data->timerId) {
1112
        fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
1113
                GetLastError());
1114

    
1115
        timeEndPeriod(data->period);
1116
        exit(1);
1117
    }
1118
}
1119

    
1120
#endif /* _WIN32 */
1121

    
1122
static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason)
1123
{
1124
    if (running)
1125
        qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque);
1126
}
1127

    
1128
int init_timer_alarm(void)
1129
{
1130
    struct qemu_alarm_timer *t = NULL;
1131
    int i, err = -1;
1132

    
1133
    for (i = 0; alarm_timers[i].name; i++) {
1134
        t = &alarm_timers[i];
1135

    
1136
        err = t->start(t);
1137
        if (!err)
1138
            break;
1139
    }
1140

    
1141
    if (err) {
1142
        err = -ENOENT;
1143
        goto fail;
1144
    }
1145

    
1146
    /* first event is at time 0 */
1147
    t->pending = 1;
1148
    alarm_timer = t;
1149
    qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t);
1150

    
1151
    return 0;
1152

    
1153
fail:
1154
    return err;
1155
}
1156

    
1157
void quit_timers(void)
1158
{
1159
    struct qemu_alarm_timer *t = alarm_timer;
1160
    alarm_timer = NULL;
1161
    t->stop(t);
1162
}
1163

    
1164
int qemu_calculate_timeout(void)
1165
{
1166
#ifndef CONFIG_IOTHREAD
1167
    int timeout;
1168

    
1169
    if (!vm_running)
1170
        timeout = 5000;
1171
    else {
1172
     /* XXX: use timeout computed from timers */
1173
        int64_t add;
1174
        int64_t delta;
1175
        /* Advance virtual time to the next event.  */
1176
        delta = qemu_icount_delta();
1177
        if (delta > 0) {
1178
            /* If virtual time is ahead of real time then just
1179
               wait for IO.  */
1180
            timeout = (delta + 999999) / 1000000;
1181
        } else {
1182
            /* Wait for either IO to occur or the next
1183
               timer event.  */
1184
            add = qemu_next_deadline();
1185
            /* We advance the timer before checking for IO.
1186
               Limit the amount we advance so that early IO
1187
               activity won't get the guest too far ahead.  */
1188
            if (add > 10000000)
1189
                add = 10000000;
1190
            delta += add;
1191
            qemu_icount += qemu_icount_round (add);
1192
            timeout = delta / 1000000;
1193
            if (timeout < 0)
1194
                timeout = 0;
1195
        }
1196
    }
1197

    
1198
    return timeout;
1199
#else /* CONFIG_IOTHREAD */
1200
    return 1000;
1201
#endif
1202
}
1203