Statistics
| Branch: | Revision:

root / qemu-timer.h @ 4260a739

History | View | Annotate | Download (8.5 kB)

1 87ecb68b pbrook
#ifndef QEMU_TIMER_H
2 87ecb68b pbrook
#define QEMU_TIMER_H
3 87ecb68b pbrook
4 29e922b6 Blue Swirl
#include "qemu-common.h"
5 691a0c9c Jan Kiszka
#include "notify.h"
6 c57c846a Blue Swirl
#include <time.h>
7 c57c846a Blue Swirl
#include <sys/time.h>
8 c57c846a Blue Swirl
9 c57c846a Blue Swirl
#ifdef _WIN32
10 c57c846a Blue Swirl
#include <windows.h>
11 c57c846a Blue Swirl
#endif
12 29e922b6 Blue Swirl
13 87ecb68b pbrook
/* timers */
14 87ecb68b pbrook
15 0ce1b948 Paolo Bonzini
#define SCALE_MS 1000000
16 0ce1b948 Paolo Bonzini
#define SCALE_US 1000
17 0ce1b948 Paolo Bonzini
#define SCALE_NS 1
18 0ce1b948 Paolo Bonzini
19 87ecb68b pbrook
typedef struct QEMUClock QEMUClock;
20 87ecb68b pbrook
typedef void QEMUTimerCB(void *opaque);
21 87ecb68b pbrook
22 87ecb68b pbrook
/* The real time clock should be used only for stuff which does not
23 87ecb68b pbrook
   change the virtual machine state, as it is run even if the virtual
24 87ecb68b pbrook
   machine is stopped. The real time clock has a frequency of 1000
25 87ecb68b pbrook
   Hz. */
26 87ecb68b pbrook
extern QEMUClock *rt_clock;
27 87ecb68b pbrook
28 87ecb68b pbrook
/* The virtual clock is only run during the emulation. It is stopped
29 87ecb68b pbrook
   when the virtual machine is stopped. Virtual timers use a high
30 87ecb68b pbrook
   precision clock, usually cpu cycles (use ticks_per_sec). */
31 87ecb68b pbrook
extern QEMUClock *vm_clock;
32 87ecb68b pbrook
33 21d5d12b Jan Kiszka
/* The host clock should be use for device models that emulate accurate
34 21d5d12b Jan Kiszka
   real time sources. It will continue to run when the virtual machine
35 21d5d12b Jan Kiszka
   is suspended, and it will reflect system time changes the host may
36 21d5d12b Jan Kiszka
   undergo (e.g. due to NTP). The host clock has the same precision as
37 21d5d12b Jan Kiszka
   the virtual clock. */
38 21d5d12b Jan Kiszka
extern QEMUClock *host_clock;
39 21d5d12b Jan Kiszka
40 41c872b6 Paolo Bonzini
int64_t qemu_get_clock_ns(QEMUClock *clock);
41 dc2dfcf0 Paolo Bonzini
int64_t qemu_clock_has_timers(QEMUClock *clock);
42 dc2dfcf0 Paolo Bonzini
int64_t qemu_clock_expired(QEMUClock *clock);
43 dc2dfcf0 Paolo Bonzini
int64_t qemu_clock_deadline(QEMUClock *clock);
44 db1a4972 Paolo Bonzini
void qemu_clock_enable(QEMUClock *clock, int enabled);
45 ab33fcda Paolo Bonzini
void qemu_clock_warp(QEMUClock *clock);
46 87ecb68b pbrook
47 691a0c9c Jan Kiszka
void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier);
48 691a0c9c Jan Kiszka
void qemu_unregister_clock_reset_notifier(QEMUClock *clock,
49 691a0c9c Jan Kiszka
                                          Notifier *notifier);
50 691a0c9c Jan Kiszka
51 4a998740 Paolo Bonzini
QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
52 4a998740 Paolo Bonzini
                          QEMUTimerCB *cb, void *opaque);
53 87ecb68b pbrook
void qemu_free_timer(QEMUTimer *ts);
54 87ecb68b pbrook
void qemu_del_timer(QEMUTimer *ts);
55 87ecb68b pbrook
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
56 87ecb68b pbrook
int qemu_timer_pending(QEMUTimer *ts);
57 2430ffe4 Stefano Stabellini
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
58 87ecb68b pbrook
59 db1a4972 Paolo Bonzini
void qemu_run_all_timers(void);
60 db1a4972 Paolo Bonzini
int qemu_alarm_pending(void);
61 db1a4972 Paolo Bonzini
void configure_alarms(char const *opt);
62 db1a4972 Paolo Bonzini
int qemu_calculate_timeout(void);
63 db1a4972 Paolo Bonzini
void init_clocks(void);
64 db1a4972 Paolo Bonzini
int init_timer_alarm(void);
65 db1a4972 Paolo Bonzini
66 70c3b557 Blue Swirl
int64_t cpu_get_ticks(void);
67 70c3b557 Blue Swirl
void cpu_enable_ticks(void);
68 70c3b557 Blue Swirl
void cpu_disable_ticks(void);
69 70c3b557 Blue Swirl
70 0ce1b948 Paolo Bonzini
static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,
71 0ce1b948 Paolo Bonzini
                                           void *opaque)
72 0ce1b948 Paolo Bonzini
{
73 4a998740 Paolo Bonzini
    return qemu_new_timer(clock, SCALE_NS, cb, opaque);
74 0ce1b948 Paolo Bonzini
}
75 0ce1b948 Paolo Bonzini
76 0ce1b948 Paolo Bonzini
static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb,
77 0ce1b948 Paolo Bonzini
                                           void *opaque)
78 0ce1b948 Paolo Bonzini
{
79 4a998740 Paolo Bonzini
    return qemu_new_timer(clock, SCALE_MS, cb, opaque);
80 0ce1b948 Paolo Bonzini
}
81 0ce1b948 Paolo Bonzini
82 0ce1b948 Paolo Bonzini
static inline int64_t qemu_get_clock_ms(QEMUClock *clock)
83 0ce1b948 Paolo Bonzini
{
84 0ce1b948 Paolo Bonzini
    return qemu_get_clock_ns(clock) / SCALE_MS;
85 0ce1b948 Paolo Bonzini
}
86 0ce1b948 Paolo Bonzini
87 274dfed8 Anthony Liguori
static inline int64_t get_ticks_per_sec(void)
88 274dfed8 Anthony Liguori
{
89 274dfed8 Anthony Liguori
    return 1000000000LL;
90 274dfed8 Anthony Liguori
}
91 87ecb68b pbrook
92 c57c846a Blue Swirl
/* real time host monotonic timer */
93 c57c846a Blue Swirl
static inline int64_t get_clock_realtime(void)
94 c57c846a Blue Swirl
{
95 c57c846a Blue Swirl
    struct timeval tv;
96 c57c846a Blue Swirl
97 c57c846a Blue Swirl
    gettimeofday(&tv, NULL);
98 c57c846a Blue Swirl
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
99 c57c846a Blue Swirl
}
100 c57c846a Blue Swirl
101 c57c846a Blue Swirl
/* Warning: don't insert tracepoints into these functions, they are
102 c57c846a Blue Swirl
   also used by simpletrace backend and tracepoints would cause
103 c57c846a Blue Swirl
   an infinite recursion! */
104 c57c846a Blue Swirl
#ifdef _WIN32
105 c57c846a Blue Swirl
extern int64_t clock_freq;
106 c57c846a Blue Swirl
107 c57c846a Blue Swirl
static inline int64_t get_clock(void)
108 c57c846a Blue Swirl
{
109 c57c846a Blue Swirl
    LARGE_INTEGER ti;
110 c57c846a Blue Swirl
    QueryPerformanceCounter(&ti);
111 c57c846a Blue Swirl
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
112 c57c846a Blue Swirl
}
113 c57c846a Blue Swirl
114 c57c846a Blue Swirl
#else
115 c57c846a Blue Swirl
116 c57c846a Blue Swirl
extern int use_rt_clock;
117 c57c846a Blue Swirl
118 c57c846a Blue Swirl
static inline int64_t get_clock(void)
119 c57c846a Blue Swirl
{
120 c57c846a Blue Swirl
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
121 c57c846a Blue Swirl
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
122 c57c846a Blue Swirl
    if (use_rt_clock) {
123 c57c846a Blue Swirl
        struct timespec ts;
124 c57c846a Blue Swirl
        clock_gettime(CLOCK_MONOTONIC, &ts);
125 c57c846a Blue Swirl
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
126 c57c846a Blue Swirl
    } else
127 c57c846a Blue Swirl
#endif
128 c57c846a Blue Swirl
    {
129 c57c846a Blue Swirl
        /* XXX: using gettimeofday leads to problems if the date
130 c57c846a Blue Swirl
           changes, so it should be avoided. */
131 c57c846a Blue Swirl
        return get_clock_realtime();
132 c57c846a Blue Swirl
    }
133 c57c846a Blue Swirl
}
134 c57c846a Blue Swirl
#endif
135 db1a4972 Paolo Bonzini
136 87ecb68b pbrook
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
137 87ecb68b pbrook
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
138 87ecb68b pbrook
139 87ecb68b pbrook
/* ptimer.c */
140 87ecb68b pbrook
typedef struct ptimer_state ptimer_state;
141 87ecb68b pbrook
typedef void (*ptimer_cb)(void *opaque);
142 87ecb68b pbrook
143 87ecb68b pbrook
ptimer_state *ptimer_init(QEMUBH *bh);
144 87ecb68b pbrook
void ptimer_set_period(ptimer_state *s, int64_t period);
145 87ecb68b pbrook
void ptimer_set_freq(ptimer_state *s, uint32_t freq);
146 87ecb68b pbrook
void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
147 87ecb68b pbrook
uint64_t ptimer_get_count(ptimer_state *s);
148 87ecb68b pbrook
void ptimer_set_count(ptimer_state *s, uint64_t count);
149 87ecb68b pbrook
void ptimer_run(ptimer_state *s, int oneshot);
150 87ecb68b pbrook
void ptimer_stop(ptimer_state *s);
151 87ecb68b pbrook
152 29e922b6 Blue Swirl
/* icount */
153 29e922b6 Blue Swirl
int64_t cpu_get_icount(void);
154 946fb27c Paolo Bonzini
int64_t cpu_get_clock(void);
155 29e922b6 Blue Swirl
156 29e922b6 Blue Swirl
/*******************************************/
157 29e922b6 Blue Swirl
/* host CPU ticks (if available) */
158 29e922b6 Blue Swirl
159 29e922b6 Blue Swirl
#if defined(_ARCH_PPC)
160 29e922b6 Blue Swirl
161 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
162 29e922b6 Blue Swirl
{
163 29e922b6 Blue Swirl
    int64_t retval;
164 29e922b6 Blue Swirl
#ifdef _ARCH_PPC64
165 29e922b6 Blue Swirl
    /* This reads timebase in one 64bit go and includes Cell workaround from:
166 29e922b6 Blue Swirl
       http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
167 29e922b6 Blue Swirl
    */
168 29e922b6 Blue Swirl
    __asm__ __volatile__ ("mftb    %0\n\t"
169 29e922b6 Blue Swirl
                          "cmpwi   %0,0\n\t"
170 29e922b6 Blue Swirl
                          "beq-    $-8"
171 29e922b6 Blue Swirl
                          : "=r" (retval));
172 29e922b6 Blue Swirl
#else
173 29e922b6 Blue Swirl
    /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
174 29e922b6 Blue Swirl
    unsigned long junk;
175 4a9590f3 Alexander Graf
    __asm__ __volatile__ ("mfspr   %1,269\n\t"  /* mftbu */
176 4a9590f3 Alexander Graf
                          "mfspr   %L0,268\n\t" /* mftb */
177 4a9590f3 Alexander Graf
                          "mfspr   %0,269\n\t"  /* mftbu */
178 29e922b6 Blue Swirl
                          "cmpw    %0,%1\n\t"
179 29e922b6 Blue Swirl
                          "bne     $-16"
180 29e922b6 Blue Swirl
                          : "=r" (retval), "=r" (junk));
181 29e922b6 Blue Swirl
#endif
182 29e922b6 Blue Swirl
    return retval;
183 29e922b6 Blue Swirl
}
184 29e922b6 Blue Swirl
185 29e922b6 Blue Swirl
#elif defined(__i386__)
186 29e922b6 Blue Swirl
187 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
188 29e922b6 Blue Swirl
{
189 29e922b6 Blue Swirl
    int64_t val;
190 29e922b6 Blue Swirl
    asm volatile ("rdtsc" : "=A" (val));
191 29e922b6 Blue Swirl
    return val;
192 29e922b6 Blue Swirl
}
193 29e922b6 Blue Swirl
194 29e922b6 Blue Swirl
#elif defined(__x86_64__)
195 29e922b6 Blue Swirl
196 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
197 29e922b6 Blue Swirl
{
198 29e922b6 Blue Swirl
    uint32_t low,high;
199 29e922b6 Blue Swirl
    int64_t val;
200 29e922b6 Blue Swirl
    asm volatile("rdtsc" : "=a" (low), "=d" (high));
201 29e922b6 Blue Swirl
    val = high;
202 29e922b6 Blue Swirl
    val <<= 32;
203 29e922b6 Blue Swirl
    val |= low;
204 29e922b6 Blue Swirl
    return val;
205 29e922b6 Blue Swirl
}
206 29e922b6 Blue Swirl
207 29e922b6 Blue Swirl
#elif defined(__hppa__)
208 29e922b6 Blue Swirl
209 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
210 29e922b6 Blue Swirl
{
211 29e922b6 Blue Swirl
    int val;
212 29e922b6 Blue Swirl
    asm volatile ("mfctl %%cr16, %0" : "=r"(val));
213 29e922b6 Blue Swirl
    return val;
214 29e922b6 Blue Swirl
}
215 29e922b6 Blue Swirl
216 29e922b6 Blue Swirl
#elif defined(__ia64)
217 29e922b6 Blue Swirl
218 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
219 29e922b6 Blue Swirl
{
220 29e922b6 Blue Swirl
    int64_t val;
221 29e922b6 Blue Swirl
    asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
222 29e922b6 Blue Swirl
    return val;
223 29e922b6 Blue Swirl
}
224 29e922b6 Blue Swirl
225 29e922b6 Blue Swirl
#elif defined(__s390__)
226 29e922b6 Blue Swirl
227 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
228 29e922b6 Blue Swirl
{
229 29e922b6 Blue Swirl
    int64_t val;
230 29e922b6 Blue Swirl
    asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
231 29e922b6 Blue Swirl
    return val;
232 29e922b6 Blue Swirl
}
233 29e922b6 Blue Swirl
234 29e922b6 Blue Swirl
#elif defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
235 29e922b6 Blue Swirl
236 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks (void)
237 29e922b6 Blue Swirl
{
238 29e922b6 Blue Swirl
#if defined(_LP64)
239 29e922b6 Blue Swirl
    uint64_t        rval;
240 29e922b6 Blue Swirl
    asm volatile("rd %%tick,%0" : "=r"(rval));
241 29e922b6 Blue Swirl
    return rval;
242 29e922b6 Blue Swirl
#else
243 29e922b6 Blue Swirl
    union {
244 29e922b6 Blue Swirl
        uint64_t i64;
245 29e922b6 Blue Swirl
        struct {
246 29e922b6 Blue Swirl
            uint32_t high;
247 29e922b6 Blue Swirl
            uint32_t low;
248 29e922b6 Blue Swirl
        }       i32;
249 29e922b6 Blue Swirl
    } rval;
250 29e922b6 Blue Swirl
    asm volatile("rd %%tick,%1; srlx %1,32,%0"
251 29e922b6 Blue Swirl
                 : "=r"(rval.i32.high), "=r"(rval.i32.low));
252 29e922b6 Blue Swirl
    return rval.i64;
253 29e922b6 Blue Swirl
#endif
254 29e922b6 Blue Swirl
}
255 29e922b6 Blue Swirl
256 29e922b6 Blue Swirl
#elif defined(__mips__) && \
257 29e922b6 Blue Swirl
    ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
258 29e922b6 Blue Swirl
/*
259 29e922b6 Blue Swirl
 * binutils wants to use rdhwr only on mips32r2
260 29e922b6 Blue Swirl
 * but as linux kernel emulate it, it's fine
261 29e922b6 Blue Swirl
 * to use it.
262 29e922b6 Blue Swirl
 *
263 29e922b6 Blue Swirl
 */
264 29e922b6 Blue Swirl
#define MIPS_RDHWR(rd, value) {                         \
265 29e922b6 Blue Swirl
        __asm__ __volatile__ (".set   push\n\t"         \
266 29e922b6 Blue Swirl
                              ".set mips32r2\n\t"       \
267 29e922b6 Blue Swirl
                              "rdhwr  %0, "rd"\n\t"     \
268 29e922b6 Blue Swirl
                              ".set   pop"              \
269 29e922b6 Blue Swirl
                              : "=r" (value));          \
270 29e922b6 Blue Swirl
    }
271 29e922b6 Blue Swirl
272 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
273 29e922b6 Blue Swirl
{
274 29e922b6 Blue Swirl
    /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
275 29e922b6 Blue Swirl
    uint32_t count;
276 29e922b6 Blue Swirl
    static uint32_t cyc_per_count = 0;
277 29e922b6 Blue Swirl
278 29e922b6 Blue Swirl
    if (!cyc_per_count) {
279 29e922b6 Blue Swirl
        MIPS_RDHWR("$3", cyc_per_count);
280 29e922b6 Blue Swirl
    }
281 29e922b6 Blue Swirl
282 29e922b6 Blue Swirl
    MIPS_RDHWR("$2", count);
283 29e922b6 Blue Swirl
    return (int64_t)(count * cyc_per_count);
284 29e922b6 Blue Swirl
}
285 29e922b6 Blue Swirl
286 14a6063a Richard Henderson
#elif defined(__alpha__)
287 14a6063a Richard Henderson
288 14a6063a Richard Henderson
static inline int64_t cpu_get_real_ticks(void)
289 14a6063a Richard Henderson
{
290 14a6063a Richard Henderson
    uint64_t cc;
291 14a6063a Richard Henderson
    uint32_t cur, ofs;
292 14a6063a Richard Henderson
293 14a6063a Richard Henderson
    asm volatile("rpcc %0" : "=r"(cc));
294 14a6063a Richard Henderson
    cur = cc;
295 14a6063a Richard Henderson
    ofs = cc >> 32;
296 14a6063a Richard Henderson
    return cur - ofs;
297 14a6063a Richard Henderson
}
298 14a6063a Richard Henderson
299 29e922b6 Blue Swirl
#else
300 29e922b6 Blue Swirl
/* The host CPU doesn't have an easily accessible cycle counter.
301 29e922b6 Blue Swirl
   Just return a monotonically increasing value.  This will be
302 29e922b6 Blue Swirl
   totally wrong, but hopefully better than nothing.  */
303 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks (void)
304 29e922b6 Blue Swirl
{
305 29e922b6 Blue Swirl
    static int64_t ticks = 0;
306 29e922b6 Blue Swirl
    return ticks++;
307 29e922b6 Blue Swirl
}
308 29e922b6 Blue Swirl
#endif
309 29e922b6 Blue Swirl
310 2d8ebcf9 Richard Henderson
#ifdef CONFIG_PROFILER
311 2d8ebcf9 Richard Henderson
static inline int64_t profile_getclock(void)
312 2d8ebcf9 Richard Henderson
{
313 2d8ebcf9 Richard Henderson
    return cpu_get_real_ticks();
314 2d8ebcf9 Richard Henderson
}
315 2d8ebcf9 Richard Henderson
316 2d8ebcf9 Richard Henderson
extern int64_t qemu_time, qemu_time_start;
317 2d8ebcf9 Richard Henderson
extern int64_t tlb_flush_time;
318 2d8ebcf9 Richard Henderson
extern int64_t dev_time;
319 2d8ebcf9 Richard Henderson
#endif
320 2d8ebcf9 Richard Henderson
321 87ecb68b pbrook
#endif