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