root / qemu-timer.h @ e3e87df4
History | View | Annotate | Download (8.1 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 | 44a9b356 | Paolo Bonzini | #include "main-loop.h" |
6 | 691a0c9c | Jan Kiszka | #include "notify.h" |
7 | 29e922b6 | Blue Swirl | |
8 | 165ceac0 | Andreas Faerber | #ifdef __FreeBSD__
|
9 | 165ceac0 | Andreas Faerber | #include <sys/param.h> |
10 | 165ceac0 | Andreas Faerber | #endif
|
11 | 165ceac0 | Andreas Faerber | |
12 | 87ecb68b | pbrook | /* timers */
|
13 | 87ecb68b | pbrook | |
14 | 0ce1b948 | Paolo Bonzini | #define SCALE_MS 1000000 |
15 | 0ce1b948 | Paolo Bonzini | #define SCALE_US 1000 |
16 | 0ce1b948 | Paolo Bonzini | #define SCALE_NS 1 |
17 | 0ce1b948 | Paolo Bonzini | |
18 | 87ecb68b | pbrook | typedef struct QEMUClock QEMUClock; |
19 | 87ecb68b | pbrook | typedef void QEMUTimerCB(void *opaque); |
20 | 87ecb68b | pbrook | |
21 | 87ecb68b | pbrook | /* The real time clock should be used only for stuff which does not
|
22 | 87ecb68b | pbrook | change the virtual machine state, as it is run even if the virtual
|
23 | 87ecb68b | pbrook | machine is stopped. The real time clock has a frequency of 1000
|
24 | 87ecb68b | pbrook | Hz. */
|
25 | 87ecb68b | pbrook | extern QEMUClock *rt_clock;
|
26 | 87ecb68b | pbrook | |
27 | 87ecb68b | pbrook | /* The virtual clock is only run during the emulation. It is stopped
|
28 | 87ecb68b | pbrook | when the virtual machine is stopped. Virtual timers use a high
|
29 | 87ecb68b | pbrook | precision clock, usually cpu cycles (use ticks_per_sec). */
|
30 | 87ecb68b | pbrook | extern QEMUClock *vm_clock;
|
31 | 87ecb68b | pbrook | |
32 | 21d5d12b | Jan Kiszka | /* The host clock should be use for device models that emulate accurate
|
33 | 21d5d12b | Jan Kiszka | real time sources. It will continue to run when the virtual machine
|
34 | 21d5d12b | Jan Kiszka | is suspended, and it will reflect system time changes the host may
|
35 | 21d5d12b | Jan Kiszka | undergo (e.g. due to NTP). The host clock has the same precision as
|
36 | 21d5d12b | Jan Kiszka | the virtual clock. */
|
37 | 21d5d12b | Jan Kiszka | extern QEMUClock *host_clock;
|
38 | 21d5d12b | Jan Kiszka | |
39 | 41c872b6 | Paolo Bonzini | int64_t qemu_get_clock_ns(QEMUClock *clock); |
40 | dc2dfcf0 | Paolo Bonzini | int64_t qemu_clock_has_timers(QEMUClock *clock); |
41 | dc2dfcf0 | Paolo Bonzini | int64_t qemu_clock_expired(QEMUClock *clock); |
42 | dc2dfcf0 | Paolo Bonzini | int64_t qemu_clock_deadline(QEMUClock *clock); |
43 | 5e1ec7b2 | Stefan Weil | void qemu_clock_enable(QEMUClock *clock, bool enabled); |
44 | ab33fcda | Paolo Bonzini | void qemu_clock_warp(QEMUClock *clock);
|
45 | 87ecb68b | pbrook | |
46 | 691a0c9c | Jan Kiszka | void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier);
|
47 | 691a0c9c | Jan Kiszka | void qemu_unregister_clock_reset_notifier(QEMUClock *clock,
|
48 | 691a0c9c | Jan Kiszka | Notifier *notifier); |
49 | 691a0c9c | Jan Kiszka | |
50 | 4a998740 | Paolo Bonzini | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
|
51 | 4a998740 | Paolo Bonzini | QEMUTimerCB *cb, void *opaque);
|
52 | 87ecb68b | pbrook | void qemu_free_timer(QEMUTimer *ts);
|
53 | 87ecb68b | pbrook | void qemu_del_timer(QEMUTimer *ts);
|
54 | 2ff68d07 | Paolo Bonzini | void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time);
|
55 | 87ecb68b | pbrook | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time);
|
56 | 5e1ec7b2 | Stefan Weil | bool qemu_timer_pending(QEMUTimer *ts);
|
57 | 5e1ec7b2 | Stefan Weil | bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time);
|
58 | 2ff68d07 | Paolo Bonzini | uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts); |
59 | 87ecb68b | pbrook | |
60 | 8156be56 | Paolo Bonzini | void qemu_run_timers(QEMUClock *clock);
|
61 | db1a4972 | Paolo Bonzini | void qemu_run_all_timers(void); |
62 | db1a4972 | Paolo Bonzini | void configure_alarms(char const *opt); |
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 | 29e922b6 | Blue Swirl | /* icount */
|
140 | 29e922b6 | Blue Swirl | int64_t cpu_get_icount(void);
|
141 | 946fb27c | Paolo Bonzini | int64_t cpu_get_clock(void);
|
142 | 29e922b6 | Blue Swirl | |
143 | 29e922b6 | Blue Swirl | /*******************************************/
|
144 | 29e922b6 | Blue Swirl | /* host CPU ticks (if available) */
|
145 | 29e922b6 | Blue Swirl | |
146 | 29e922b6 | Blue Swirl | #if defined(_ARCH_PPC)
|
147 | 29e922b6 | Blue Swirl | |
148 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
149 | 29e922b6 | Blue Swirl | { |
150 | 29e922b6 | Blue Swirl | int64_t retval; |
151 | 29e922b6 | Blue Swirl | #ifdef _ARCH_PPC64
|
152 | 29e922b6 | Blue Swirl | /* This reads timebase in one 64bit go and includes Cell workaround from:
|
153 | 29e922b6 | Blue Swirl | http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
|
154 | 29e922b6 | Blue Swirl | */
|
155 | 29e922b6 | Blue Swirl | __asm__ __volatile__ ("mftb %0\n\t"
|
156 | 29e922b6 | Blue Swirl | "cmpwi %0,0\n\t"
|
157 | 29e922b6 | Blue Swirl | "beq- $-8"
|
158 | 29e922b6 | Blue Swirl | : "=r" (retval));
|
159 | 29e922b6 | Blue Swirl | #else
|
160 | 29e922b6 | Blue Swirl | /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
|
161 | 29e922b6 | Blue Swirl | unsigned long junk; |
162 | 4a9590f3 | Alexander Graf | __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */ |
163 | 4a9590f3 | Alexander Graf | "mfspr %L0,268\n\t" /* mftb */ |
164 | 4a9590f3 | Alexander Graf | "mfspr %0,269\n\t" /* mftbu */ |
165 | 29e922b6 | Blue Swirl | "cmpw %0,%1\n\t"
|
166 | 29e922b6 | Blue Swirl | "bne $-16"
|
167 | 29e922b6 | Blue Swirl | : "=r" (retval), "=r" (junk)); |
168 | 29e922b6 | Blue Swirl | #endif
|
169 | 29e922b6 | Blue Swirl | return retval;
|
170 | 29e922b6 | Blue Swirl | } |
171 | 29e922b6 | Blue Swirl | |
172 | 29e922b6 | Blue Swirl | #elif defined(__i386__)
|
173 | 29e922b6 | Blue Swirl | |
174 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
175 | 29e922b6 | Blue Swirl | { |
176 | 29e922b6 | Blue Swirl | int64_t val; |
177 | 29e922b6 | Blue Swirl | asm volatile ("rdtsc" : "=A" (val)); |
178 | 29e922b6 | Blue Swirl | return val;
|
179 | 29e922b6 | Blue Swirl | } |
180 | 29e922b6 | Blue Swirl | |
181 | 29e922b6 | Blue Swirl | #elif defined(__x86_64__)
|
182 | 29e922b6 | Blue Swirl | |
183 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
184 | 29e922b6 | Blue Swirl | { |
185 | 29e922b6 | Blue Swirl | uint32_t low,high; |
186 | 29e922b6 | Blue Swirl | int64_t val; |
187 | 29e922b6 | Blue Swirl | asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
188 | 29e922b6 | Blue Swirl | val = high; |
189 | 29e922b6 | Blue Swirl | val <<= 32;
|
190 | 29e922b6 | Blue Swirl | val |= low; |
191 | 29e922b6 | Blue Swirl | return val;
|
192 | 29e922b6 | Blue Swirl | } |
193 | 29e922b6 | Blue Swirl | |
194 | 29e922b6 | Blue Swirl | #elif defined(__hppa__)
|
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 | int val;
|
199 | 29e922b6 | Blue Swirl | asm volatile ("mfctl %%cr16, %0" : "=r"(val)); |
200 | 29e922b6 | Blue Swirl | return val;
|
201 | 29e922b6 | Blue Swirl | } |
202 | 29e922b6 | Blue Swirl | |
203 | 29e922b6 | Blue Swirl | #elif defined(__ia64)
|
204 | 29e922b6 | Blue Swirl | |
205 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
206 | 29e922b6 | Blue Swirl | { |
207 | 29e922b6 | Blue Swirl | int64_t val; |
208 | 29e922b6 | Blue Swirl | asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory"); |
209 | 29e922b6 | Blue Swirl | return val;
|
210 | 29e922b6 | Blue Swirl | } |
211 | 29e922b6 | Blue Swirl | |
212 | 29e922b6 | Blue Swirl | #elif defined(__s390__)
|
213 | 29e922b6 | Blue Swirl | |
214 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
215 | 29e922b6 | Blue Swirl | { |
216 | 29e922b6 | Blue Swirl | int64_t val; |
217 | 29e922b6 | Blue Swirl | asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc"); |
218 | 29e922b6 | Blue Swirl | return val;
|
219 | 29e922b6 | Blue Swirl | } |
220 | 29e922b6 | Blue Swirl | |
221 | 29e922b6 | Blue Swirl | #elif defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || defined(__sparc_v9__)
|
222 | 29e922b6 | Blue Swirl | |
223 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks (void) |
224 | 29e922b6 | Blue Swirl | { |
225 | 29e922b6 | Blue Swirl | #if defined(_LP64)
|
226 | 29e922b6 | Blue Swirl | uint64_t rval; |
227 | 29e922b6 | Blue Swirl | asm volatile("rd %%tick,%0" : "=r"(rval)); |
228 | 29e922b6 | Blue Swirl | return rval;
|
229 | 29e922b6 | Blue Swirl | #else
|
230 | 29e922b6 | Blue Swirl | union {
|
231 | 29e922b6 | Blue Swirl | uint64_t i64; |
232 | 29e922b6 | Blue Swirl | struct {
|
233 | 29e922b6 | Blue Swirl | uint32_t high; |
234 | 29e922b6 | Blue Swirl | uint32_t low; |
235 | 29e922b6 | Blue Swirl | } i32; |
236 | 29e922b6 | Blue Swirl | } rval; |
237 | 29e922b6 | Blue Swirl | asm volatile("rd %%tick,%1; srlx %1,32,%0" |
238 | 29e922b6 | Blue Swirl | : "=r"(rval.i32.high), "=r"(rval.i32.low)); |
239 | 29e922b6 | Blue Swirl | return rval.i64;
|
240 | 29e922b6 | Blue Swirl | #endif
|
241 | 29e922b6 | Blue Swirl | } |
242 | 29e922b6 | Blue Swirl | |
243 | 29e922b6 | Blue Swirl | #elif defined(__mips__) && \
|
244 | 29e922b6 | Blue Swirl | ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
|
245 | 29e922b6 | Blue Swirl | /*
|
246 | 29e922b6 | Blue Swirl | * binutils wants to use rdhwr only on mips32r2
|
247 | 29e922b6 | Blue Swirl | * but as linux kernel emulate it, it's fine
|
248 | 29e922b6 | Blue Swirl | * to use it.
|
249 | 29e922b6 | Blue Swirl | *
|
250 | 29e922b6 | Blue Swirl | */
|
251 | 29e922b6 | Blue Swirl | #define MIPS_RDHWR(rd, value) { \
|
252 | 29e922b6 | Blue Swirl | __asm__ __volatile__ (".set push\n\t" \
|
253 | 29e922b6 | Blue Swirl | ".set mips32r2\n\t" \
|
254 | 29e922b6 | Blue Swirl | "rdhwr %0, "rd"\n\t" \ |
255 | 29e922b6 | Blue Swirl | ".set pop" \
|
256 | 29e922b6 | Blue Swirl | : "=r" (value)); \
|
257 | 29e922b6 | Blue Swirl | } |
258 | 29e922b6 | Blue Swirl | |
259 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks(void) |
260 | 29e922b6 | Blue Swirl | { |
261 | 29e922b6 | Blue Swirl | /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
|
262 | 29e922b6 | Blue Swirl | uint32_t count; |
263 | 29e922b6 | Blue Swirl | static uint32_t cyc_per_count = 0; |
264 | 29e922b6 | Blue Swirl | |
265 | 29e922b6 | Blue Swirl | if (!cyc_per_count) {
|
266 | 29e922b6 | Blue Swirl | MIPS_RDHWR("$3", cyc_per_count);
|
267 | 29e922b6 | Blue Swirl | } |
268 | 29e922b6 | Blue Swirl | |
269 | 29e922b6 | Blue Swirl | MIPS_RDHWR("$2", count);
|
270 | 29e922b6 | Blue Swirl | return (int64_t)(count * cyc_per_count);
|
271 | 29e922b6 | Blue Swirl | } |
272 | 29e922b6 | Blue Swirl | |
273 | 14a6063a | Richard Henderson | #elif defined(__alpha__)
|
274 | 14a6063a | Richard Henderson | |
275 | 14a6063a | Richard Henderson | static inline int64_t cpu_get_real_ticks(void) |
276 | 14a6063a | Richard Henderson | { |
277 | 14a6063a | Richard Henderson | uint64_t cc; |
278 | 14a6063a | Richard Henderson | uint32_t cur, ofs; |
279 | 14a6063a | Richard Henderson | |
280 | 14a6063a | Richard Henderson | asm volatile("rpcc %0" : "=r"(cc)); |
281 | 14a6063a | Richard Henderson | cur = cc; |
282 | 14a6063a | Richard Henderson | ofs = cc >> 32;
|
283 | 14a6063a | Richard Henderson | return cur - ofs;
|
284 | 14a6063a | Richard Henderson | } |
285 | 14a6063a | Richard Henderson | |
286 | 29e922b6 | Blue Swirl | #else
|
287 | 29e922b6 | Blue Swirl | /* The host CPU doesn't have an easily accessible cycle counter.
|
288 | 29e922b6 | Blue Swirl | Just return a monotonically increasing value. This will be
|
289 | 29e922b6 | Blue Swirl | totally wrong, but hopefully better than nothing. */
|
290 | 29e922b6 | Blue Swirl | static inline int64_t cpu_get_real_ticks (void) |
291 | 29e922b6 | Blue Swirl | { |
292 | 29e922b6 | Blue Swirl | static int64_t ticks = 0; |
293 | 29e922b6 | Blue Swirl | return ticks++;
|
294 | 29e922b6 | Blue Swirl | } |
295 | 29e922b6 | Blue Swirl | #endif
|
296 | 29e922b6 | Blue Swirl | |
297 | 2d8ebcf9 | Richard Henderson | #ifdef CONFIG_PROFILER
|
298 | 2d8ebcf9 | Richard Henderson | static inline int64_t profile_getclock(void) |
299 | 2d8ebcf9 | Richard Henderson | { |
300 | 2d8ebcf9 | Richard Henderson | return cpu_get_real_ticks();
|
301 | 2d8ebcf9 | Richard Henderson | } |
302 | 2d8ebcf9 | Richard Henderson | |
303 | 2d8ebcf9 | Richard Henderson | extern int64_t qemu_time, qemu_time_start;
|
304 | 2d8ebcf9 | Richard Henderson | extern int64_t tlb_flush_time;
|
305 | 2d8ebcf9 | Richard Henderson | extern int64_t dev_time;
|
306 | 2d8ebcf9 | Richard Henderson | #endif
|
307 | 2d8ebcf9 | Richard Henderson | |
308 | 87ecb68b | pbrook | #endif |