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