]> git.proxmox.com Git - qemu.git/blame - qemu-timer.h
vnc: rename vnc-encoding-* vnc-enc-*
[qemu.git] / qemu-timer.h
CommitLineData
87ecb68b
PB
1#ifndef QEMU_TIMER_H
2#define QEMU_TIMER_H
3
29e922b6
BS
4#include "qemu-common.h"
5
87ecb68b
PB
6/* timers */
7
8typedef struct QEMUClock QEMUClock;
9typedef void QEMUTimerCB(void *opaque);
10
11/* The real time clock should be used only for stuff which does not
12 change the virtual machine state, as it is run even if the virtual
13 machine is stopped. The real time clock has a frequency of 1000
14 Hz. */
15extern QEMUClock *rt_clock;
16
17/* The virtual clock is only run during the emulation. It is stopped
18 when the virtual machine is stopped. Virtual timers use a high
19 precision clock, usually cpu cycles (use ticks_per_sec). */
20extern QEMUClock *vm_clock;
21
21d5d12b
JK
22/* The host clock should be use for device models that emulate accurate
23 real time sources. It will continue to run when the virtual machine
24 is suspended, and it will reflect system time changes the host may
25 undergo (e.g. due to NTP). The host clock has the same precision as
26 the virtual clock. */
27extern QEMUClock *host_clock;
28
87ecb68b 29int64_t qemu_get_clock(QEMUClock *clock);
41c872b6 30int64_t qemu_get_clock_ns(QEMUClock *clock);
db1a4972 31void qemu_clock_enable(QEMUClock *clock, int enabled);
87ecb68b
PB
32
33QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque);
34void qemu_free_timer(QEMUTimer *ts);
35void qemu_del_timer(QEMUTimer *ts);
36void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
37int qemu_timer_pending(QEMUTimer *ts);
2430ffe4 38int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
87ecb68b 39
db1a4972
PB
40void qemu_run_all_timers(void);
41int qemu_alarm_pending(void);
42int64_t qemu_next_deadline(void);
43void configure_alarms(char const *opt);
44void configure_icount(const char *option);
45int qemu_calculate_timeout(void);
46void init_clocks(void);
47int init_timer_alarm(void);
48void quit_timers(void);
49
274dfed8
AL
50static inline int64_t get_ticks_per_sec(void)
51{
52 return 1000000000LL;
53}
87ecb68b 54
db1a4972 55
87ecb68b
PB
56void qemu_get_timer(QEMUFile *f, QEMUTimer *ts);
57void qemu_put_timer(QEMUFile *f, QEMUTimer *ts);
58
59/* ptimer.c */
60typedef struct ptimer_state ptimer_state;
61typedef void (*ptimer_cb)(void *opaque);
62
63ptimer_state *ptimer_init(QEMUBH *bh);
64void ptimer_set_period(ptimer_state *s, int64_t period);
65void ptimer_set_freq(ptimer_state *s, uint32_t freq);
66void ptimer_set_limit(ptimer_state *s, uint64_t limit, int reload);
67uint64_t ptimer_get_count(ptimer_state *s);
68void ptimer_set_count(ptimer_state *s, uint64_t count);
69void ptimer_run(ptimer_state *s, int oneshot);
70void ptimer_stop(ptimer_state *s);
71void qemu_put_ptimer(QEMUFile *f, ptimer_state *s);
72void qemu_get_ptimer(QEMUFile *f, ptimer_state *s);
73
29e922b6
BS
74/* icount */
75int64_t qemu_icount_round(int64_t count);
76extern int64_t qemu_icount;
77extern int use_icount;
78extern int icount_time_shift;
79extern int64_t qemu_icount_bias;
80int64_t cpu_get_icount(void);
81
82/*******************************************/
83/* host CPU ticks (if available) */
84
85#if defined(_ARCH_PPC)
86
87static inline int64_t cpu_get_real_ticks(void)
88{
89 int64_t retval;
90#ifdef _ARCH_PPC64
91 /* This reads timebase in one 64bit go and includes Cell workaround from:
92 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
93 */
94 __asm__ __volatile__ ("mftb %0\n\t"
95 "cmpwi %0,0\n\t"
96 "beq- $-8"
97 : "=r" (retval));
98#else
99 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
100 unsigned long junk;
4a9590f3
AG
101 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
102 "mfspr %L0,268\n\t" /* mftb */
103 "mfspr %0,269\n\t" /* mftbu */
29e922b6
BS
104 "cmpw %0,%1\n\t"
105 "bne $-16"
106 : "=r" (retval), "=r" (junk));
107#endif
108 return retval;
109}
110
111#elif defined(__i386__)
112
113static inline int64_t cpu_get_real_ticks(void)
114{
115 int64_t val;
116 asm volatile ("rdtsc" : "=A" (val));
117 return val;
118}
119
120#elif defined(__x86_64__)
121
122static inline int64_t cpu_get_real_ticks(void)
123{
124 uint32_t low,high;
125 int64_t val;
126 asm volatile("rdtsc" : "=a" (low), "=d" (high));
127 val = high;
128 val <<= 32;
129 val |= low;
130 return val;
131}
132
133#elif defined(__hppa__)
134
135static inline int64_t cpu_get_real_ticks(void)
136{
137 int val;
138 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
139 return val;
140}
141
142#elif defined(__ia64)
143
144static inline int64_t cpu_get_real_ticks(void)
145{
146 int64_t val;
147 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
148 return val;
149}
150
151#elif defined(__s390__)
152
153static inline int64_t cpu_get_real_ticks(void)
154{
155 int64_t val;
156 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
157 return val;
158}
159
160#elif defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
161
162static inline int64_t cpu_get_real_ticks (void)
163{
164#if defined(_LP64)
165 uint64_t rval;
166 asm volatile("rd %%tick,%0" : "=r"(rval));
167 return rval;
168#else
169 union {
170 uint64_t i64;
171 struct {
172 uint32_t high;
173 uint32_t low;
174 } i32;
175 } rval;
176 asm volatile("rd %%tick,%1; srlx %1,32,%0"
177 : "=r"(rval.i32.high), "=r"(rval.i32.low));
178 return rval.i64;
179#endif
180}
181
182#elif defined(__mips__) && \
183 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
184/*
185 * binutils wants to use rdhwr only on mips32r2
186 * but as linux kernel emulate it, it's fine
187 * to use it.
188 *
189 */
190#define MIPS_RDHWR(rd, value) { \
191 __asm__ __volatile__ (".set push\n\t" \
192 ".set mips32r2\n\t" \
193 "rdhwr %0, "rd"\n\t" \
194 ".set pop" \
195 : "=r" (value)); \
196 }
197
198static inline int64_t cpu_get_real_ticks(void)
199{
200 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
201 uint32_t count;
202 static uint32_t cyc_per_count = 0;
203
204 if (!cyc_per_count) {
205 MIPS_RDHWR("$3", cyc_per_count);
206 }
207
208 MIPS_RDHWR("$2", count);
209 return (int64_t)(count * cyc_per_count);
210}
211
14a6063a
RH
212#elif defined(__alpha__)
213
214static inline int64_t cpu_get_real_ticks(void)
215{
216 uint64_t cc;
217 uint32_t cur, ofs;
218
219 asm volatile("rpcc %0" : "=r"(cc));
220 cur = cc;
221 ofs = cc >> 32;
222 return cur - ofs;
223}
224
29e922b6
BS
225#else
226/* The host CPU doesn't have an easily accessible cycle counter.
227 Just return a monotonically increasing value. This will be
228 totally wrong, but hopefully better than nothing. */
229static inline int64_t cpu_get_real_ticks (void)
230{
231 static int64_t ticks = 0;
232 return ticks++;
233}
234#endif
235
236#ifdef NEED_CPU_H
237/* Deterministic execution requires that IO only be performed on the last
238 instruction of a TB so that interrupts take effect immediately. */
239static inline int can_do_io(CPUState *env)
240{
241 if (!use_icount)
242 return 1;
243
244 /* If not executing code then assume we are ok. */
245 if (!env->current_tb)
246 return 1;
247
248 return env->can_do_io != 0;
249}
250#endif
251
2d8ebcf9
RH
252#ifdef CONFIG_PROFILER
253static inline int64_t profile_getclock(void)
254{
255 return cpu_get_real_ticks();
256}
257
258extern int64_t qemu_time, qemu_time_start;
259extern int64_t tlb_flush_time;
260extern int64_t dev_time;
261#endif
262
87ecb68b 263#endif