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