target/arm: Use _ra versions of cpu_stl_data() in v7M helpers
[qemu/ar7.git] / include / qemu / timer.h
blob5d978e163466c6b6680afc2ceed7eb46e27e8d25
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
4 #include "qemu/bitops.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 /**
56 * QEMU Timer attributes:
58 * An individual timer may be given one or multiple attributes when initialized.
59 * Each attribute corresponds to one bit. Attributes modify the processing
60 * of timers when they fire.
62 * The following attributes are available:
64 * QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem
66 * Timers with this attribute do not recorded in rr mode, therefore it could be
67 * used for the subsystems that operate outside the guest core. Applicable only
68 * with virtual clock type.
71 #define QEMU_TIMER_ATTR_EXTERNAL BIT(0)
73 typedef struct QEMUTimerList QEMUTimerList;
75 struct QEMUTimerListGroup {
76 QEMUTimerList *tl[QEMU_CLOCK_MAX];
79 typedef void QEMUTimerCB(void *opaque);
80 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
82 struct QEMUTimer {
83 int64_t expire_time; /* in nanoseconds */
84 QEMUTimerList *timer_list;
85 QEMUTimerCB *cb;
86 void *opaque;
87 QEMUTimer *next;
88 int attributes;
89 int scale;
92 extern QEMUTimerListGroup main_loop_tlg;
95 * qemu_clock_get_ns;
96 * @type: the clock type
98 * Get the nanosecond value of a clock with
99 * type @type
101 * Returns: the clock value in nanoseconds
103 int64_t qemu_clock_get_ns(QEMUClockType type);
106 * qemu_clock_get_ms;
107 * @type: the clock type
109 * Get the millisecond value of a clock with
110 * type @type
112 * Returns: the clock value in milliseconds
114 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
116 return qemu_clock_get_ns(type) / SCALE_MS;
120 * qemu_clock_get_us;
121 * @type: the clock type
123 * Get the microsecond value of a clock with
124 * type @type
126 * Returns: the clock value in microseconds
128 static inline int64_t qemu_clock_get_us(QEMUClockType type)
130 return qemu_clock_get_ns(type) / SCALE_US;
134 * qemu_clock_has_timers:
135 * @type: the clock type
137 * Determines whether a clock's default timer list
138 * has timers attached
140 * Note that this function should not be used when other threads also access
141 * the timer list. The return value may be outdated by the time it is acted
142 * upon.
144 * Returns: true if the clock's default timer list
145 * has timers attached
147 bool qemu_clock_has_timers(QEMUClockType type);
150 * qemu_clock_expired:
151 * @type: the clock type
153 * Determines whether a clock's default timer list
154 * has an expired timer.
156 * Returns: true if the clock's default timer list has
157 * an expired timer
159 bool qemu_clock_expired(QEMUClockType type);
162 * qemu_clock_use_for_deadline:
163 * @type: the clock type
165 * Determine whether a clock should be used for deadline
166 * calculations. Some clocks, for instance vm_clock with
167 * use_icount set, do not count in nanoseconds. Such clocks
168 * are not used for deadline calculations, and are presumed
169 * to interrupt any poll using qemu_notify/aio_notify
170 * etc.
172 * Returns: true if the clock runs in nanoseconds and
173 * should be used for a deadline.
175 bool qemu_clock_use_for_deadline(QEMUClockType type);
178 * qemu_clock_deadline_ns_all:
179 * @type: the clock type
181 * Calculate the deadline across all timer lists associated
182 * with a clock (as opposed to just the default one)
183 * in nanoseconds, or -1 if no timer is set to expire.
185 * Returns: time until expiry in nanoseconds or -1
187 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
190 * qemu_clock_get_main_loop_timerlist:
191 * @type: the clock type
193 * Return the default timer list associated with a clock.
195 * Returns: the default timer list
197 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
200 * qemu_clock_nofify:
201 * @type: the clock type
203 * Call the notifier callback connected with the default timer
204 * list linked to the clock, or qemu_notify() if none.
206 void qemu_clock_notify(QEMUClockType type);
209 * qemu_clock_enable:
210 * @type: the clock type
211 * @enabled: true to enable, false to disable
213 * Enable or disable a clock
214 * Disabling the clock will wait for related timerlists to stop
215 * executing qemu_run_timers. Thus, this functions should not
216 * be used from the callback of a timer that is based on @clock.
217 * Doing so would cause a deadlock.
219 * Caller should hold BQL.
221 void qemu_clock_enable(QEMUClockType type, bool enabled);
224 * qemu_start_warp_timer:
226 * Starts a timer for virtual clock update
228 void qemu_start_warp_timer(void);
231 * qemu_clock_register_reset_notifier:
232 * @type: the clock type
233 * @notifier: the notifier function
235 * Register a notifier function to call when the clock
236 * concerned is reset.
238 void qemu_clock_register_reset_notifier(QEMUClockType type,
239 Notifier *notifier);
242 * qemu_clock_unregister_reset_notifier:
243 * @type: the clock type
244 * @notifier: the notifier function
246 * Unregister a notifier function to call when the clock
247 * concerned is reset.
249 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
250 Notifier *notifier);
253 * qemu_clock_run_timers:
254 * @type: clock on which to operate
256 * Run all the timers associated with the default timer list
257 * of a clock.
259 * Returns: true if any timer ran.
261 bool qemu_clock_run_timers(QEMUClockType type);
264 * qemu_clock_run_all_timers:
266 * Run all the timers associated with the default timer list
267 * of every clock.
269 * Returns: true if any timer ran.
271 bool qemu_clock_run_all_timers(void);
274 * qemu_clock_get_last:
276 * Returns last clock query time.
278 uint64_t qemu_clock_get_last(QEMUClockType type);
280 * qemu_clock_set_last:
282 * Sets last clock query time.
284 void qemu_clock_set_last(QEMUClockType type, uint64_t last);
288 * QEMUTimerList
292 * timerlist_new:
293 * @type: the clock type to associate with the timerlist
294 * @cb: the callback to call on notification
295 * @opaque: the opaque pointer to pass to the callback
297 * Create a new timerlist associated with the clock of
298 * type @type.
300 * Returns: a pointer to the QEMUTimerList created
302 QEMUTimerList *timerlist_new(QEMUClockType type,
303 QEMUTimerListNotifyCB *cb, void *opaque);
306 * timerlist_free:
307 * @timer_list: the timer list to free
309 * Frees a timer_list. It must have no active timers.
311 void timerlist_free(QEMUTimerList *timer_list);
314 * timerlist_has_timers:
315 * @timer_list: the timer list to operate on
317 * Determine whether a timer list has active timers
319 * Note that this function should not be used when other threads also access
320 * the timer list. The return value may be outdated by the time it is acted
321 * upon.
323 * Returns: true if the timer list has timers.
325 bool timerlist_has_timers(QEMUTimerList *timer_list);
328 * timerlist_expired:
329 * @timer_list: the timer list to operate on
331 * Determine whether a timer list has any timers which
332 * are expired.
334 * Returns: true if the timer list has timers which
335 * have expired.
337 bool timerlist_expired(QEMUTimerList *timer_list);
340 * timerlist_deadline_ns:
341 * @timer_list: the timer list to operate on
343 * Determine the deadline for a timer_list, i.e.
344 * the number of nanoseconds until the first timer
345 * expires. Return -1 if there are no timers.
347 * Returns: the number of nanoseconds until the earliest
348 * timer expires -1 if none
350 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
353 * timerlist_get_clock:
354 * @timer_list: the timer list to operate on
356 * Determine the clock type associated with a timer list.
358 * Returns: the clock type associated with the
359 * timer list.
361 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
364 * timerlist_run_timers:
365 * @timer_list: the timer list to use
367 * Call all expired timers associated with the timer list.
369 * Returns: true if any timer expired
371 bool timerlist_run_timers(QEMUTimerList *timer_list);
374 * timerlist_notify:
375 * @timer_list: the timer list to use
377 * call the notifier callback associated with the timer list.
379 void timerlist_notify(QEMUTimerList *timer_list);
382 * QEMUTimerListGroup
386 * timerlistgroup_init:
387 * @tlg: the timer list group
388 * @cb: the callback to call when a notify is required
389 * @opaque: the opaque pointer to be passed to the callback.
391 * Initialise a timer list group. This must already be
392 * allocated in memory and zeroed. The notifier callback is
393 * called whenever a clock in the timer list group is
394 * reenabled or whenever a timer associated with any timer
395 * list is modified. If @cb is specified as null, qemu_notify()
396 * is used instead.
398 void timerlistgroup_init(QEMUTimerListGroup *tlg,
399 QEMUTimerListNotifyCB *cb, void *opaque);
402 * timerlistgroup_deinit:
403 * @tlg: the timer list group
405 * Deinitialise a timer list group. This must already be
406 * initialised. Note the memory is not freed.
408 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
411 * timerlistgroup_run_timers:
412 * @tlg: the timer list group
414 * Run the timers associated with a timer list group.
415 * This will run timers on multiple clocks.
417 * Returns: true if any timer callback ran
419 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
422 * timerlistgroup_deadline_ns:
423 * @tlg: the timer list group
425 * Determine the deadline of the soonest timer to
426 * expire associated with any timer list linked to
427 * the timer list group. Only clocks suitable for
428 * deadline calculation are included.
430 * Returns: the deadline in nanoseconds or -1 if no
431 * timers are to expire.
433 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
436 * QEMUTimer
440 * timer_init_full:
441 * @ts: the timer to be initialised
442 * @timer_list_group: (optional) the timer list group to attach the timer to
443 * @type: the clock type to use
444 * @scale: the scale value for the timer
445 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
446 * @cb: the callback to be called when the timer expires
447 * @opaque: the opaque pointer to be passed to the callback
449 * Initialise a timer with the given scale and attributes,
450 * and associate it with timer list for given clock @type in @timer_list_group
451 * (or default timer list group, if NULL).
452 * The caller is responsible for allocating the memory.
454 * You need not call an explicit deinit call. Simply make
455 * sure it is not on a list with timer_del.
457 void timer_init_full(QEMUTimer *ts,
458 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
459 int scale, int attributes,
460 QEMUTimerCB *cb, void *opaque);
463 * timer_init:
464 * @ts: the timer to be initialised
465 * @type: the clock to associate with the timer
466 * @scale: the scale value for the timer
467 * @cb: the callback to call when the timer expires
468 * @opaque: the opaque pointer to pass to the callback
470 * Initialize a timer with the given scale on the default timer list
471 * associated with the clock.
472 * See timer_init_full for details.
474 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
475 QEMUTimerCB *cb, void *opaque)
477 timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
481 * timer_init_ns:
482 * @ts: the timer to be initialised
483 * @type: the clock to associate with the timer
484 * @cb: the callback to call when the timer expires
485 * @opaque: the opaque pointer to pass to the callback
487 * Initialize a timer with nanosecond scale on the default timer list
488 * associated with the clock.
489 * See timer_init_full for details.
491 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
492 QEMUTimerCB *cb, void *opaque)
494 timer_init(ts, type, SCALE_NS, cb, opaque);
498 * timer_init_us:
499 * @ts: the timer to be initialised
500 * @type: the clock to associate with the timer
501 * @cb: the callback to call when the timer expires
502 * @opaque: the opaque pointer to pass to the callback
504 * Initialize a timer with microsecond scale on the default timer list
505 * associated with the clock.
506 * See timer_init_full for details.
508 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
509 QEMUTimerCB *cb, void *opaque)
511 timer_init(ts, type, SCALE_US, cb, opaque);
515 * timer_init_ms:
516 * @ts: the timer to be initialised
517 * @type: the clock to associate with the timer
518 * @cb: the callback to call when the timer expires
519 * @opaque: the opaque pointer to pass to the callback
521 * Initialize a timer with millisecond scale on the default timer list
522 * associated with the clock.
523 * See timer_init_full for details.
525 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
526 QEMUTimerCB *cb, void *opaque)
528 timer_init(ts, type, SCALE_MS, cb, opaque);
532 * timer_new_full:
533 * @timer_list_group: (optional) the timer list group to attach the timer to
534 * @type: the clock type to use
535 * @scale: the scale value for the timer
536 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
537 * @cb: the callback to be called when the timer expires
538 * @opaque: the opaque pointer to be passed to the callback
540 * Create a new timer with the given scale and attributes,
541 * and associate it with timer list for given clock @type in @timer_list_group
542 * (or default timer list group, if NULL).
543 * The memory is allocated by the function.
545 * This is not the preferred interface unless you know you
546 * are going to call timer_free. Use timer_init or timer_init_full instead.
548 * The default timer list has one special feature: in icount mode,
549 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
550 * not true of other timer lists, which are typically associated
551 * with an AioContext---each of them runs its timer callbacks in its own
552 * AioContext thread.
554 * Returns: a pointer to the timer
556 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
557 QEMUClockType type,
558 int scale, int attributes,
559 QEMUTimerCB *cb, void *opaque)
561 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
562 timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
563 return ts;
567 * timer_new:
568 * @type: the clock type to use
569 * @scale: the scale value for the timer
570 * @cb: the callback to be called when the timer expires
571 * @opaque: the opaque pointer to be passed to the callback
573 * Create a new timer with the given scale,
574 * and associate it with the default timer list for the clock type @type.
575 * See timer_new_full for details.
577 * Returns: a pointer to the timer
579 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
580 QEMUTimerCB *cb, void *opaque)
582 return timer_new_full(NULL, type, scale, 0, cb, opaque);
586 * timer_new_ns:
587 * @type: the clock type to associate with the timer
588 * @cb: the callback to call when the timer expires
589 * @opaque: the opaque pointer to pass to the callback
591 * Create a new timer with nanosecond scale on the default timer list
592 * associated with the clock.
593 * See timer_new_full for details.
595 * Returns: a pointer to the newly created timer
597 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
598 void *opaque)
600 return timer_new(type, SCALE_NS, cb, opaque);
604 * timer_new_us:
605 * @type: the clock type to associate with the timer
606 * @cb: the callback to call when the timer expires
607 * @opaque: the opaque pointer to pass to the callback
609 * Create a new timer with microsecond scale on the default timer list
610 * associated with the clock.
611 * See timer_new_full for details.
613 * Returns: a pointer to the newly created timer
615 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
616 void *opaque)
618 return timer_new(type, SCALE_US, cb, opaque);
622 * timer_new_ms:
623 * @type: the clock type to associate with the timer
624 * @cb: the callback to call when the timer expires
625 * @opaque: the opaque pointer to pass to the callback
627 * Create a new timer with millisecond scale on the default timer list
628 * associated with the clock.
629 * See timer_new_full for details.
631 * Returns: a pointer to the newly created timer
633 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
634 void *opaque)
636 return timer_new(type, SCALE_MS, cb, opaque);
640 * timer_deinit:
641 * @ts: the timer to be de-initialised
643 * Deassociate the timer from any timerlist. You should
644 * call timer_del before. After this call, any further
645 * timer_del call cannot cause dangling pointer accesses
646 * even if the previously used timerlist is freed.
648 void timer_deinit(QEMUTimer *ts);
651 * timer_free:
652 * @ts: the timer
654 * Free a timer (it must not be on the active list)
656 static inline void timer_free(QEMUTimer *ts)
658 g_free(ts);
662 * timer_del:
663 * @ts: the timer
665 * Delete a timer from the active list.
667 * This function is thread-safe but the timer and its timer list must not be
668 * freed while this function is running.
670 void timer_del(QEMUTimer *ts);
673 * timer_mod_ns:
674 * @ts: the timer
675 * @expire_time: the expiry time in nanoseconds
677 * Modify a timer to expire at @expire_time
679 * This function is thread-safe but the timer and its timer list must not be
680 * freed while this function is running.
682 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
685 * timer_mod_anticipate_ns:
686 * @ts: the timer
687 * @expire_time: the expiry time in nanoseconds
689 * Modify a timer to expire at @expire_time or the current time,
690 * whichever comes earlier.
692 * This function is thread-safe but the timer and its timer list must not be
693 * freed while this function is running.
695 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
698 * timer_mod:
699 * @ts: the timer
700 * @expire_time: the expire time in the units associated with the timer
702 * Modify a timer to expiry at @expire_time, taking into
703 * account the scale associated with the timer.
705 * This function is thread-safe but the timer and its timer list must not be
706 * freed while this function is running.
708 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
711 * timer_mod_anticipate:
712 * @ts: the timer
713 * @expire_time: the expiry time in nanoseconds
715 * Modify a timer to expire at @expire_time or the current time, whichever
716 * comes earlier, taking into account the scale associated with the timer.
718 * This function is thread-safe but the timer and its timer list must not be
719 * freed while this function is running.
721 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
724 * timer_pending:
725 * @ts: the timer
727 * Determines whether a timer is pending (i.e. is on the
728 * active list of timers, whether or not it has not yet expired).
730 * Returns: true if the timer is pending
732 bool timer_pending(QEMUTimer *ts);
735 * timer_expired:
736 * @ts: the timer
737 * @current_time: the current time
739 * Determines whether a timer has expired.
741 * Returns: true if the timer has expired
743 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
746 * timer_expire_time_ns:
747 * @ts: the timer
749 * Determine the expiry time of a timer
751 * Returns: the expiry time in nanoseconds
753 uint64_t timer_expire_time_ns(QEMUTimer *ts);
756 * timer_get:
757 * @f: the file
758 * @ts: the timer
760 * Read a timer @ts from a file @f
762 void timer_get(QEMUFile *f, QEMUTimer *ts);
765 * timer_put:
766 * @f: the file
767 * @ts: the timer
769 void timer_put(QEMUFile *f, QEMUTimer *ts);
772 * General utility functions
776 * qemu_timeout_ns_to_ms:
777 * @ns: nanosecond timeout value
779 * Convert a nanosecond timeout value (or -1) to
780 * a millisecond value (or -1), always rounding up.
782 * Returns: millisecond timeout value
784 int qemu_timeout_ns_to_ms(int64_t ns);
787 * qemu_poll_ns:
788 * @fds: Array of file descriptors
789 * @nfds: number of file descriptors
790 * @timeout: timeout in nanoseconds
792 * Perform a poll like g_poll but with a timeout in nanoseconds.
793 * See g_poll documentation for further details.
795 * Returns: number of fds ready
797 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
800 * qemu_soonest_timeout:
801 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
802 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
804 * Calculates the soonest of two timeout values. -1 means infinite, which
805 * is later than any other value.
807 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
809 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
811 /* we can abuse the fact that -1 (which means infinite) is a maximal
812 * value when cast to unsigned. As this is disgusting, it's kept in
813 * one inline function.
815 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
819 * initclocks:
821 * Initialise the clock & timer infrastructure
823 void init_clocks(QEMUTimerListNotifyCB *notify_cb);
825 int64_t cpu_get_ticks(void);
826 /* Caller must hold BQL */
827 void cpu_enable_ticks(void);
828 /* Caller must hold BQL */
829 void cpu_disable_ticks(void);
831 static inline int64_t get_max_clock_jump(void)
833 /* This should be small enough to prevent excessive interrupts from being
834 * generated by the RTC on clock jumps, but large enough to avoid frequent
835 * unnecessary resets in idle VMs.
837 return 60 * NANOSECONDS_PER_SECOND;
841 * Low level clock functions
844 /* get host real time in nanosecond */
845 static inline int64_t get_clock_realtime(void)
847 struct timeval tv;
849 gettimeofday(&tv, NULL);
850 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
853 /* Warning: don't insert tracepoints into these functions, they are
854 also used by simpletrace backend and tracepoints would cause
855 an infinite recursion! */
856 #ifdef _WIN32
857 extern int64_t clock_freq;
859 static inline int64_t get_clock(void)
861 LARGE_INTEGER ti;
862 QueryPerformanceCounter(&ti);
863 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
866 #else
868 extern int use_rt_clock;
870 static inline int64_t get_clock(void)
872 #ifdef CLOCK_MONOTONIC
873 if (use_rt_clock) {
874 struct timespec ts;
875 clock_gettime(CLOCK_MONOTONIC, &ts);
876 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
877 } else
878 #endif
880 /* XXX: using gettimeofday leads to problems if the date
881 changes, so it should be avoided. */
882 return get_clock_realtime();
885 #endif
887 /* icount */
888 int64_t cpu_get_icount_raw(void);
889 int64_t cpu_get_icount(void);
890 int64_t cpu_get_clock(void);
891 int64_t cpu_icount_to_ns(int64_t icount);
892 void cpu_update_icount(CPUState *cpu);
894 /*******************************************/
895 /* host CPU ticks (if available) */
897 #if defined(_ARCH_PPC)
899 static inline int64_t cpu_get_host_ticks(void)
901 int64_t retval;
902 #ifdef _ARCH_PPC64
903 /* This reads timebase in one 64bit go and includes Cell workaround from:
904 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
906 __asm__ __volatile__ ("mftb %0\n\t"
907 "cmpwi %0,0\n\t"
908 "beq- $-8"
909 : "=r" (retval));
910 #else
911 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
912 unsigned long junk;
913 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
914 "mfspr %L0,268\n\t" /* mftb */
915 "mfspr %0,269\n\t" /* mftbu */
916 "cmpw %0,%1\n\t"
917 "bne $-16"
918 : "=r" (retval), "=r" (junk));
919 #endif
920 return retval;
923 #elif defined(__i386__)
925 static inline int64_t cpu_get_host_ticks(void)
927 int64_t val;
928 asm volatile ("rdtsc" : "=A" (val));
929 return val;
932 #elif defined(__x86_64__)
934 static inline int64_t cpu_get_host_ticks(void)
936 uint32_t low,high;
937 int64_t val;
938 asm volatile("rdtsc" : "=a" (low), "=d" (high));
939 val = high;
940 val <<= 32;
941 val |= low;
942 return val;
945 #elif defined(__hppa__)
947 static inline int64_t cpu_get_host_ticks(void)
949 int val;
950 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
951 return val;
954 #elif defined(__s390__)
956 static inline int64_t cpu_get_host_ticks(void)
958 int64_t val;
959 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
960 return val;
963 #elif defined(__sparc__)
965 static inline int64_t cpu_get_host_ticks (void)
967 #if defined(_LP64)
968 uint64_t rval;
969 asm volatile("rd %%tick,%0" : "=r"(rval));
970 return rval;
971 #else
972 /* We need an %o or %g register for this. For recent enough gcc
973 there is an "h" constraint for that. Don't bother with that. */
974 union {
975 uint64_t i64;
976 struct {
977 uint32_t high;
978 uint32_t low;
979 } i32;
980 } rval;
981 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
982 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
983 return rval.i64;
984 #endif
987 #elif defined(__mips__) && \
988 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
990 * binutils wants to use rdhwr only on mips32r2
991 * but as linux kernel emulate it, it's fine
992 * to use it.
995 #define MIPS_RDHWR(rd, value) { \
996 __asm__ __volatile__ (".set push\n\t" \
997 ".set mips32r2\n\t" \
998 "rdhwr %0, "rd"\n\t" \
999 ".set pop" \
1000 : "=r" (value)); \
1003 static inline int64_t cpu_get_host_ticks(void)
1005 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
1006 uint32_t count;
1007 static uint32_t cyc_per_count = 0;
1009 if (!cyc_per_count) {
1010 MIPS_RDHWR("$3", cyc_per_count);
1013 MIPS_RDHWR("$2", count);
1014 return (int64_t)(count * cyc_per_count);
1017 #elif defined(__alpha__)
1019 static inline int64_t cpu_get_host_ticks(void)
1021 uint64_t cc;
1022 uint32_t cur, ofs;
1024 asm volatile("rpcc %0" : "=r"(cc));
1025 cur = cc;
1026 ofs = cc >> 32;
1027 return cur - ofs;
1030 #else
1031 /* The host CPU doesn't have an easily accessible cycle counter.
1032 Just return a monotonically increasing value. This will be
1033 totally wrong, but hopefully better than nothing. */
1034 static inline int64_t cpu_get_host_ticks(void)
1036 return get_clock();
1038 #endif
1040 #ifdef CONFIG_PROFILER
1041 static inline int64_t profile_getclock(void)
1043 return get_clock();
1046 extern int64_t dev_time;
1047 #endif
1049 #endif