Statistics
| Branch: | Revision:

root / qemu-timer.h @ 0ce1b948

History | View | Annotate | Download (8.8 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 c57c846a Blue Swirl
#include <time.h>
6 c57c846a Blue Swirl
#include <sys/time.h>
7 c57c846a Blue Swirl
8 c57c846a Blue Swirl
#ifdef _WIN32
9 c57c846a Blue Swirl
#include <windows.h>
10 c57c846a Blue Swirl
#include <mmsystem.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 87ecb68b pbrook
int64_t qemu_get_clock(QEMUClock *clock);
41 41c872b6 Paolo Bonzini
int64_t qemu_get_clock_ns(QEMUClock *clock);
42 db1a4972 Paolo Bonzini
void qemu_clock_enable(QEMUClock *clock, int enabled);
43 87ecb68b pbrook
44 87ecb68b pbrook
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
45 87ecb68b pbrook
void qemu_free_timer(QEMUTimer *ts);
46 87ecb68b pbrook
void qemu_del_timer(QEMUTimer *ts);
47 87ecb68b pbrook
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
48 87ecb68b pbrook
int qemu_timer_pending(QEMUTimer *ts);
49 2430ffe4 Stefano Stabellini
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
50 87ecb68b pbrook
51 db1a4972 Paolo Bonzini
void qemu_run_all_timers(void);
52 db1a4972 Paolo Bonzini
int qemu_alarm_pending(void);
53 db1a4972 Paolo Bonzini
int64_t qemu_next_deadline(void);
54 db1a4972 Paolo Bonzini
void configure_alarms(char const *opt);
55 db1a4972 Paolo Bonzini
void configure_icount(const char *option);
56 db1a4972 Paolo Bonzini
int qemu_calculate_timeout(void);
57 db1a4972 Paolo Bonzini
void init_clocks(void);
58 db1a4972 Paolo Bonzini
int init_timer_alarm(void);
59 db1a4972 Paolo Bonzini
void quit_timers(void);
60 db1a4972 Paolo Bonzini
61 0ce1b948 Paolo Bonzini
static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,
62 0ce1b948 Paolo Bonzini
                                           void *opaque)
63 0ce1b948 Paolo Bonzini
{
64 0ce1b948 Paolo Bonzini
    assert(clock != rt_clock);
65 0ce1b948 Paolo Bonzini
    return qemu_new_timer(clock, cb, opaque);
66 0ce1b948 Paolo Bonzini
}
67 0ce1b948 Paolo Bonzini
68 0ce1b948 Paolo Bonzini
static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, QEMUTimerCB *cb,
69 0ce1b948 Paolo Bonzini
                                           void *opaque)
70 0ce1b948 Paolo Bonzini
{
71 0ce1b948 Paolo Bonzini
    assert(clock == rt_clock);
72 0ce1b948 Paolo Bonzini
    return qemu_new_timer(clock, cb, opaque);
73 0ce1b948 Paolo Bonzini
}
74 0ce1b948 Paolo Bonzini
75 0ce1b948 Paolo Bonzini
static inline int64_t qemu_get_clock_ms(QEMUClock *clock)
76 0ce1b948 Paolo Bonzini
{
77 0ce1b948 Paolo Bonzini
    return qemu_get_clock_ns(clock) / SCALE_MS;
78 0ce1b948 Paolo Bonzini
}
79 0ce1b948 Paolo Bonzini
80 274dfed8 Anthony Liguori
static inline int64_t get_ticks_per_sec(void)
81 274dfed8 Anthony Liguori
{
82 274dfed8 Anthony Liguori
    return 1000000000LL;
83 274dfed8 Anthony Liguori
}
84 87ecb68b pbrook
85 c57c846a Blue Swirl
/* real time host monotonic timer */
86 c57c846a Blue Swirl
static inline int64_t get_clock_realtime(void)
87 c57c846a Blue Swirl
{
88 c57c846a Blue Swirl
    struct timeval tv;
89 c57c846a Blue Swirl
90 c57c846a Blue Swirl
    gettimeofday(&tv, NULL);
91 c57c846a Blue Swirl
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
92 c57c846a Blue Swirl
}
93 c57c846a Blue Swirl
94 c57c846a Blue Swirl
/* Warning: don't insert tracepoints into these functions, they are
95 c57c846a Blue Swirl
   also used by simpletrace backend and tracepoints would cause
96 c57c846a Blue Swirl
   an infinite recursion! */
97 c57c846a Blue Swirl
#ifdef _WIN32
98 c57c846a Blue Swirl
extern int64_t clock_freq;
99 c57c846a Blue Swirl
100 c57c846a Blue Swirl
static inline int64_t get_clock(void)
101 c57c846a Blue Swirl
{
102 c57c846a Blue Swirl
    LARGE_INTEGER ti;
103 c57c846a Blue Swirl
    QueryPerformanceCounter(&ti);
104 c57c846a Blue Swirl
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
105 c57c846a Blue Swirl
}
106 c57c846a Blue Swirl
107 c57c846a Blue Swirl
#else
108 c57c846a Blue Swirl
109 c57c846a Blue Swirl
extern int use_rt_clock;
110 c57c846a Blue Swirl
111 c57c846a Blue Swirl
static inline int64_t get_clock(void)
112 c57c846a Blue Swirl
{
113 c57c846a Blue Swirl
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
114 c57c846a Blue Swirl
    || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
115 c57c846a Blue Swirl
    if (use_rt_clock) {
116 c57c846a Blue Swirl
        struct timespec ts;
117 c57c846a Blue Swirl
        clock_gettime(CLOCK_MONOTONIC, &ts);
118 c57c846a Blue Swirl
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
119 c57c846a Blue Swirl
    } else
120 c57c846a Blue Swirl
#endif
121 c57c846a Blue Swirl
    {
122 c57c846a Blue Swirl
        /* XXX: using gettimeofday leads to problems if the date
123 c57c846a Blue Swirl
           changes, so it should be avoided. */
124 c57c846a Blue Swirl
        return get_clock_realtime();
125 c57c846a Blue Swirl
    }
126 c57c846a Blue Swirl
}
127 c57c846a Blue Swirl
#endif
128 db1a4972 Paolo Bonzini
129 87ecb68b pbrook
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
130 87ecb68b pbrook
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
131 87ecb68b pbrook
132 87ecb68b pbrook
/* ptimer.c */
133 87ecb68b pbrook
typedef struct ptimer_state ptimer_state;
134 87ecb68b pbrook
typedef void (*ptimer_cb)(void *opaque);
135 87ecb68b pbrook
136 87ecb68b pbrook
ptimer_state *ptimer_init(QEMUBH *bh);
137 87ecb68b pbrook
void ptimer_set_period(ptimer_state *s, int64_t period);
138 87ecb68b pbrook
void ptimer_set_freq(ptimer_state *s, uint32_t freq);
139 87ecb68b pbrook
void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
140 87ecb68b pbrook
uint64_t ptimer_get_count(ptimer_state *s);
141 87ecb68b pbrook
void ptimer_set_count(ptimer_state *s, uint64_t count);
142 87ecb68b pbrook
void ptimer_run(ptimer_state *s, int oneshot);
143 87ecb68b pbrook
void ptimer_stop(ptimer_state *s);
144 87ecb68b pbrook
void qemu_put_ptimer(QEMUFile *f, ptimer_state *s);
145 87ecb68b pbrook
void qemu_get_ptimer(QEMUFile *f, ptimer_state *s);
146 87ecb68b pbrook
147 29e922b6 Blue Swirl
/* icount */
148 29e922b6 Blue Swirl
int64_t qemu_icount_round(int64_t count);
149 29e922b6 Blue Swirl
extern int64_t qemu_icount;
150 29e922b6 Blue Swirl
extern int use_icount;
151 29e922b6 Blue Swirl
extern int icount_time_shift;
152 29e922b6 Blue Swirl
extern int64_t qemu_icount_bias;
153 29e922b6 Blue Swirl
int64_t cpu_get_icount(void);
154 29e922b6 Blue Swirl
155 29e922b6 Blue Swirl
/*******************************************/
156 29e922b6 Blue Swirl
/* host CPU ticks (if available) */
157 29e922b6 Blue Swirl
158 29e922b6 Blue Swirl
#if defined(_ARCH_PPC)
159 29e922b6 Blue Swirl
160 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
161 29e922b6 Blue Swirl
{
162 29e922b6 Blue Swirl
    int64_t retval;
163 29e922b6 Blue Swirl
#ifdef _ARCH_PPC64
164 29e922b6 Blue Swirl
    /* This reads timebase in one 64bit go and includes Cell workaround from:
165 29e922b6 Blue Swirl
       http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
166 29e922b6 Blue Swirl
    */
167 29e922b6 Blue Swirl
    __asm__ __volatile__ ("mftb    %0\n\t"
168 29e922b6 Blue Swirl
                          "cmpwi   %0,0\n\t"
169 29e922b6 Blue Swirl
                          "beq-    $-8"
170 29e922b6 Blue Swirl
                          : "=r" (retval));
171 29e922b6 Blue Swirl
#else
172 29e922b6 Blue Swirl
    /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
173 29e922b6 Blue Swirl
    unsigned long junk;
174 4a9590f3 Alexander Graf
    __asm__ __volatile__ ("mfspr   %1,269\n\t"  /* mftbu */
175 4a9590f3 Alexander Graf
                          "mfspr   %L0,268\n\t" /* mftb */
176 4a9590f3 Alexander Graf
                          "mfspr   %0,269\n\t"  /* mftbu */
177 29e922b6 Blue Swirl
                          "cmpw    %0,%1\n\t"
178 29e922b6 Blue Swirl
                          "bne     $-16"
179 29e922b6 Blue Swirl
                          : "=r" (retval), "=r" (junk));
180 29e922b6 Blue Swirl
#endif
181 29e922b6 Blue Swirl
    return retval;
182 29e922b6 Blue Swirl
}
183 29e922b6 Blue Swirl
184 29e922b6 Blue Swirl
#elif defined(__i386__)
185 29e922b6 Blue Swirl
186 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
187 29e922b6 Blue Swirl
{
188 29e922b6 Blue Swirl
    int64_t val;
189 29e922b6 Blue Swirl
    asm volatile ("rdtsc" : "=A" (val));
190 29e922b6 Blue Swirl
    return val;
191 29e922b6 Blue Swirl
}
192 29e922b6 Blue Swirl
193 29e922b6 Blue Swirl
#elif defined(__x86_64__)
194 29e922b6 Blue Swirl
195 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
196 29e922b6 Blue Swirl
{
197 29e922b6 Blue Swirl
    uint32_t low,high;
198 29e922b6 Blue Swirl
    int64_t val;
199 29e922b6 Blue Swirl
    asm volatile("rdtsc" : "=a" (low), "=d" (high));
200 29e922b6 Blue Swirl
    val = high;
201 29e922b6 Blue Swirl
    val <<= 32;
202 29e922b6 Blue Swirl
    val |= low;
203 29e922b6 Blue Swirl
    return val;
204 29e922b6 Blue Swirl
}
205 29e922b6 Blue Swirl
206 29e922b6 Blue Swirl
#elif defined(__hppa__)
207 29e922b6 Blue Swirl
208 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
209 29e922b6 Blue Swirl
{
210 29e922b6 Blue Swirl
    int val;
211 29e922b6 Blue Swirl
    asm volatile ("mfctl %%cr16, %0" : "=r"(val));
212 29e922b6 Blue Swirl
    return val;
213 29e922b6 Blue Swirl
}
214 29e922b6 Blue Swirl
215 29e922b6 Blue Swirl
#elif defined(__ia64)
216 29e922b6 Blue Swirl
217 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
218 29e922b6 Blue Swirl
{
219 29e922b6 Blue Swirl
    int64_t val;
220 29e922b6 Blue Swirl
    asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
221 29e922b6 Blue Swirl
    return val;
222 29e922b6 Blue Swirl
}
223 29e922b6 Blue Swirl
224 29e922b6 Blue Swirl
#elif defined(__s390__)
225 29e922b6 Blue Swirl
226 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
227 29e922b6 Blue Swirl
{
228 29e922b6 Blue Swirl
    int64_t val;
229 29e922b6 Blue Swirl
    asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
230 29e922b6 Blue Swirl
    return val;
231 29e922b6 Blue Swirl
}
232 29e922b6 Blue Swirl
233 29e922b6 Blue Swirl
#elif defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
234 29e922b6 Blue Swirl
235 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks (void)
236 29e922b6 Blue Swirl
{
237 29e922b6 Blue Swirl
#if defined(_LP64)
238 29e922b6 Blue Swirl
    uint64_t        rval;
239 29e922b6 Blue Swirl
    asm volatile("rd %%tick,%0" : "=r"(rval));
240 29e922b6 Blue Swirl
    return rval;
241 29e922b6 Blue Swirl
#else
242 29e922b6 Blue Swirl
    union {
243 29e922b6 Blue Swirl
        uint64_t i64;
244 29e922b6 Blue Swirl
        struct {
245 29e922b6 Blue Swirl
            uint32_t high;
246 29e922b6 Blue Swirl
            uint32_t low;
247 29e922b6 Blue Swirl
        }       i32;
248 29e922b6 Blue Swirl
    } rval;
249 29e922b6 Blue Swirl
    asm volatile("rd %%tick,%1; srlx %1,32,%0"
250 29e922b6 Blue Swirl
                 : "=r"(rval.i32.high), "=r"(rval.i32.low));
251 29e922b6 Blue Swirl
    return rval.i64;
252 29e922b6 Blue Swirl
#endif
253 29e922b6 Blue Swirl
}
254 29e922b6 Blue Swirl
255 29e922b6 Blue Swirl
#elif defined(__mips__) && \
256 29e922b6 Blue Swirl
    ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
257 29e922b6 Blue Swirl
/*
258 29e922b6 Blue Swirl
 * binutils wants to use rdhwr only on mips32r2
259 29e922b6 Blue Swirl
 * but as linux kernel emulate it, it's fine
260 29e922b6 Blue Swirl
 * to use it.
261 29e922b6 Blue Swirl
 *
262 29e922b6 Blue Swirl
 */
263 29e922b6 Blue Swirl
#define MIPS_RDHWR(rd, value) {                         \
264 29e922b6 Blue Swirl
        __asm__ __volatile__ (".set   push\n\t"         \
265 29e922b6 Blue Swirl
                              ".set mips32r2\n\t"       \
266 29e922b6 Blue Swirl
                              "rdhwr  %0, "rd"\n\t"     \
267 29e922b6 Blue Swirl
                              ".set   pop"              \
268 29e922b6 Blue Swirl
                              : "=r" (value));          \
269 29e922b6 Blue Swirl
    }
270 29e922b6 Blue Swirl
271 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks(void)
272 29e922b6 Blue Swirl
{
273 29e922b6 Blue Swirl
    /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
274 29e922b6 Blue Swirl
    uint32_t count;
275 29e922b6 Blue Swirl
    static uint32_t cyc_per_count = 0;
276 29e922b6 Blue Swirl
277 29e922b6 Blue Swirl
    if (!cyc_per_count) {
278 29e922b6 Blue Swirl
        MIPS_RDHWR("$3", cyc_per_count);
279 29e922b6 Blue Swirl
    }
280 29e922b6 Blue Swirl
281 29e922b6 Blue Swirl
    MIPS_RDHWR("$2", count);
282 29e922b6 Blue Swirl
    return (int64_t)(count * cyc_per_count);
283 29e922b6 Blue Swirl
}
284 29e922b6 Blue Swirl
285 14a6063a Richard Henderson
#elif defined(__alpha__)
286 14a6063a Richard Henderson
287 14a6063a Richard Henderson
static inline int64_t cpu_get_real_ticks(void)
288 14a6063a Richard Henderson
{
289 14a6063a Richard Henderson
    uint64_t cc;
290 14a6063a Richard Henderson
    uint32_t cur, ofs;
291 14a6063a Richard Henderson
292 14a6063a Richard Henderson
    asm volatile("rpcc %0" : "=r"(cc));
293 14a6063a Richard Henderson
    cur = cc;
294 14a6063a Richard Henderson
    ofs = cc >> 32;
295 14a6063a Richard Henderson
    return cur - ofs;
296 14a6063a Richard Henderson
}
297 14a6063a Richard Henderson
298 29e922b6 Blue Swirl
#else
299 29e922b6 Blue Swirl
/* The host CPU doesn't have an easily accessible cycle counter.
300 29e922b6 Blue Swirl
   Just return a monotonically increasing value.  This will be
301 29e922b6 Blue Swirl
   totally wrong, but hopefully better than nothing.  */
302 29e922b6 Blue Swirl
static inline int64_t cpu_get_real_ticks (void)
303 29e922b6 Blue Swirl
{
304 29e922b6 Blue Swirl
    static int64_t ticks = 0;
305 29e922b6 Blue Swirl
    return ticks++;
306 29e922b6 Blue Swirl
}
307 29e922b6 Blue Swirl
#endif
308 29e922b6 Blue Swirl
309 29e922b6 Blue Swirl
#ifdef NEED_CPU_H
310 29e922b6 Blue Swirl
/* Deterministic execution requires that IO only be performed on the last
311 29e922b6 Blue Swirl
   instruction of a TB so that interrupts take effect immediately.  */
312 29e922b6 Blue Swirl
static inline int can_do_io(CPUState *env)
313 29e922b6 Blue Swirl
{
314 29e922b6 Blue Swirl
    if (!use_icount)
315 29e922b6 Blue Swirl
        return 1;
316 29e922b6 Blue Swirl
317 29e922b6 Blue Swirl
    /* If not executing code then assume we are ok.  */
318 29e922b6 Blue Swirl
    if (!env->current_tb)
319 29e922b6 Blue Swirl
        return 1;
320 29e922b6 Blue Swirl
321 29e922b6 Blue Swirl
    return env->can_do_io != 0;
322 29e922b6 Blue Swirl
}
323 29e922b6 Blue Swirl
#endif
324 29e922b6 Blue Swirl
325 2d8ebcf9 Richard Henderson
#ifdef CONFIG_PROFILER
326 2d8ebcf9 Richard Henderson
static inline int64_t profile_getclock(void)
327 2d8ebcf9 Richard Henderson
{
328 2d8ebcf9 Richard Henderson
    return cpu_get_real_ticks();
329 2d8ebcf9 Richard Henderson
}
330 2d8ebcf9 Richard Henderson
331 2d8ebcf9 Richard Henderson
extern int64_t qemu_time, qemu_time_start;
332 2d8ebcf9 Richard Henderson
extern int64_t tlb_flush_time;
333 2d8ebcf9 Richard Henderson
extern int64_t dev_time;
334 2d8ebcf9 Richard Henderson
#endif
335 2d8ebcf9 Richard Henderson
336 87ecb68b pbrook
#endif