]>
Commit | Line | Data |
---|---|---|
87ecb68b PB |
1 | #ifndef QEMU_TIMER_H |
2 | #define QEMU_TIMER_H | |
3 | ||
ff83c66e | 4 | #include "qemu/typedefs.h" |
29e922b6 | 5 | #include "qemu-common.h" |
1de7afc9 | 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 | ||
ff83c66e AB |
14 | /** |
15 | * QEMUClockType: | |
16 | * | |
17 | * The following clock types are available: | |
18 | * | |
19 | * @QEMU_CLOCK_REALTIME: Real time clock | |
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. | |
25 | * | |
26 | * Formerly rt_clock | |
27 | * | |
28 | * @QEMU_CLOCK_VIRTUAL: virtual clock | |
29 | * | |
30 | * The virtual clock is only run during the emulation. It is stopped | |
31 | * when the virtual machine is stopped. Virtual timers use a high | |
32 | * precision clock, usually cpu cycles (use ticks_per_sec). | |
33 | * | |
34 | * Formerly vm_clock | |
35 | * | |
36 | * @QEMU_CLOCK_HOST: host clock | |
37 | * | |
38 | * The host clock should be use for device models that emulate accurate | |
39 | * real time sources. It will continue to run when the virtual machine | |
40 | * is suspended, and it will reflect system time changes the host may | |
41 | * undergo (e.g. due to NTP). The host clock has the same precision as | |
42 | * the virtual clock. | |
43 | * | |
44 | * Formerly host_clock | |
45 | */ | |
46 | ||
47 | typedef enum { | |
48 | QEMU_CLOCK_REALTIME = 0, | |
49 | QEMU_CLOCK_VIRTUAL = 1, | |
50 | QEMU_CLOCK_HOST = 2, | |
51 | QEMU_CLOCK_MAX | |
52 | } QEMUClockType; | |
58ac56b9 | 53 | |
87ecb68b | 54 | typedef struct QEMUClock QEMUClock; |
ff83c66e | 55 | typedef struct QEMUTimerList QEMUTimerList; |
754d6a54 AB |
56 | |
57 | struct QEMUTimerListGroup { | |
58 | QEMUTimerList *tl[QEMU_CLOCK_MAX]; | |
59 | }; | |
60 | ||
87ecb68b | 61 | typedef void QEMUTimerCB(void *opaque); |
d5541d86 | 62 | typedef void QEMUTimerListNotifyCB(void *opaque); |
87ecb68b | 63 | |
ff83c66e AB |
64 | struct QEMUTimer { |
65 | int64_t expire_time; /* in nanoseconds */ | |
66 | QEMUTimerList *timer_list; | |
67 | QEMUTimerCB *cb; | |
68 | void *opaque; | |
69 | QEMUTimer *next; | |
70 | int scale; | |
71 | }; | |
72 | ||
754d6a54 | 73 | extern QEMUTimerListGroup main_loop_tlg; |
ff83c66e | 74 | extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX]; |
87ecb68b | 75 | |
ff83c66e AB |
76 | /** |
77 | * qemu_clock_ptr: | |
78 | * @type: type of clock | |
79 | * | |
80 | * Translate a clock type into a pointer to QEMUClock object. | |
81 | * | |
82 | * Returns: a pointer to the QEMUClock object | |
83 | */ | |
84 | static inline QEMUClock *qemu_clock_ptr(QEMUClockType type) | |
85 | { | |
86 | return qemu_clocks[type]; | |
87 | } | |
87ecb68b | 88 | |
ff83c66e AB |
89 | /* These three clocks are maintained here with separate variable |
90 | * names for compatibility only. | |
91 | */ | |
92 | #define rt_clock (qemu_clock_ptr(QEMU_CLOCK_REALTIME)) | |
93 | #define vm_clock (qemu_clock_ptr(QEMU_CLOCK_VIRTUAL)) | |
94 | #define host_clock (qemu_clock_ptr(QEMU_CLOCK_HOST)) | |
21d5d12b | 95 | |
41c872b6 | 96 | int64_t qemu_get_clock_ns(QEMUClock *clock); |
ff83c66e AB |
97 | bool qemu_clock_has_timers(QEMUClock *clock); |
98 | bool qemu_clock_expired(QEMUClock *clock); | |
dc2dfcf0 | 99 | int64_t qemu_clock_deadline(QEMUClock *clock); |
02a03a9f AB |
100 | |
101 | /** | |
102 | * qemu_clock_deadline_ns: | |
103 | * @clock: the clock to operate on | |
104 | * | |
105 | * Calculate the timeout of the earliest expiring timer | |
106 | * in nanoseconds, or -1 if no timer is set to expire. | |
107 | * | |
108 | * Returns: time until expiry in nanoseconds or -1 | |
109 | */ | |
110 | int64_t qemu_clock_deadline_ns(QEMUClock *clock); | |
111 | ||
ff83c66e AB |
112 | /** |
113 | * qemu_clock_use_for_deadline: | |
114 | * @clock: the clock to operate on | |
115 | * | |
116 | * Determine whether a clock should be used for deadline | |
117 | * calculations. Some clocks, for instance vm_clock with | |
118 | * use_icount set, do not count in nanoseconds. Such clocks | |
119 | * are not used for deadline calculations, and are presumed | |
120 | * to interrupt any poll using qemu_notify/aio_notify | |
121 | * etc. | |
122 | * | |
123 | * Returns: true if the clock runs in nanoseconds and | |
124 | * should be used for a deadline. | |
125 | */ | |
126 | bool qemu_clock_use_for_deadline(QEMUClock *clock); | |
127 | ||
128 | /** | |
129 | * qemu_clock_get_main_loop_timerlist: | |
130 | * @clock: the clock to operate on | |
131 | * | |
132 | * Return the default timer list assocatiated with a clock. | |
133 | * | |
134 | * Returns: the default timer list | |
135 | */ | |
136 | QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock); | |
137 | ||
b1bbfe72 AB |
138 | /** |
139 | * qemu_clock_nofify: | |
140 | * @clock: the clock to operate on | |
141 | * | |
142 | * Call the notifier callback connected with the default timer | |
143 | * list linked to the clock, or qemu_notify() if none. | |
144 | */ | |
145 | void qemu_clock_notify(QEMUClock *clock); | |
146 | ||
ff83c66e AB |
147 | /** |
148 | * timerlist_new: | |
149 | * @type: the clock type to associate with the timerlist | |
d5541d86 AB |
150 | * @cb: the callback to call on notification |
151 | * @opaque: the opaque pointer to pass to the callback | |
ff83c66e AB |
152 | * |
153 | * Create a new timerlist associated with the clock of | |
154 | * type @type. | |
155 | * | |
156 | * Returns: a pointer to the QEMUTimerList created | |
157 | */ | |
d5541d86 AB |
158 | QEMUTimerList *timerlist_new(QEMUClockType type, |
159 | QEMUTimerListNotifyCB *cb, void *opaque); | |
ff83c66e AB |
160 | |
161 | /** | |
162 | * timerlist_free: | |
163 | * @timer_list: the timer list to free | |
164 | * | |
165 | * Frees a timer_list. It must have no active timers. | |
166 | */ | |
167 | void timerlist_free(QEMUTimerList *timer_list); | |
168 | ||
169 | /** | |
170 | * timerlist_has_timers: | |
171 | * @timer_list: the timer list to operate on | |
172 | * | |
173 | * Determine whether a timer list has active timers | |
174 | * | |
175 | * Returns: true if the timer list has timers. | |
176 | */ | |
177 | bool timerlist_has_timers(QEMUTimerList *timer_list); | |
178 | ||
179 | /** | |
180 | * timerlist_expired: | |
181 | * @timer_list: the timer list to operate on | |
182 | * | |
183 | * Determine whether a timer list has any timers which | |
184 | * are expired. | |
185 | * | |
186 | * Returns: true if the timer list has timers which | |
187 | * have expired. | |
188 | */ | |
189 | bool timerlist_expired(QEMUTimerList *timer_list); | |
190 | ||
191 | /** | |
192 | * timerlist_deadline: | |
193 | * @timer_list: the timer list to operate on | |
194 | * | |
195 | * Determine the deadline for a timer_list. This is | |
196 | * a legacy function which returns INT32_MAX if the | |
197 | * timer list has no timers or if the earliest timer | |
198 | * expires later than INT32_MAX nanoseconds away. | |
199 | * | |
200 | * Returns: the number of nanoseconds until the earliest | |
201 | * timer expires or INT32_MAX in the situations listed | |
202 | * above | |
203 | */ | |
204 | int64_t timerlist_deadline(QEMUTimerList *timer_list); | |
205 | ||
206 | /** | |
207 | * timerlist_deadline_ns: | |
208 | * @timer_list: the timer list to operate on | |
209 | * | |
210 | * Determine the deadline for a timer_list, i.e. | |
211 | * the number of nanoseconds until the first timer | |
212 | * expires. Return -1 if there are no timers. | |
213 | * | |
214 | * Returns: the number of nanoseconds until the earliest | |
215 | * timer expires -1 if none | |
216 | */ | |
217 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list); | |
218 | ||
219 | /** | |
220 | * timerlist_getclock: | |
221 | * @timer_list: the timer list to operate on | |
222 | * | |
223 | * Determine the clock associated with a timer list. | |
224 | * | |
225 | * Returns: the clock associated with the timer list. | |
226 | */ | |
227 | QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list); | |
228 | ||
229 | /** | |
230 | * timerlist_run_timers: | |
231 | * @timer_list: the timer list to use | |
232 | * | |
233 | * Call all expired timers associated with the timer list. | |
234 | * | |
235 | * Returns: true if any timer expired | |
236 | */ | |
237 | bool timerlist_run_timers(QEMUTimerList *timer_list); | |
238 | ||
d5541d86 AB |
239 | /** |
240 | * timerlist_notify: | |
241 | * @timer_list: the timer list to use | |
242 | * | |
243 | * call the notifier callback associated with the timer list. | |
244 | */ | |
245 | void timerlist_notify(QEMUTimerList *timer_list); | |
246 | ||
754d6a54 AB |
247 | /** |
248 | * timerlistgroup_init: | |
249 | * @tlg: the timer list group | |
d5541d86 AB |
250 | * @cb: the callback to call when a notify is required |
251 | * @opaque: the opaque pointer to be passed to the callback. | |
754d6a54 AB |
252 | * |
253 | * Initialise a timer list group. This must already be | |
d5541d86 AB |
254 | * allocated in memory and zeroed. The notifier callback is |
255 | * called whenever a clock in the timer list group is | |
256 | * reenabled or whenever a timer associated with any timer | |
257 | * list is modified. If @cb is specified as null, qemu_notify() | |
258 | * is used instead. | |
754d6a54 | 259 | */ |
d5541d86 AB |
260 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
261 | QEMUTimerListNotifyCB *cb, void *opaque); | |
754d6a54 AB |
262 | |
263 | /** | |
264 | * timerlistgroup_deinit: | |
265 | * @tlg: the timer list group | |
266 | * | |
267 | * Deinitialise a timer list group. This must already be | |
268 | * initialised. Note the memory is not freed. | |
269 | */ | |
270 | void timerlistgroup_deinit(QEMUTimerListGroup *tlg); | |
271 | ||
272 | /** | |
273 | * timerlistgroup_run_timers: | |
274 | * @tlg: the timer list group | |
275 | * | |
276 | * Run the timers associated with a timer list group. | |
277 | * This will run timers on multiple clocks. | |
278 | * | |
279 | * Returns: true if any timer callback ran | |
280 | */ | |
281 | bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg); | |
282 | ||
283 | /** | |
284 | * timerlistgroup_deadline_ns | |
285 | * @tlg: the timer list group | |
286 | * | |
287 | * Determine the deadline of the soonest timer to | |
288 | * expire associated with any timer list linked to | |
289 | * the timer list group. Only clocks suitable for | |
290 | * deadline calculation are included. | |
291 | * | |
292 | * Returns: the deadline in nanoseconds or -1 if no | |
293 | * timers are to expire. | |
294 | */ | |
295 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg); | |
296 | ||
02a03a9f AB |
297 | /** |
298 | * qemu_timeout_ns_to_ms: | |
299 | * @ns: nanosecond timeout value | |
300 | * | |
301 | * Convert a nanosecond timeout value (or -1) to | |
302 | * a millisecond value (or -1), always rounding up. | |
303 | * | |
304 | * Returns: millisecond timeout value | |
305 | */ | |
306 | int qemu_timeout_ns_to_ms(int64_t ns); | |
307 | ||
4e0c6529 AB |
308 | /** |
309 | * qemu_poll_ns: | |
310 | * @fds: Array of file descriptors | |
311 | * @nfds: number of file descriptors | |
312 | * @timeout: timeout in nanoseconds | |
313 | * | |
314 | * Perform a poll like g_poll but with a timeout in nanoseconds. | |
315 | * See g_poll documentation for further details. | |
316 | * | |
317 | * Returns: number of fds ready | |
318 | */ | |
319 | int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout); | |
5e1ec7b2 | 320 | void qemu_clock_enable(QEMUClock *clock, bool enabled); |
ab33fcda | 321 | void qemu_clock_warp(QEMUClock *clock); |
87ecb68b | 322 | |
691a0c9c JK |
323 | void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier); |
324 | void qemu_unregister_clock_reset_notifier(QEMUClock *clock, | |
325 | Notifier *notifier); | |
326 | ||
4a998740 PB |
327 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, |
328 | QEMUTimerCB *cb, void *opaque); | |
ff83c66e AB |
329 | |
330 | /** | |
331 | * timer_init: | |
332 | * @ts: the timer to be initialised | |
333 | * @timer_list: the timer list to attach the timer to | |
334 | * @scale: the scale value for the tiemr | |
335 | * @cb: the callback to be called when the timer expires | |
336 | * @opaque: the opaque pointer to be passed to the callback | |
337 | * | |
338 | * Initialise a new timer and associate it with @timer_list. | |
339 | * The caller is responsible for allocating the memory. | |
340 | * | |
341 | * You need not call an explicit deinit call. Simply make | |
342 | * sure it is not on a list with timer_del. | |
343 | */ | |
344 | void timer_init(QEMUTimer *ts, | |
345 | QEMUTimerList *timer_list, int scale, | |
346 | QEMUTimerCB *cb, void *opaque); | |
347 | ||
348 | /** | |
349 | * timer_new_tl: | |
350 | * @timer_list: the timer list to attach the timer to | |
351 | * @scale: the scale value for the tiemr | |
352 | * @cb: the callback to be called when the timer expires | |
353 | * @opaque: the opaque pointer to be passed to the callback | |
354 | * | |
355 | * Creeate a new timer and associate it with @timer_list. | |
356 | * The memory is allocated by the function. | |
357 | * | |
358 | * This is not the preferred interface unless you know you | |
359 | * are going to call timer_free. Use timer_init instead. | |
360 | * | |
361 | * Returns: a pointer to the timer | |
362 | */ | |
363 | static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list, | |
364 | int scale, | |
365 | QEMUTimerCB *cb, | |
366 | void *opaque) | |
367 | { | |
368 | QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer)); | |
369 | timer_init(ts, timer_list, scale, cb, opaque); | |
370 | return ts; | |
371 | } | |
372 | ||
87ecb68b PB |
373 | void qemu_free_timer(QEMUTimer *ts); |
374 | void qemu_del_timer(QEMUTimer *ts); | |
2ff68d07 | 375 | void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time); |
87ecb68b | 376 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time); |
e93379b0 AB |
377 | bool timer_pending(QEMUTimer *ts); |
378 | bool timer_expired(QEMUTimer *timer_head, int64_t current_time); | |
379 | uint64_t timer_expire_time_ns(QEMUTimer *ts); | |
87ecb68b | 380 | |
ff83c66e AB |
381 | /* New format calling conventions for timers */ |
382 | ||
383 | /** | |
384 | * timer_free: | |
385 | * @ts: the timer | |
386 | * | |
387 | * Free a timer (it must not be on the active list) | |
388 | */ | |
389 | static inline void timer_free(QEMUTimer *ts) | |
390 | { | |
391 | qemu_free_timer(ts); | |
392 | } | |
393 | ||
394 | /** | |
395 | * timer_del: | |
396 | * @ts: the timer | |
397 | * | |
398 | * Delete a timer from the active list. | |
399 | */ | |
400 | static inline void timer_del(QEMUTimer *ts) | |
401 | { | |
402 | qemu_del_timer(ts); | |
403 | } | |
404 | ||
405 | /** | |
406 | * timer_mod_ns: | |
407 | * @ts: the timer | |
408 | * @expire_time: the expiry time in nanoseconds | |
409 | * | |
410 | * Modify a timer to expire at @expire_time | |
411 | */ | |
412 | static inline void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) | |
413 | { | |
414 | qemu_mod_timer_ns(ts, expire_time); | |
415 | } | |
416 | ||
417 | /** | |
418 | * timer_mod: | |
419 | * @ts: the timer | |
420 | * @expire_time: the expire time in the units associated with the timer | |
421 | * | |
422 | * Modify a timer to expiry at @expire_time, taking into | |
423 | * account the scale associated with the timer. | |
424 | */ | |
425 | static inline void timer_mod(QEMUTimer *ts, int64_t expire_timer) | |
426 | { | |
427 | qemu_mod_timer(ts, expire_timer); | |
428 | } | |
429 | ||
f9a976b7 AB |
430 | /** |
431 | * qemu_run_timers: | |
432 | * @clock: clock on which to operate | |
433 | * | |
ff83c66e AB |
434 | * Run all the timers associated with the default timer list |
435 | * of a clock. | |
f9a976b7 AB |
436 | * |
437 | * Returns: true if any timer ran. | |
438 | */ | |
439 | bool qemu_run_timers(QEMUClock *clock); | |
440 | ||
441 | /** | |
442 | * qemu_run_all_timers: | |
443 | * | |
ff83c66e AB |
444 | * Run all the timers associated with the default timer list |
445 | * of every clock. | |
f9a976b7 AB |
446 | * |
447 | * Returns: true if any timer ran. | |
448 | */ | |
449 | bool qemu_run_all_timers(void); | |
450 | ||
db1a4972 | 451 | void configure_alarms(char const *opt); |
db1a4972 PB |
452 | void init_clocks(void); |
453 | int init_timer_alarm(void); | |
db1a4972 | 454 | |
70c3b557 BS |
455 | int64_t cpu_get_ticks(void); |
456 | void cpu_enable_ticks(void); | |
457 | void cpu_disable_ticks(void); | |
458 | ||
02a03a9f AB |
459 | /** |
460 | * qemu_soonest_timeout: | |
461 | * @timeout1: first timeout in nanoseconds (or -1 for infinite) | |
462 | * @timeout2: second timeout in nanoseconds (or -1 for infinite) | |
463 | * | |
464 | * Calculates the soonest of two timeout values. -1 means infinite, which | |
465 | * is later than any other value. | |
466 | * | |
467 | * Returns: soonest timeout value in nanoseconds (or -1 for infinite) | |
468 | */ | |
469 | static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2) | |
470 | { | |
471 | /* we can abuse the fact that -1 (which means infinite) is a maximal | |
472 | * value when cast to unsigned. As this is disgusting, it's kept in | |
473 | * one inline function. | |
474 | */ | |
475 | return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2; | |
476 | } | |
477 | ||
ff83c66e AB |
478 | /** |
479 | * qemu_new_timer_ns: | |
480 | * @clock: the clock to associate with the timer | |
481 | * @callback: the callback to call when the timer expires | |
482 | * @opaque: the opaque pointer to pass to the callback | |
483 | * | |
484 | * Create a new timer with nanosecond scale on the default timer list | |
485 | * associated with the clock. | |
486 | * | |
487 | * Returns: a pointer to the newly created timer | |
488 | */ | |
0ce1b948 PB |
489 | static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb, |
490 | void *opaque) | |
491 | { | |
4a998740 | 492 | return qemu_new_timer(clock, SCALE_NS, cb, opaque); |
0ce1b948 PB |
493 | } |
494 | ||
ff83c66e AB |
495 | /** |
496 | * qemu_new_timer_us: | |
497 | * @clock: the clock to associate with the timer | |
498 | * @callback: the callback to call when the timer expires | |
499 | * @opaque: the opaque pointer to pass to the callback | |
500 | * | |
501 | * Create a new timer with microsecond scale on the default timer list | |
502 | * associated with the clock. | |
503 | * | |
504 | * Returns: a pointer to the newly created timer | |
505 | */ | |
506 | static inline QEMUTimer *qemu_new_timer_us(QEMUClock *clock, | |
507 | QEMUTimerCB *cb, | |
508 | void *opaque) | |
509 | { | |
510 | return qemu_new_timer(clock, SCALE_US, cb, opaque); | |
511 | } | |
512 | ||
513 | /** | |
514 | * qemu_new_timer_ms: | |
515 | * @clock: the clock to associate with the timer | |
516 | * @callback: the callback to call when the timer expires | |
517 | * @opaque: the opaque pointer to pass to the callback | |
518 | * | |
519 | * Create a new timer with millisecond scale on the default timer list | |
520 | * associated with the clock. | |
521 | * | |
522 | * Returns: a pointer to the newly created timer | |
523 | */ | |
524 | static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock, | |
525 | QEMUTimerCB *cb, | |
0ce1b948 PB |
526 | void *opaque) |
527 | { | |
4a998740 | 528 | return qemu_new_timer(clock, SCALE_MS, cb, opaque); |
0ce1b948 PB |
529 | } |
530 | ||
531 | static inline int64_t qemu_get_clock_ms(QEMUClock *clock) | |
532 | { | |
533 | return qemu_get_clock_ns(clock) / SCALE_MS; | |
534 | } | |
535 | ||
274dfed8 AL |
536 | static inline int64_t get_ticks_per_sec(void) |
537 | { | |
538 | return 1000000000LL; | |
539 | } | |
87ecb68b | 540 | |
c57c846a BS |
541 | /* real time host monotonic timer */ |
542 | static inline int64_t get_clock_realtime(void) | |
543 | { | |
544 | struct timeval tv; | |
545 | ||
546 | gettimeofday(&tv, NULL); | |
547 | return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); | |
548 | } | |
549 | ||
550 | /* Warning: don't insert tracepoints into these functions, they are | |
551 | also used by simpletrace backend and tracepoints would cause | |
552 | an infinite recursion! */ | |
553 | #ifdef _WIN32 | |
554 | extern int64_t clock_freq; | |
555 | ||
556 | static inline int64_t get_clock(void) | |
557 | { | |
558 | LARGE_INTEGER ti; | |
559 | QueryPerformanceCounter(&ti); | |
560 | return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq); | |
561 | } | |
562 | ||
563 | #else | |
564 | ||
565 | extern int use_rt_clock; | |
566 | ||
567 | static inline int64_t get_clock(void) | |
568 | { | |
d05ef160 | 569 | #ifdef CLOCK_MONOTONIC |
c57c846a BS |
570 | if (use_rt_clock) { |
571 | struct timespec ts; | |
572 | clock_gettime(CLOCK_MONOTONIC, &ts); | |
573 | return ts.tv_sec * 1000000000LL + ts.tv_nsec; | |
574 | } else | |
575 | #endif | |
576 | { | |
577 | /* XXX: using gettimeofday leads to problems if the date | |
578 | changes, so it should be avoided. */ | |
579 | return get_clock_realtime(); | |
580 | } | |
581 | } | |
582 | #endif | |
db1a4972 | 583 | |
87ecb68b PB |
584 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts); |
585 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts); | |
586 | ||
29e922b6 | 587 | /* icount */ |
29e922b6 | 588 | int64_t cpu_get_icount(void); |
946fb27c | 589 | int64_t cpu_get_clock(void); |
29e922b6 BS |
590 | |
591 | /*******************************************/ | |
592 | /* host CPU ticks (if available) */ | |
593 | ||
594 | #if defined(_ARCH_PPC) | |
595 | ||
596 | static inline int64_t cpu_get_real_ticks(void) | |
597 | { | |
598 | int64_t retval; | |
599 | #ifdef _ARCH_PPC64 | |
600 | /* This reads timebase in one 64bit go and includes Cell workaround from: | |
601 | http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html | |
602 | */ | |
603 | __asm__ __volatile__ ("mftb %0\n\t" | |
604 | "cmpwi %0,0\n\t" | |
605 | "beq- $-8" | |
606 | : "=r" (retval)); | |
607 | #else | |
608 | /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */ | |
609 | unsigned long junk; | |
4a9590f3 AG |
610 | __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */ |
611 | "mfspr %L0,268\n\t" /* mftb */ | |
612 | "mfspr %0,269\n\t" /* mftbu */ | |
29e922b6 BS |
613 | "cmpw %0,%1\n\t" |
614 | "bne $-16" | |
615 | : "=r" (retval), "=r" (junk)); | |
616 | #endif | |
617 | return retval; | |
618 | } | |
619 | ||
620 | #elif defined(__i386__) | |
621 | ||
622 | static inline int64_t cpu_get_real_ticks(void) | |
623 | { | |
624 | int64_t val; | |
625 | asm volatile ("rdtsc" : "=A" (val)); | |
626 | return val; | |
627 | } | |
628 | ||
629 | #elif defined(__x86_64__) | |
630 | ||
631 | static inline int64_t cpu_get_real_ticks(void) | |
632 | { | |
633 | uint32_t low,high; | |
634 | int64_t val; | |
635 | asm volatile("rdtsc" : "=a" (low), "=d" (high)); | |
636 | val = high; | |
637 | val <<= 32; | |
638 | val |= low; | |
639 | return val; | |
640 | } | |
641 | ||
642 | #elif defined(__hppa__) | |
643 | ||
644 | static inline int64_t cpu_get_real_ticks(void) | |
645 | { | |
646 | int val; | |
647 | asm volatile ("mfctl %%cr16, %0" : "=r"(val)); | |
648 | return val; | |
649 | } | |
650 | ||
651 | #elif defined(__ia64) | |
652 | ||
653 | static inline int64_t cpu_get_real_ticks(void) | |
654 | { | |
655 | int64_t val; | |
656 | asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory"); | |
657 | return val; | |
658 | } | |
659 | ||
660 | #elif defined(__s390__) | |
661 | ||
662 | static inline int64_t cpu_get_real_ticks(void) | |
663 | { | |
664 | int64_t val; | |
665 | asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc"); | |
666 | return val; | |
667 | } | |
668 | ||
9b9c37c3 | 669 | #elif defined(__sparc__) |
29e922b6 BS |
670 | |
671 | static inline int64_t cpu_get_real_ticks (void) | |
672 | { | |
673 | #if defined(_LP64) | |
674 | uint64_t rval; | |
675 | asm volatile("rd %%tick,%0" : "=r"(rval)); | |
676 | return rval; | |
677 | #else | |
9b9c37c3 RH |
678 | /* We need an %o or %g register for this. For recent enough gcc |
679 | there is an "h" constraint for that. Don't bother with that. */ | |
29e922b6 BS |
680 | union { |
681 | uint64_t i64; | |
682 | struct { | |
683 | uint32_t high; | |
684 | uint32_t low; | |
685 | } i32; | |
686 | } rval; | |
9b9c37c3 RH |
687 | asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1" |
688 | : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1"); | |
29e922b6 BS |
689 | return rval.i64; |
690 | #endif | |
691 | } | |
692 | ||
693 | #elif defined(__mips__) && \ | |
694 | ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__)) | |
695 | /* | |
696 | * binutils wants to use rdhwr only on mips32r2 | |
697 | * but as linux kernel emulate it, it's fine | |
698 | * to use it. | |
699 | * | |
700 | */ | |
701 | #define MIPS_RDHWR(rd, value) { \ | |
702 | __asm__ __volatile__ (".set push\n\t" \ | |
703 | ".set mips32r2\n\t" \ | |
704 | "rdhwr %0, "rd"\n\t" \ | |
705 | ".set pop" \ | |
706 | : "=r" (value)); \ | |
707 | } | |
708 | ||
709 | static inline int64_t cpu_get_real_ticks(void) | |
710 | { | |
711 | /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */ | |
712 | uint32_t count; | |
713 | static uint32_t cyc_per_count = 0; | |
714 | ||
715 | if (!cyc_per_count) { | |
716 | MIPS_RDHWR("$3", cyc_per_count); | |
717 | } | |
718 | ||
719 | MIPS_RDHWR("$2", count); | |
720 | return (int64_t)(count * cyc_per_count); | |
721 | } | |
722 | ||
14a6063a RH |
723 | #elif defined(__alpha__) |
724 | ||
725 | static inline int64_t cpu_get_real_ticks(void) | |
726 | { | |
727 | uint64_t cc; | |
728 | uint32_t cur, ofs; | |
729 | ||
730 | asm volatile("rpcc %0" : "=r"(cc)); | |
731 | cur = cc; | |
732 | ofs = cc >> 32; | |
733 | return cur - ofs; | |
734 | } | |
735 | ||
29e922b6 BS |
736 | #else |
737 | /* The host CPU doesn't have an easily accessible cycle counter. | |
738 | Just return a monotonically increasing value. This will be | |
739 | totally wrong, but hopefully better than nothing. */ | |
740 | static inline int64_t cpu_get_real_ticks (void) | |
741 | { | |
742 | static int64_t ticks = 0; | |
743 | return ticks++; | |
744 | } | |
745 | #endif | |
746 | ||
2d8ebcf9 RH |
747 | #ifdef CONFIG_PROFILER |
748 | static inline int64_t profile_getclock(void) | |
749 | { | |
750 | return cpu_get_real_ticks(); | |
751 | } | |
752 | ||
753 | extern int64_t qemu_time, qemu_time_start; | |
754 | extern int64_t tlb_flush_time; | |
755 | extern int64_t dev_time; | |
756 | #endif | |
757 | ||
87ecb68b | 758 | #endif |