qmp-input: Don't consume input when checking has_member
[qemu.git] / include / qemu / timer.h
blob471969a24d3b5bc55364b6ac862a253e14c293c5
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
4 #include "qemu-common.h"
5 #include "qemu/notify.h"
6 #include "qemu/host-utils.h"
8 #define NANOSECONDS_PER_SECOND 1000000000LL
10 /* timers */
12 #define SCALE_MS 1000000
13 #define SCALE_US 1000
14 #define SCALE_NS 1
16 /**
17 * QEMUClockType:
19 * The following clock types are available:
21 * @QEMU_CLOCK_REALTIME: Real time clock
23 * The real time clock should be used only for stuff which does not
24 * change the virtual machine state, as it is run even if the virtual
25 * machine is stopped. The real time clock has a frequency of 1000
26 * Hz.
28 * @QEMU_CLOCK_VIRTUAL: virtual clock
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).
34 * @QEMU_CLOCK_HOST: host clock
36 * The host clock should be use for device models that emulate accurate
37 * real time sources. It will continue to run when the virtual machine
38 * is suspended, and it will reflect system time changes the host may
39 * undergo (e.g. due to NTP). The host clock has the same precision as
40 * the virtual clock.
42 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
44 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
45 * In icount mode, this clock counts nanoseconds while the virtual
46 * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
47 * while the CPUs are sleeping and thus not executing instructions.
50 typedef enum {
51 QEMU_CLOCK_REALTIME = 0,
52 QEMU_CLOCK_VIRTUAL = 1,
53 QEMU_CLOCK_HOST = 2,
54 QEMU_CLOCK_VIRTUAL_RT = 3,
55 QEMU_CLOCK_MAX
56 } QEMUClockType;
58 typedef struct QEMUTimerList QEMUTimerList;
60 struct QEMUTimerListGroup {
61 QEMUTimerList *tl[QEMU_CLOCK_MAX];
64 typedef void QEMUTimerCB(void *opaque);
65 typedef void QEMUTimerListNotifyCB(void *opaque);
67 struct QEMUTimer {
68 int64_t expire_time; /* in nanoseconds */
69 QEMUTimerList *timer_list;
70 QEMUTimerCB *cb;
71 void *opaque;
72 QEMUTimer *next;
73 int scale;
76 extern QEMUTimerListGroup main_loop_tlg;
79 * QEMUClockType
83 * qemu_clock_get_ns;
84 * @type: the clock type
86 * Get the nanosecond value of a clock with
87 * type @type
89 * Returns: the clock value in nanoseconds
91 int64_t qemu_clock_get_ns(QEMUClockType type);
93 /**
94 * qemu_clock_get_ms;
95 * @type: the clock type
97 * Get the millisecond value of a clock with
98 * type @type
100 * Returns: the clock value in milliseconds
102 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
104 return qemu_clock_get_ns(type) / SCALE_MS;
108 * qemu_clock_get_us;
109 * @type: the clock type
111 * Get the microsecond value of a clock with
112 * type @type
114 * Returns: the clock value in microseconds
116 static inline int64_t qemu_clock_get_us(QEMUClockType type)
118 return qemu_clock_get_ns(type) / SCALE_US;
122 * qemu_clock_has_timers:
123 * @type: the clock type
125 * Determines whether a clock's default timer list
126 * has timers attached
128 * Note that this function should not be used when other threads also access
129 * the timer list. The return value may be outdated by the time it is acted
130 * upon.
132 * Returns: true if the clock's default timer list
133 * has timers attached
135 bool qemu_clock_has_timers(QEMUClockType type);
138 * qemu_clock_expired:
139 * @type: the clock type
141 * Determines whether a clock's default timer list
142 * has an expired clock.
144 * Returns: true if the clock's default timer list has
145 * an expired timer
147 bool qemu_clock_expired(QEMUClockType type);
150 * qemu_clock_use_for_deadline:
151 * @type: the clock type
153 * Determine whether a clock should be used for deadline
154 * calculations. Some clocks, for instance vm_clock with
155 * use_icount set, do not count in nanoseconds. Such clocks
156 * are not used for deadline calculations, and are presumed
157 * to interrupt any poll using qemu_notify/aio_notify
158 * etc.
160 * Returns: true if the clock runs in nanoseconds and
161 * should be used for a deadline.
163 bool qemu_clock_use_for_deadline(QEMUClockType type);
166 * qemu_clock_deadline_ns_all:
167 * @type: the clock type
169 * Calculate the deadline across all timer lists associated
170 * with a clock (as opposed to just the default one)
171 * in nanoseconds, or -1 if no timer is set to expire.
173 * Returns: time until expiry in nanoseconds or -1
175 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
178 * qemu_clock_get_main_loop_timerlist:
179 * @type: the clock type
181 * Return the default timer list assocatiated with a clock.
183 * Returns: the default timer list
185 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
188 * qemu_clock_nofify:
189 * @type: the clock type
191 * Call the notifier callback connected with the default timer
192 * list linked to the clock, or qemu_notify() if none.
194 void qemu_clock_notify(QEMUClockType type);
197 * qemu_clock_enable:
198 * @type: the clock type
199 * @enabled: true to enable, false to disable
201 * Enable or disable a clock
202 * Disabling the clock will wait for related timerlists to stop
203 * executing qemu_run_timers. Thus, this functions should not
204 * be used from the callback of a timer that is based on @clock.
205 * Doing so would cause a deadlock.
207 * Caller should hold BQL.
209 void qemu_clock_enable(QEMUClockType type, bool enabled);
212 * qemu_start_warp_timer:
214 * Starts a timer for virtual clock update
216 void qemu_start_warp_timer(void);
219 * qemu_clock_register_reset_notifier:
220 * @type: the clock type
221 * @notifier: the notifier function
223 * Register a notifier function to call when the clock
224 * concerned is reset.
226 void qemu_clock_register_reset_notifier(QEMUClockType type,
227 Notifier *notifier);
230 * qemu_clock_unregister_reset_notifier:
231 * @type: the clock type
232 * @notifier: the notifier function
234 * Unregister a notifier function to call when the clock
235 * concerned is reset.
237 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
238 Notifier *notifier);
241 * qemu_clock_run_timers:
242 * @type: clock on which to operate
244 * Run all the timers associated with the default timer list
245 * of a clock.
247 * Returns: true if any timer ran.
249 bool qemu_clock_run_timers(QEMUClockType type);
252 * qemu_clock_run_all_timers:
254 * Run all the timers associated with the default timer list
255 * of every clock.
257 * Returns: true if any timer ran.
259 bool qemu_clock_run_all_timers(void);
262 * QEMUTimerList
266 * timerlist_new:
267 * @type: the clock type to associate with the timerlist
268 * @cb: the callback to call on notification
269 * @opaque: the opaque pointer to pass to the callback
271 * Create a new timerlist associated with the clock of
272 * type @type.
274 * Returns: a pointer to the QEMUTimerList created
276 QEMUTimerList *timerlist_new(QEMUClockType type,
277 QEMUTimerListNotifyCB *cb, void *opaque);
280 * timerlist_free:
281 * @timer_list: the timer list to free
283 * Frees a timer_list. It must have no active timers.
285 void timerlist_free(QEMUTimerList *timer_list);
288 * timerlist_has_timers:
289 * @timer_list: the timer list to operate on
291 * Determine whether a timer list has active timers
293 * Note that this function should not be used when other threads also access
294 * the timer list. The return value may be outdated by the time it is acted
295 * upon.
297 * Returns: true if the timer list has timers.
299 bool timerlist_has_timers(QEMUTimerList *timer_list);
302 * timerlist_expired:
303 * @timer_list: the timer list to operate on
305 * Determine whether a timer list has any timers which
306 * are expired.
308 * Returns: true if the timer list has timers which
309 * have expired.
311 bool timerlist_expired(QEMUTimerList *timer_list);
314 * timerlist_deadline_ns:
315 * @timer_list: the timer list to operate on
317 * Determine the deadline for a timer_list, i.e.
318 * the number of nanoseconds until the first timer
319 * expires. Return -1 if there are no timers.
321 * Returns: the number of nanoseconds until the earliest
322 * timer expires -1 if none
324 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
327 * timerlist_get_clock:
328 * @timer_list: the timer list to operate on
330 * Determine the clock type associated with a timer list.
332 * Returns: the clock type associated with the
333 * timer list.
335 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
338 * timerlist_run_timers:
339 * @timer_list: the timer list to use
341 * Call all expired timers associated with the timer list.
343 * Returns: true if any timer expired
345 bool timerlist_run_timers(QEMUTimerList *timer_list);
348 * timerlist_notify:
349 * @timer_list: the timer list to use
351 * call the notifier callback associated with the timer list.
353 void timerlist_notify(QEMUTimerList *timer_list);
356 * QEMUTimerListGroup
360 * timerlistgroup_init:
361 * @tlg: the timer list group
362 * @cb: the callback to call when a notify is required
363 * @opaque: the opaque pointer to be passed to the callback.
365 * Initialise a timer list group. This must already be
366 * allocated in memory and zeroed. The notifier callback is
367 * called whenever a clock in the timer list group is
368 * reenabled or whenever a timer associated with any timer
369 * list is modified. If @cb is specified as null, qemu_notify()
370 * is used instead.
372 void timerlistgroup_init(QEMUTimerListGroup *tlg,
373 QEMUTimerListNotifyCB *cb, void *opaque);
376 * timerlistgroup_deinit:
377 * @tlg: the timer list group
379 * Deinitialise a timer list group. This must already be
380 * initialised. Note the memory is not freed.
382 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
385 * timerlistgroup_run_timers:
386 * @tlg: the timer list group
388 * Run the timers associated with a timer list group.
389 * This will run timers on multiple clocks.
391 * Returns: true if any timer callback ran
393 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
396 * timerlistgroup_deadline_ns:
397 * @tlg: the timer list group
399 * Determine the deadline of the soonest timer to
400 * expire associated with any timer list linked to
401 * the timer list group. Only clocks suitable for
402 * deadline calculation are included.
404 * Returns: the deadline in nanoseconds or -1 if no
405 * timers are to expire.
407 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
410 * QEMUTimer
414 * timer_init_tl:
415 * @ts: the timer to be initialised
416 * @timer_list: the timer list to attach the timer to
417 * @scale: the scale value for the timer
418 * @cb: the callback to be called when the timer expires
419 * @opaque: the opaque pointer to be passed to the callback
421 * Initialise a new timer and associate it with @timer_list.
422 * The caller is responsible for allocating the memory.
424 * You need not call an explicit deinit call. Simply make
425 * sure it is not on a list with timer_del.
427 void timer_init_tl(QEMUTimer *ts,
428 QEMUTimerList *timer_list, int scale,
429 QEMUTimerCB *cb, void *opaque);
432 * timer_init:
433 * @type: the clock to associate with the timer
434 * @scale: the scale value for the timer
435 * @cb: the callback to call when the timer expires
436 * @opaque: the opaque pointer to pass to the callback
438 * Initialize a timer with the given scale on the default timer list
439 * associated with the clock.
441 * You need not call an explicit deinit call. Simply make
442 * sure it is not on a list with timer_del.
444 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
445 QEMUTimerCB *cb, void *opaque)
447 timer_init_tl(ts, main_loop_tlg.tl[type], scale, cb, opaque);
451 * timer_init_ns:
452 * @type: the clock to associate with the timer
453 * @cb: the callback to call when the timer expires
454 * @opaque: the opaque pointer to pass to the callback
456 * Initialize a timer with nanosecond scale on the default timer list
457 * associated with the clock.
459 * You need not call an explicit deinit call. Simply make
460 * sure it is not on a list with timer_del.
462 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
463 QEMUTimerCB *cb, void *opaque)
465 timer_init(ts, type, SCALE_NS, cb, opaque);
469 * timer_init_us:
470 * @type: the clock to associate with the timer
471 * @cb: the callback to call when the timer expires
472 * @opaque: the opaque pointer to pass to the callback
474 * Initialize a timer with microsecond scale on the default timer list
475 * associated with the clock.
477 * You need not call an explicit deinit call. Simply make
478 * sure it is not on a list with timer_del.
480 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
481 QEMUTimerCB *cb, void *opaque)
483 timer_init(ts, type, SCALE_US, cb, opaque);
487 * timer_init_ms:
488 * @type: the clock to associate with the timer
489 * @cb: the callback to call when the timer expires
490 * @opaque: the opaque pointer to pass to the callback
492 * Initialize a timer with millisecond scale on the default timer list
493 * associated with the clock.
495 * You need not call an explicit deinit call. Simply make
496 * sure it is not on a list with timer_del.
498 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
499 QEMUTimerCB *cb, void *opaque)
501 timer_init(ts, type, SCALE_MS, cb, opaque);
505 * timer_new_tl:
506 * @timer_list: the timer list to attach the timer to
507 * @scale: the scale value for the timer
508 * @cb: the callback to be called when the timer expires
509 * @opaque: the opaque pointer to be passed to the callback
511 * Creeate a new timer and associate it with @timer_list.
512 * The memory is allocated by the function.
514 * This is not the preferred interface unless you know you
515 * are going to call timer_free. Use timer_init instead.
517 * Returns: a pointer to the timer
519 static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
520 int scale,
521 QEMUTimerCB *cb,
522 void *opaque)
524 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
525 timer_init_tl(ts, timer_list, scale, cb, opaque);
526 return ts;
530 * timer_new:
531 * @type: the clock type to use
532 * @scale: the scale value for the timer
533 * @cb: the callback to be called when the timer expires
534 * @opaque: the opaque pointer to be passed to the callback
536 * Creeate a new timer and associate it with the default
537 * timer list for the clock type @type.
539 * Returns: a pointer to the timer
541 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
542 QEMUTimerCB *cb, void *opaque)
544 return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
548 * timer_new_ns:
549 * @clock: the clock to associate with the timer
550 * @callback: the callback to call when the timer expires
551 * @opaque: the opaque pointer to pass to the callback
553 * Create a new timer with nanosecond scale on the default timer list
554 * associated with the clock.
556 * Returns: a pointer to the newly created timer
558 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
559 void *opaque)
561 return timer_new(type, SCALE_NS, cb, opaque);
565 * timer_new_us:
566 * @clock: the clock to associate with the timer
567 * @callback: the callback to call when the timer expires
568 * @opaque: the opaque pointer to pass to the callback
570 * Create a new timer with microsecond scale on the default timer list
571 * associated with the clock.
573 * Returns: a pointer to the newly created timer
575 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
576 void *opaque)
578 return timer_new(type, SCALE_US, cb, opaque);
582 * timer_new_ms:
583 * @clock: the clock to associate with the timer
584 * @callback: the callback to call when the timer expires
585 * @opaque: the opaque pointer to pass to the callback
587 * Create a new timer with millisecond scale on the default timer list
588 * associated with the clock.
590 * Returns: a pointer to the newly created timer
592 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
593 void *opaque)
595 return timer_new(type, SCALE_MS, cb, opaque);
599 * timer_deinit:
600 * @ts: the timer to be de-initialised
602 * Deassociate the timer from any timerlist. You should
603 * call timer_del before. After this call, any further
604 * timer_del call cannot cause dangling pointer accesses
605 * even if the previously used timerlist is freed.
607 void timer_deinit(QEMUTimer *ts);
610 * timer_free:
611 * @ts: the timer
613 * Free a timer (it must not be on the active list)
615 void timer_free(QEMUTimer *ts);
618 * timer_del:
619 * @ts: the timer
621 * Delete a timer from the active list.
623 * This function is thread-safe but the timer and its timer list must not be
624 * freed while this function is running.
626 void timer_del(QEMUTimer *ts);
629 * timer_mod_ns:
630 * @ts: the timer
631 * @expire_time: the expiry time in nanoseconds
633 * Modify a timer to expire at @expire_time
635 * This function is thread-safe but the timer and its timer list must not be
636 * freed while this function is running.
638 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
641 * timer_mod_anticipate_ns:
642 * @ts: the timer
643 * @expire_time: the expiry time in nanoseconds
645 * Modify a timer to expire at @expire_time or the current time,
646 * whichever comes earlier.
648 * This function is thread-safe but the timer and its timer list must not be
649 * freed while this function is running.
651 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
654 * timer_mod:
655 * @ts: the timer
656 * @expire_time: the expire time in the units associated with the timer
658 * Modify a timer to expiry at @expire_time, taking into
659 * account the scale associated with the timer.
661 * This function is thread-safe but the timer and its timer list must not be
662 * freed while this function is running.
664 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
667 * timer_mod_anticipate:
668 * @ts: the timer
669 * @expire_time: the expiry time in nanoseconds
671 * Modify a timer to expire at @expire_time or the current time, whichever
672 * comes earlier, taking into account the scale associated with the timer.
674 * This function is thread-safe but the timer and its timer list must not be
675 * freed while this function is running.
677 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
680 * timer_pending:
681 * @ts: the timer
683 * Determines whether a timer is pending (i.e. is on the
684 * active list of timers, whether or not it has not yet expired).
686 * Returns: true if the timer is pending
688 bool timer_pending(QEMUTimer *ts);
691 * timer_expired:
692 * @ts: the timer
694 * Determines whether a timer has expired.
696 * Returns: true if the timer has expired
698 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
701 * timer_expire_time_ns:
702 * @ts: the timer
704 * Determine the expiry time of a timer
706 * Returns: the expiry time in nanoseconds
708 uint64_t timer_expire_time_ns(QEMUTimer *ts);
711 * timer_get:
712 * @f: the file
713 * @ts: the timer
715 * Read a timer @ts from a file @f
717 void timer_get(QEMUFile *f, QEMUTimer *ts);
720 * timer_put:
721 * @f: the file
722 * @ts: the timer
724 void timer_put(QEMUFile *f, QEMUTimer *ts);
727 * General utility functions
731 * qemu_timeout_ns_to_ms:
732 * @ns: nanosecond timeout value
734 * Convert a nanosecond timeout value (or -1) to
735 * a millisecond value (or -1), always rounding up.
737 * Returns: millisecond timeout value
739 int qemu_timeout_ns_to_ms(int64_t ns);
742 * qemu_poll_ns:
743 * @fds: Array of file descriptors
744 * @nfds: number of file descriptors
745 * @timeout: timeout in nanoseconds
747 * Perform a poll like g_poll but with a timeout in nanoseconds.
748 * See g_poll documentation for further details.
750 * Returns: number of fds ready
752 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
755 * qemu_soonest_timeout:
756 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
757 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
759 * Calculates the soonest of two timeout values. -1 means infinite, which
760 * is later than any other value.
762 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
764 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
766 /* we can abuse the fact that -1 (which means infinite) is a maximal
767 * value when cast to unsigned. As this is disgusting, it's kept in
768 * one inline function.
770 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
774 * initclocks:
776 * Initialise the clock & timer infrastructure
778 void init_clocks(void);
780 int64_t cpu_get_ticks(void);
781 /* Caller must hold BQL */
782 void cpu_enable_ticks(void);
783 /* Caller must hold BQL */
784 void cpu_disable_ticks(void);
786 static inline int64_t get_max_clock_jump(void)
788 /* This should be small enough to prevent excessive interrupts from being
789 * generated by the RTC on clock jumps, but large enough to avoid frequent
790 * unnecessary resets in idle VMs.
792 return 60 * NANOSECONDS_PER_SECOND;
796 * Low level clock functions
799 /* real time host monotonic timer */
800 static inline int64_t get_clock_realtime(void)
802 struct timeval tv;
804 gettimeofday(&tv, NULL);
805 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
808 /* Warning: don't insert tracepoints into these functions, they are
809 also used by simpletrace backend and tracepoints would cause
810 an infinite recursion! */
811 #ifdef _WIN32
812 extern int64_t clock_freq;
814 static inline int64_t get_clock(void)
816 LARGE_INTEGER ti;
817 QueryPerformanceCounter(&ti);
818 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
821 #else
823 extern int use_rt_clock;
825 static inline int64_t get_clock(void)
827 #ifdef CLOCK_MONOTONIC
828 if (use_rt_clock) {
829 struct timespec ts;
830 clock_gettime(CLOCK_MONOTONIC, &ts);
831 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
832 } else
833 #endif
835 /* XXX: using gettimeofday leads to problems if the date
836 changes, so it should be avoided. */
837 return get_clock_realtime();
840 #endif
842 /* icount */
843 int64_t cpu_get_icount_raw(void);
844 int64_t cpu_get_icount(void);
845 int64_t cpu_get_clock(void);
846 int64_t cpu_icount_to_ns(int64_t icount);
848 /*******************************************/
849 /* host CPU ticks (if available) */
851 #if defined(_ARCH_PPC)
853 static inline int64_t cpu_get_host_ticks(void)
855 int64_t retval;
856 #ifdef _ARCH_PPC64
857 /* This reads timebase in one 64bit go and includes Cell workaround from:
858 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
860 __asm__ __volatile__ ("mftb %0\n\t"
861 "cmpwi %0,0\n\t"
862 "beq- $-8"
863 : "=r" (retval));
864 #else
865 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
866 unsigned long junk;
867 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
868 "mfspr %L0,268\n\t" /* mftb */
869 "mfspr %0,269\n\t" /* mftbu */
870 "cmpw %0,%1\n\t"
871 "bne $-16"
872 : "=r" (retval), "=r" (junk));
873 #endif
874 return retval;
877 #elif defined(__i386__)
879 static inline int64_t cpu_get_host_ticks(void)
881 int64_t val;
882 asm volatile ("rdtsc" : "=A" (val));
883 return val;
886 #elif defined(__x86_64__)
888 static inline int64_t cpu_get_host_ticks(void)
890 uint32_t low,high;
891 int64_t val;
892 asm volatile("rdtsc" : "=a" (low), "=d" (high));
893 val = high;
894 val <<= 32;
895 val |= low;
896 return val;
899 #elif defined(__hppa__)
901 static inline int64_t cpu_get_host_ticks(void)
903 int val;
904 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
905 return val;
908 #elif defined(__ia64)
910 static inline int64_t cpu_get_host_ticks(void)
912 int64_t val;
913 asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
914 return val;
917 #elif defined(__s390__)
919 static inline int64_t cpu_get_host_ticks(void)
921 int64_t val;
922 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
923 return val;
926 #elif defined(__sparc__)
928 static inline int64_t cpu_get_host_ticks (void)
930 #if defined(_LP64)
931 uint64_t rval;
932 asm volatile("rd %%tick,%0" : "=r"(rval));
933 return rval;
934 #else
935 /* We need an %o or %g register for this. For recent enough gcc
936 there is an "h" constraint for that. Don't bother with that. */
937 union {
938 uint64_t i64;
939 struct {
940 uint32_t high;
941 uint32_t low;
942 } i32;
943 } rval;
944 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
945 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
946 return rval.i64;
947 #endif
950 #elif defined(__mips__) && \
951 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
953 * binutils wants to use rdhwr only on mips32r2
954 * but as linux kernel emulate it, it's fine
955 * to use it.
958 #define MIPS_RDHWR(rd, value) { \
959 __asm__ __volatile__ (".set push\n\t" \
960 ".set mips32r2\n\t" \
961 "rdhwr %0, "rd"\n\t" \
962 ".set pop" \
963 : "=r" (value)); \
966 static inline int64_t cpu_get_host_ticks(void)
968 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
969 uint32_t count;
970 static uint32_t cyc_per_count = 0;
972 if (!cyc_per_count) {
973 MIPS_RDHWR("$3", cyc_per_count);
976 MIPS_RDHWR("$2", count);
977 return (int64_t)(count * cyc_per_count);
980 #elif defined(__alpha__)
982 static inline int64_t cpu_get_host_ticks(void)
984 uint64_t cc;
985 uint32_t cur, ofs;
987 asm volatile("rpcc %0" : "=r"(cc));
988 cur = cc;
989 ofs = cc >> 32;
990 return cur - ofs;
993 #else
994 /* The host CPU doesn't have an easily accessible cycle counter.
995 Just return a monotonically increasing value. This will be
996 totally wrong, but hopefully better than nothing. */
997 static inline int64_t cpu_get_host_ticks (void)
999 static int64_t ticks = 0;
1000 return ticks++;
1002 #endif
1004 #ifdef CONFIG_PROFILER
1005 static inline int64_t profile_getclock(void)
1007 return get_clock();
1010 extern int64_t tcg_time;
1011 extern int64_t dev_time;
1012 #endif
1014 #endif