fpu: Bound increment for scalbn
[qemu.git] / include / qemu / timer.h
blob39ea907e65f0957b227cd83c5cac4d72a6ab2b39
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 runs even if the virtual
25 * machine is stopped.
27 * @QEMU_CLOCK_VIRTUAL: virtual clock
29 * The virtual clock only runs during the emulation. It stops
30 * when the virtual machine is stopped.
32 * @QEMU_CLOCK_HOST: host clock
34 * The host clock should be used 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).
39 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
41 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
42 * In icount mode, this clock counts nanoseconds while the virtual
43 * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
44 * while the CPUs are sleeping and thus not executing instructions.
47 typedef enum {
48 QEMU_CLOCK_REALTIME = 0,
49 QEMU_CLOCK_VIRTUAL = 1,
50 QEMU_CLOCK_HOST = 2,
51 QEMU_CLOCK_VIRTUAL_RT = 3,
52 QEMU_CLOCK_MAX
53 } QEMUClockType;
55 typedef struct QEMUTimerList QEMUTimerList;
57 struct QEMUTimerListGroup {
58 QEMUTimerList *tl[QEMU_CLOCK_MAX];
61 typedef void QEMUTimerCB(void *opaque);
62 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
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;
73 extern QEMUTimerListGroup main_loop_tlg;
76 * qemu_clock_get_ns;
77 * @type: the clock type
79 * Get the nanosecond value of a clock with
80 * type @type
82 * Returns: the clock value in nanoseconds
84 int64_t qemu_clock_get_ns(QEMUClockType type);
86 /**
87 * qemu_clock_get_ms;
88 * @type: the clock type
90 * Get the millisecond value of a clock with
91 * type @type
93 * Returns: the clock value in milliseconds
95 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
97 return qemu_clock_get_ns(type) / SCALE_MS;
101 * qemu_clock_get_us;
102 * @type: the clock type
104 * Get the microsecond value of a clock with
105 * type @type
107 * Returns: the clock value in microseconds
109 static inline int64_t qemu_clock_get_us(QEMUClockType type)
111 return qemu_clock_get_ns(type) / SCALE_US;
115 * qemu_clock_has_timers:
116 * @type: the clock type
118 * Determines whether a clock's default timer list
119 * has timers attached
121 * Note that this function should not be used when other threads also access
122 * the timer list. The return value may be outdated by the time it is acted
123 * upon.
125 * Returns: true if the clock's default timer list
126 * has timers attached
128 bool qemu_clock_has_timers(QEMUClockType type);
131 * qemu_clock_expired:
132 * @type: the clock type
134 * Determines whether a clock's default timer list
135 * has an expired timer.
137 * Returns: true if the clock's default timer list has
138 * an expired timer
140 bool qemu_clock_expired(QEMUClockType type);
143 * qemu_clock_use_for_deadline:
144 * @type: the clock type
146 * Determine whether a clock should be used for deadline
147 * calculations. Some clocks, for instance vm_clock with
148 * use_icount set, do not count in nanoseconds. Such clocks
149 * are not used for deadline calculations, and are presumed
150 * to interrupt any poll using qemu_notify/aio_notify
151 * etc.
153 * Returns: true if the clock runs in nanoseconds and
154 * should be used for a deadline.
156 bool qemu_clock_use_for_deadline(QEMUClockType type);
159 * qemu_clock_deadline_ns_all:
160 * @type: the clock type
162 * Calculate the deadline across all timer lists associated
163 * with a clock (as opposed to just the default one)
164 * in nanoseconds, or -1 if no timer is set to expire.
166 * Returns: time until expiry in nanoseconds or -1
168 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
171 * qemu_clock_get_main_loop_timerlist:
172 * @type: the clock type
174 * Return the default timer list associated with a clock.
176 * Returns: the default timer list
178 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
181 * qemu_clock_nofify:
182 * @type: the clock type
184 * Call the notifier callback connected with the default timer
185 * list linked to the clock, or qemu_notify() if none.
187 void qemu_clock_notify(QEMUClockType type);
190 * qemu_clock_enable:
191 * @type: the clock type
192 * @enabled: true to enable, false to disable
194 * Enable or disable a clock
195 * Disabling the clock will wait for related timerlists to stop
196 * executing qemu_run_timers. Thus, this functions should not
197 * be used from the callback of a timer that is based on @clock.
198 * Doing so would cause a deadlock.
200 * Caller should hold BQL.
202 void qemu_clock_enable(QEMUClockType type, bool enabled);
205 * qemu_start_warp_timer:
207 * Starts a timer for virtual clock update
209 void qemu_start_warp_timer(void);
212 * qemu_clock_register_reset_notifier:
213 * @type: the clock type
214 * @notifier: the notifier function
216 * Register a notifier function to call when the clock
217 * concerned is reset.
219 void qemu_clock_register_reset_notifier(QEMUClockType type,
220 Notifier *notifier);
223 * qemu_clock_unregister_reset_notifier:
224 * @type: the clock type
225 * @notifier: the notifier function
227 * Unregister a notifier function to call when the clock
228 * concerned is reset.
230 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
231 Notifier *notifier);
234 * qemu_clock_run_timers:
235 * @type: clock on which to operate
237 * Run all the timers associated with the default timer list
238 * of a clock.
240 * Returns: true if any timer ran.
242 bool qemu_clock_run_timers(QEMUClockType type);
245 * qemu_clock_run_all_timers:
247 * Run all the timers associated with the default timer list
248 * of every clock.
250 * Returns: true if any timer ran.
252 bool qemu_clock_run_all_timers(void);
255 * qemu_clock_get_last:
257 * Returns last clock query time.
259 uint64_t qemu_clock_get_last(QEMUClockType type);
261 * qemu_clock_set_last:
263 * Sets last clock query time.
265 void qemu_clock_set_last(QEMUClockType type, uint64_t last);
269 * QEMUTimerList
273 * timerlist_new:
274 * @type: the clock type to associate with the timerlist
275 * @cb: the callback to call on notification
276 * @opaque: the opaque pointer to pass to the callback
278 * Create a new timerlist associated with the clock of
279 * type @type.
281 * Returns: a pointer to the QEMUTimerList created
283 QEMUTimerList *timerlist_new(QEMUClockType type,
284 QEMUTimerListNotifyCB *cb, void *opaque);
287 * timerlist_free:
288 * @timer_list: the timer list to free
290 * Frees a timer_list. It must have no active timers.
292 void timerlist_free(QEMUTimerList *timer_list);
295 * timerlist_has_timers:
296 * @timer_list: the timer list to operate on
298 * Determine whether a timer list has active timers
300 * Note that this function should not be used when other threads also access
301 * the timer list. The return value may be outdated by the time it is acted
302 * upon.
304 * Returns: true if the timer list has timers.
306 bool timerlist_has_timers(QEMUTimerList *timer_list);
309 * timerlist_expired:
310 * @timer_list: the timer list to operate on
312 * Determine whether a timer list has any timers which
313 * are expired.
315 * Returns: true if the timer list has timers which
316 * have expired.
318 bool timerlist_expired(QEMUTimerList *timer_list);
321 * timerlist_deadline_ns:
322 * @timer_list: the timer list to operate on
324 * Determine the deadline for a timer_list, i.e.
325 * the number of nanoseconds until the first timer
326 * expires. Return -1 if there are no timers.
328 * Returns: the number of nanoseconds until the earliest
329 * timer expires -1 if none
331 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
334 * timerlist_get_clock:
335 * @timer_list: the timer list to operate on
337 * Determine the clock type associated with a timer list.
339 * Returns: the clock type associated with the
340 * timer list.
342 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
345 * timerlist_run_timers:
346 * @timer_list: the timer list to use
348 * Call all expired timers associated with the timer list.
350 * Returns: true if any timer expired
352 bool timerlist_run_timers(QEMUTimerList *timer_list);
355 * timerlist_notify:
356 * @timer_list: the timer list to use
358 * call the notifier callback associated with the timer list.
360 void timerlist_notify(QEMUTimerList *timer_list);
363 * QEMUTimerListGroup
367 * timerlistgroup_init:
368 * @tlg: the timer list group
369 * @cb: the callback to call when a notify is required
370 * @opaque: the opaque pointer to be passed to the callback.
372 * Initialise a timer list group. This must already be
373 * allocated in memory and zeroed. The notifier callback is
374 * called whenever a clock in the timer list group is
375 * reenabled or whenever a timer associated with any timer
376 * list is modified. If @cb is specified as null, qemu_notify()
377 * is used instead.
379 void timerlistgroup_init(QEMUTimerListGroup *tlg,
380 QEMUTimerListNotifyCB *cb, void *opaque);
383 * timerlistgroup_deinit:
384 * @tlg: the timer list group
386 * Deinitialise a timer list group. This must already be
387 * initialised. Note the memory is not freed.
389 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
392 * timerlistgroup_run_timers:
393 * @tlg: the timer list group
395 * Run the timers associated with a timer list group.
396 * This will run timers on multiple clocks.
398 * Returns: true if any timer callback ran
400 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
403 * timerlistgroup_deadline_ns:
404 * @tlg: the timer list group
406 * Determine the deadline of the soonest timer to
407 * expire associated with any timer list linked to
408 * the timer list group. Only clocks suitable for
409 * deadline calculation are included.
411 * Returns: the deadline in nanoseconds or -1 if no
412 * timers are to expire.
414 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
417 * QEMUTimer
421 * timer_init_tl:
422 * @ts: the timer to be initialised
423 * @timer_list: the timer list to attach the timer to
424 * @scale: the scale value for the timer
425 * @cb: the callback to be called when the timer expires
426 * @opaque: the opaque pointer to be passed to the callback
428 * Initialise a new timer and associate it with @timer_list.
429 * The caller is responsible for allocating the memory.
431 * You need not call an explicit deinit call. Simply make
432 * sure it is not on a list with timer_del.
434 void timer_init_tl(QEMUTimer *ts,
435 QEMUTimerList *timer_list, int scale,
436 QEMUTimerCB *cb, void *opaque);
439 * timer_init:
440 * @ts: the timer to be initialised
441 * @type: the clock to associate with the timer
442 * @scale: the scale value for the timer
443 * @cb: the callback to call when the timer expires
444 * @opaque: the opaque pointer to pass to the callback
446 * Initialize a timer with the given scale on the default timer list
447 * associated with the clock.
449 * You need not call an explicit deinit call. Simply make
450 * sure it is not on a list with timer_del.
452 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
453 QEMUTimerCB *cb, void *opaque)
455 timer_init_tl(ts, main_loop_tlg.tl[type], scale, cb, opaque);
459 * timer_init_ns:
460 * @ts: the timer to be initialised
461 * @type: the clock to associate with the timer
462 * @cb: the callback to call when the timer expires
463 * @opaque: the opaque pointer to pass to the callback
465 * Initialize a timer with nanosecond scale on the default timer list
466 * associated with the clock.
468 * You need not call an explicit deinit call. Simply make
469 * sure it is not on a list with timer_del.
471 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
472 QEMUTimerCB *cb, void *opaque)
474 timer_init(ts, type, SCALE_NS, cb, opaque);
478 * timer_init_us:
479 * @ts: the timer to be initialised
480 * @type: the clock to associate with the timer
481 * @cb: the callback to call when the timer expires
482 * @opaque: the opaque pointer to pass to the callback
484 * Initialize a timer with microsecond scale on the default timer list
485 * associated with the clock.
487 * You need not call an explicit deinit call. Simply make
488 * sure it is not on a list with timer_del.
490 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
491 QEMUTimerCB *cb, void *opaque)
493 timer_init(ts, type, SCALE_US, cb, opaque);
497 * timer_init_ms:
498 * @ts: the timer to be initialised
499 * @type: the clock to associate with the timer
500 * @cb: the callback to call when the timer expires
501 * @opaque: the opaque pointer to pass to the callback
503 * Initialize a timer with millisecond scale on the default timer list
504 * associated with the clock.
506 * You need not call an explicit deinit call. Simply make
507 * sure it is not on a list with timer_del.
509 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
510 QEMUTimerCB *cb, void *opaque)
512 timer_init(ts, type, SCALE_MS, cb, opaque);
516 * timer_new_tl:
517 * @timer_list: the timer list to attach the timer to
518 * @scale: the scale value for the timer
519 * @cb: the callback to be called when the timer expires
520 * @opaque: the opaque pointer to be passed to the callback
522 * Create a new timer and associate it with @timer_list.
523 * The memory is allocated by the function.
525 * This is not the preferred interface unless you know you
526 * are going to call timer_free. Use timer_init instead.
528 * Returns: a pointer to the timer
530 static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
531 int scale,
532 QEMUTimerCB *cb,
533 void *opaque)
535 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
536 timer_init_tl(ts, timer_list, scale, cb, opaque);
537 return ts;
541 * timer_new:
542 * @type: the clock type to use
543 * @scale: the scale value for the timer
544 * @cb: the callback to be called when the timer expires
545 * @opaque: the opaque pointer to be passed to the callback
547 * Create a new timer and associate it with the default
548 * timer list for the clock type @type.
550 * The default timer list has one special feature: in icount mode,
551 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
552 * not true of other timer lists, which are typically associated
553 * with an AioContext---each of them runs its timer callbacks in its own
554 * AioContext thread.
556 * Returns: a pointer to the timer
558 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
559 QEMUTimerCB *cb, void *opaque)
561 return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
565 * timer_new_ns:
566 * @type: the clock type to associate with the timer
567 * @cb: the callback to call when the timer expires
568 * @opaque: the opaque pointer to pass to the callback
570 * Create a new timer with nanosecond scale on the default timer list
571 * associated with the clock.
573 * The default timer list has one special feature: in icount mode,
574 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
575 * not true of other timer lists, which are typically associated
576 * with an AioContext---each of them runs its timer callbacks in its own
577 * AioContext thread.
579 * Returns: a pointer to the newly created timer
581 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
582 void *opaque)
584 return timer_new(type, SCALE_NS, cb, opaque);
588 * timer_new_us:
589 * @type: the clock type to associate with the timer
590 * @cb: the callback to call when the timer expires
591 * @opaque: the opaque pointer to pass to the callback
593 * The default timer list has one special feature: in icount mode,
594 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
595 * not true of other timer lists, which are typically associated
596 * with an AioContext---each of them runs its timer callbacks in its own
597 * AioContext thread.
599 * Create a new timer with microsecond scale on the default timer list
600 * associated with the clock.
602 * Returns: a pointer to the newly created timer
604 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
605 void *opaque)
607 return timer_new(type, SCALE_US, cb, opaque);
611 * timer_new_ms:
612 * @type: the clock type to associate with the timer
613 * @cb: the callback to call when the timer expires
614 * @opaque: the opaque pointer to pass to the callback
616 * The default timer list has one special feature: in icount mode,
617 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
618 * not true of other timer lists, which are typically associated
619 * with an AioContext---each of them runs its timer callbacks in its own
620 * AioContext thread.
622 * Create a new timer with millisecond scale on the default timer list
623 * associated with the clock.
625 * Returns: a pointer to the newly created timer
627 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
628 void *opaque)
630 return timer_new(type, SCALE_MS, cb, opaque);
634 * timer_deinit:
635 * @ts: the timer to be de-initialised
637 * Deassociate the timer from any timerlist. You should
638 * call timer_del before. After this call, any further
639 * timer_del call cannot cause dangling pointer accesses
640 * even if the previously used timerlist is freed.
642 void timer_deinit(QEMUTimer *ts);
645 * timer_free:
646 * @ts: the timer
648 * Free a timer (it must not be on the active list)
650 static inline void timer_free(QEMUTimer *ts)
652 g_free(ts);
656 * timer_del:
657 * @ts: the timer
659 * Delete a timer from the active list.
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_del(QEMUTimer *ts);
667 * timer_mod_ns:
668 * @ts: the timer
669 * @expire_time: the expiry time in nanoseconds
671 * Modify a timer to expire at @expire_time
673 * This function is thread-safe but the timer and its timer list must not be
674 * freed while this function is running.
676 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
679 * timer_mod_anticipate_ns:
680 * @ts: the timer
681 * @expire_time: the expiry time in nanoseconds
683 * Modify a timer to expire at @expire_time or the current time,
684 * whichever comes earlier.
686 * This function is thread-safe but the timer and its timer list must not be
687 * freed while this function is running.
689 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
692 * timer_mod:
693 * @ts: the timer
694 * @expire_time: the expire time in the units associated with the timer
696 * Modify a timer to expiry at @expire_time, taking into
697 * account the scale associated with the timer.
699 * This function is thread-safe but the timer and its timer list must not be
700 * freed while this function is running.
702 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
705 * timer_mod_anticipate:
706 * @ts: the timer
707 * @expire_time: the expiry time in nanoseconds
709 * Modify a timer to expire at @expire_time or the current time, whichever
710 * comes earlier, taking into account the scale associated with the timer.
712 * This function is thread-safe but the timer and its timer list must not be
713 * freed while this function is running.
715 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
718 * timer_pending:
719 * @ts: the timer
721 * Determines whether a timer is pending (i.e. is on the
722 * active list of timers, whether or not it has not yet expired).
724 * Returns: true if the timer is pending
726 bool timer_pending(QEMUTimer *ts);
729 * timer_expired:
730 * @ts: the timer
731 * @current_time: the current time
733 * Determines whether a timer has expired.
735 * Returns: true if the timer has expired
737 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
740 * timer_expire_time_ns:
741 * @ts: the timer
743 * Determine the expiry time of a timer
745 * Returns: the expiry time in nanoseconds
747 uint64_t timer_expire_time_ns(QEMUTimer *ts);
750 * timer_get:
751 * @f: the file
752 * @ts: the timer
754 * Read a timer @ts from a file @f
756 void timer_get(QEMUFile *f, QEMUTimer *ts);
759 * timer_put:
760 * @f: the file
761 * @ts: the timer
763 void timer_put(QEMUFile *f, QEMUTimer *ts);
766 * General utility functions
770 * qemu_timeout_ns_to_ms:
771 * @ns: nanosecond timeout value
773 * Convert a nanosecond timeout value (or -1) to
774 * a millisecond value (or -1), always rounding up.
776 * Returns: millisecond timeout value
778 int qemu_timeout_ns_to_ms(int64_t ns);
781 * qemu_poll_ns:
782 * @fds: Array of file descriptors
783 * @nfds: number of file descriptors
784 * @timeout: timeout in nanoseconds
786 * Perform a poll like g_poll but with a timeout in nanoseconds.
787 * See g_poll documentation for further details.
789 * Returns: number of fds ready
791 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
794 * qemu_soonest_timeout:
795 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
796 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
798 * Calculates the soonest of two timeout values. -1 means infinite, which
799 * is later than any other value.
801 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
803 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
805 /* we can abuse the fact that -1 (which means infinite) is a maximal
806 * value when cast to unsigned. As this is disgusting, it's kept in
807 * one inline function.
809 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
813 * initclocks:
815 * Initialise the clock & timer infrastructure
817 void init_clocks(QEMUTimerListNotifyCB *notify_cb);
819 int64_t cpu_get_ticks(void);
820 /* Caller must hold BQL */
821 void cpu_enable_ticks(void);
822 /* Caller must hold BQL */
823 void cpu_disable_ticks(void);
825 static inline int64_t get_max_clock_jump(void)
827 /* This should be small enough to prevent excessive interrupts from being
828 * generated by the RTC on clock jumps, but large enough to avoid frequent
829 * unnecessary resets in idle VMs.
831 return 60 * NANOSECONDS_PER_SECOND;
835 * Low level clock functions
838 /* get host real time in nanosecond */
839 static inline int64_t get_clock_realtime(void)
841 struct timeval tv;
843 gettimeofday(&tv, NULL);
844 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
847 /* Warning: don't insert tracepoints into these functions, they are
848 also used by simpletrace backend and tracepoints would cause
849 an infinite recursion! */
850 #ifdef _WIN32
851 extern int64_t clock_freq;
853 static inline int64_t get_clock(void)
855 LARGE_INTEGER ti;
856 QueryPerformanceCounter(&ti);
857 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
860 #else
862 extern int use_rt_clock;
864 static inline int64_t get_clock(void)
866 #ifdef CLOCK_MONOTONIC
867 if (use_rt_clock) {
868 struct timespec ts;
869 clock_gettime(CLOCK_MONOTONIC, &ts);
870 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
871 } else
872 #endif
874 /* XXX: using gettimeofday leads to problems if the date
875 changes, so it should be avoided. */
876 return get_clock_realtime();
879 #endif
881 /* icount */
882 int64_t cpu_get_icount_raw(void);
883 int64_t cpu_get_icount(void);
884 int64_t cpu_get_clock(void);
885 int64_t cpu_icount_to_ns(int64_t icount);
886 void cpu_update_icount(CPUState *cpu);
888 /*******************************************/
889 /* host CPU ticks (if available) */
891 #if defined(_ARCH_PPC)
893 static inline int64_t cpu_get_host_ticks(void)
895 int64_t retval;
896 #ifdef _ARCH_PPC64
897 /* This reads timebase in one 64bit go and includes Cell workaround from:
898 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
900 __asm__ __volatile__ ("mftb %0\n\t"
901 "cmpwi %0,0\n\t"
902 "beq- $-8"
903 : "=r" (retval));
904 #else
905 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
906 unsigned long junk;
907 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
908 "mfspr %L0,268\n\t" /* mftb */
909 "mfspr %0,269\n\t" /* mftbu */
910 "cmpw %0,%1\n\t"
911 "bne $-16"
912 : "=r" (retval), "=r" (junk));
913 #endif
914 return retval;
917 #elif defined(__i386__)
919 static inline int64_t cpu_get_host_ticks(void)
921 int64_t val;
922 asm volatile ("rdtsc" : "=A" (val));
923 return val;
926 #elif defined(__x86_64__)
928 static inline int64_t cpu_get_host_ticks(void)
930 uint32_t low,high;
931 int64_t val;
932 asm volatile("rdtsc" : "=a" (low), "=d" (high));
933 val = high;
934 val <<= 32;
935 val |= low;
936 return val;
939 #elif defined(__hppa__)
941 static inline int64_t cpu_get_host_ticks(void)
943 int val;
944 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
945 return val;
948 #elif defined(__s390__)
950 static inline int64_t cpu_get_host_ticks(void)
952 int64_t val;
953 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
954 return val;
957 #elif defined(__sparc__)
959 static inline int64_t cpu_get_host_ticks (void)
961 #if defined(_LP64)
962 uint64_t rval;
963 asm volatile("rd %%tick,%0" : "=r"(rval));
964 return rval;
965 #else
966 /* We need an %o or %g register for this. For recent enough gcc
967 there is an "h" constraint for that. Don't bother with that. */
968 union {
969 uint64_t i64;
970 struct {
971 uint32_t high;
972 uint32_t low;
973 } i32;
974 } rval;
975 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
976 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
977 return rval.i64;
978 #endif
981 #elif defined(__mips__) && \
982 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
984 * binutils wants to use rdhwr only on mips32r2
985 * but as linux kernel emulate it, it's fine
986 * to use it.
989 #define MIPS_RDHWR(rd, value) { \
990 __asm__ __volatile__ (".set push\n\t" \
991 ".set mips32r2\n\t" \
992 "rdhwr %0, "rd"\n\t" \
993 ".set pop" \
994 : "=r" (value)); \
997 static inline int64_t cpu_get_host_ticks(void)
999 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
1000 uint32_t count;
1001 static uint32_t cyc_per_count = 0;
1003 if (!cyc_per_count) {
1004 MIPS_RDHWR("$3", cyc_per_count);
1007 MIPS_RDHWR("$2", count);
1008 return (int64_t)(count * cyc_per_count);
1011 #elif defined(__alpha__)
1013 static inline int64_t cpu_get_host_ticks(void)
1015 uint64_t cc;
1016 uint32_t cur, ofs;
1018 asm volatile("rpcc %0" : "=r"(cc));
1019 cur = cc;
1020 ofs = cc >> 32;
1021 return cur - ofs;
1024 #else
1025 /* The host CPU doesn't have an easily accessible cycle counter.
1026 Just return a monotonically increasing value. This will be
1027 totally wrong, but hopefully better than nothing. */
1028 static inline int64_t cpu_get_host_ticks(void)
1030 return get_clock();
1032 #endif
1034 #ifdef CONFIG_PROFILER
1035 static inline int64_t profile_getclock(void)
1037 return get_clock();
1040 extern int64_t tcg_time;
1041 extern int64_t dev_time;
1042 #endif
1044 #endif