Merge remote-tracking branch 'remotes/kraxel/tags/seabios-1.12-20181120-pull-request...
[qemu/ar7.git] / include / qemu / timer.h
bloba86330c987be616d11c1a7ed2120f68a96ee201a
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
4 #include "qemu-common.h"
5 #include "qemu/bitops.h"
6 #include "qemu/notify.h"
7 #include "qemu/host-utils.h"
9 #define NANOSECONDS_PER_SECOND 1000000000LL
11 /* timers */
13 #define SCALE_MS 1000000
14 #define SCALE_US 1000
15 #define SCALE_NS 1
17 /**
18 * QEMUClockType:
20 * The following clock types are available:
22 * @QEMU_CLOCK_REALTIME: Real time clock
24 * The real time clock should be used only for stuff which does not
25 * change the virtual machine state, as it runs even if the virtual
26 * machine is stopped.
28 * @QEMU_CLOCK_VIRTUAL: virtual clock
30 * The virtual clock only runs during the emulation. It stops
31 * when the virtual machine is stopped.
33 * @QEMU_CLOCK_HOST: host clock
35 * The host clock should be used for device models that emulate accurate
36 * real time sources. It will continue to run when the virtual machine
37 * is suspended, and it will reflect system time changes the host may
38 * undergo (e.g. due to NTP).
40 * @QEMU_CLOCK_VIRTUAL_RT: realtime clock used for icount warp
42 * Outside icount mode, this clock is the same as @QEMU_CLOCK_VIRTUAL.
43 * In icount mode, this clock counts nanoseconds while the virtual
44 * machine is running. It is used to increase @QEMU_CLOCK_VIRTUAL
45 * while the CPUs are sleeping and thus not executing instructions.
48 typedef enum {
49 QEMU_CLOCK_REALTIME = 0,
50 QEMU_CLOCK_VIRTUAL = 1,
51 QEMU_CLOCK_HOST = 2,
52 QEMU_CLOCK_VIRTUAL_RT = 3,
53 QEMU_CLOCK_MAX
54 } QEMUClockType;
56 /**
57 * QEMU Timer attributes:
59 * An individual timer may be given one or multiple attributes when initialized.
60 * Each attribute corresponds to one bit. Attributes modify the processing
61 * of timers when they fire.
63 * The following attributes are available:
65 * QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem
67 * Timers with this attribute do not recorded in rr mode, therefore it could be
68 * used for the subsystems that operate outside the guest core. Applicable only
69 * with virtual clock type.
72 #define QEMU_TIMER_ATTR_EXTERNAL BIT(0)
74 typedef struct QEMUTimerList QEMUTimerList;
76 struct QEMUTimerListGroup {
77 QEMUTimerList *tl[QEMU_CLOCK_MAX];
80 typedef void QEMUTimerCB(void *opaque);
81 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
83 struct QEMUTimer {
84 int64_t expire_time; /* in nanoseconds */
85 QEMUTimerList *timer_list;
86 QEMUTimerCB *cb;
87 void *opaque;
88 QEMUTimer *next;
89 int attributes;
90 int scale;
93 extern QEMUTimerListGroup main_loop_tlg;
96 * qemu_clock_get_ns;
97 * @type: the clock type
99 * Get the nanosecond value of a clock with
100 * type @type
102 * Returns: the clock value in nanoseconds
104 int64_t qemu_clock_get_ns(QEMUClockType type);
107 * qemu_clock_get_ms;
108 * @type: the clock type
110 * Get the millisecond value of a clock with
111 * type @type
113 * Returns: the clock value in milliseconds
115 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
117 return qemu_clock_get_ns(type) / SCALE_MS;
121 * qemu_clock_get_us;
122 * @type: the clock type
124 * Get the microsecond value of a clock with
125 * type @type
127 * Returns: the clock value in microseconds
129 static inline int64_t qemu_clock_get_us(QEMUClockType type)
131 return qemu_clock_get_ns(type) / SCALE_US;
135 * qemu_clock_has_timers:
136 * @type: the clock type
138 * Determines whether a clock's default timer list
139 * has timers attached
141 * Note that this function should not be used when other threads also access
142 * the timer list. The return value may be outdated by the time it is acted
143 * upon.
145 * Returns: true if the clock's default timer list
146 * has timers attached
148 bool qemu_clock_has_timers(QEMUClockType type);
151 * qemu_clock_expired:
152 * @type: the clock type
154 * Determines whether a clock's default timer list
155 * has an expired timer.
157 * Returns: true if the clock's default timer list has
158 * an expired timer
160 bool qemu_clock_expired(QEMUClockType type);
163 * qemu_clock_use_for_deadline:
164 * @type: the clock type
166 * Determine whether a clock should be used for deadline
167 * calculations. Some clocks, for instance vm_clock with
168 * use_icount set, do not count in nanoseconds. Such clocks
169 * are not used for deadline calculations, and are presumed
170 * to interrupt any poll using qemu_notify/aio_notify
171 * etc.
173 * Returns: true if the clock runs in nanoseconds and
174 * should be used for a deadline.
176 bool qemu_clock_use_for_deadline(QEMUClockType type);
179 * qemu_clock_deadline_ns_all:
180 * @type: the clock type
182 * Calculate the deadline across all timer lists associated
183 * with a clock (as opposed to just the default one)
184 * in nanoseconds, or -1 if no timer is set to expire.
186 * Returns: time until expiry in nanoseconds or -1
188 int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
191 * qemu_clock_get_main_loop_timerlist:
192 * @type: the clock type
194 * Return the default timer list associated with a clock.
196 * Returns: the default timer list
198 QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
201 * qemu_clock_nofify:
202 * @type: the clock type
204 * Call the notifier callback connected with the default timer
205 * list linked to the clock, or qemu_notify() if none.
207 void qemu_clock_notify(QEMUClockType type);
210 * qemu_clock_enable:
211 * @type: the clock type
212 * @enabled: true to enable, false to disable
214 * Enable or disable a clock
215 * Disabling the clock will wait for related timerlists to stop
216 * executing qemu_run_timers. Thus, this functions should not
217 * be used from the callback of a timer that is based on @clock.
218 * Doing so would cause a deadlock.
220 * Caller should hold BQL.
222 void qemu_clock_enable(QEMUClockType type, bool enabled);
225 * qemu_start_warp_timer:
227 * Starts a timer for virtual clock update
229 void qemu_start_warp_timer(void);
232 * qemu_clock_register_reset_notifier:
233 * @type: the clock type
234 * @notifier: the notifier function
236 * Register a notifier function to call when the clock
237 * concerned is reset.
239 void qemu_clock_register_reset_notifier(QEMUClockType type,
240 Notifier *notifier);
243 * qemu_clock_unregister_reset_notifier:
244 * @type: the clock type
245 * @notifier: the notifier function
247 * Unregister a notifier function to call when the clock
248 * concerned is reset.
250 void qemu_clock_unregister_reset_notifier(QEMUClockType type,
251 Notifier *notifier);
254 * qemu_clock_run_timers:
255 * @type: clock on which to operate
257 * Run all the timers associated with the default timer list
258 * of a clock.
260 * Returns: true if any timer ran.
262 bool qemu_clock_run_timers(QEMUClockType type);
265 * qemu_clock_run_all_timers:
267 * Run all the timers associated with the default timer list
268 * of every clock.
270 * Returns: true if any timer ran.
272 bool qemu_clock_run_all_timers(void);
275 * qemu_clock_get_last:
277 * Returns last clock query time.
279 uint64_t qemu_clock_get_last(QEMUClockType type);
281 * qemu_clock_set_last:
283 * Sets last clock query time.
285 void qemu_clock_set_last(QEMUClockType type, uint64_t last);
289 * QEMUTimerList
293 * timerlist_new:
294 * @type: the clock type to associate with the timerlist
295 * @cb: the callback to call on notification
296 * @opaque: the opaque pointer to pass to the callback
298 * Create a new timerlist associated with the clock of
299 * type @type.
301 * Returns: a pointer to the QEMUTimerList created
303 QEMUTimerList *timerlist_new(QEMUClockType type,
304 QEMUTimerListNotifyCB *cb, void *opaque);
307 * timerlist_free:
308 * @timer_list: the timer list to free
310 * Frees a timer_list. It must have no active timers.
312 void timerlist_free(QEMUTimerList *timer_list);
315 * timerlist_has_timers:
316 * @timer_list: the timer list to operate on
318 * Determine whether a timer list has active timers
320 * Note that this function should not be used when other threads also access
321 * the timer list. The return value may be outdated by the time it is acted
322 * upon.
324 * Returns: true if the timer list has timers.
326 bool timerlist_has_timers(QEMUTimerList *timer_list);
329 * timerlist_expired:
330 * @timer_list: the timer list to operate on
332 * Determine whether a timer list has any timers which
333 * are expired.
335 * Returns: true if the timer list has timers which
336 * have expired.
338 bool timerlist_expired(QEMUTimerList *timer_list);
341 * timerlist_deadline_ns:
342 * @timer_list: the timer list to operate on
344 * Determine the deadline for a timer_list, i.e.
345 * the number of nanoseconds until the first timer
346 * expires. Return -1 if there are no timers.
348 * Returns: the number of nanoseconds until the earliest
349 * timer expires -1 if none
351 int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);
354 * timerlist_get_clock:
355 * @timer_list: the timer list to operate on
357 * Determine the clock type associated with a timer list.
359 * Returns: the clock type associated with the
360 * timer list.
362 QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
365 * timerlist_run_timers:
366 * @timer_list: the timer list to use
368 * Call all expired timers associated with the timer list.
370 * Returns: true if any timer expired
372 bool timerlist_run_timers(QEMUTimerList *timer_list);
375 * timerlist_notify:
376 * @timer_list: the timer list to use
378 * call the notifier callback associated with the timer list.
380 void timerlist_notify(QEMUTimerList *timer_list);
383 * QEMUTimerListGroup
387 * timerlistgroup_init:
388 * @tlg: the timer list group
389 * @cb: the callback to call when a notify is required
390 * @opaque: the opaque pointer to be passed to the callback.
392 * Initialise a timer list group. This must already be
393 * allocated in memory and zeroed. The notifier callback is
394 * called whenever a clock in the timer list group is
395 * reenabled or whenever a timer associated with any timer
396 * list is modified. If @cb is specified as null, qemu_notify()
397 * is used instead.
399 void timerlistgroup_init(QEMUTimerListGroup *tlg,
400 QEMUTimerListNotifyCB *cb, void *opaque);
403 * timerlistgroup_deinit:
404 * @tlg: the timer list group
406 * Deinitialise a timer list group. This must already be
407 * initialised. Note the memory is not freed.
409 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
412 * timerlistgroup_run_timers:
413 * @tlg: the timer list group
415 * Run the timers associated with a timer list group.
416 * This will run timers on multiple clocks.
418 * Returns: true if any timer callback ran
420 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
423 * timerlistgroup_deadline_ns:
424 * @tlg: the timer list group
426 * Determine the deadline of the soonest timer to
427 * expire associated with any timer list linked to
428 * the timer list group. Only clocks suitable for
429 * deadline calculation are included.
431 * Returns: the deadline in nanoseconds or -1 if no
432 * timers are to expire.
434 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
437 * QEMUTimer
441 * timer_init_full:
442 * @ts: the timer to be initialised
443 * @timer_list_group: (optional) the timer list group to attach the timer to
444 * @type: the clock type to use
445 * @scale: the scale value for the timer
446 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
447 * @cb: the callback to be called when the timer expires
448 * @opaque: the opaque pointer to be passed to the callback
450 * Initialise a timer with the given scale and attributes,
451 * and associate it with timer list for given clock @type in @timer_list_group
452 * (or default timer list group, if NULL).
453 * The caller is responsible for allocating the memory.
455 * You need not call an explicit deinit call. Simply make
456 * sure it is not on a list with timer_del.
458 void timer_init_full(QEMUTimer *ts,
459 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
460 int scale, int attributes,
461 QEMUTimerCB *cb, void *opaque);
464 * timer_init:
465 * @ts: the timer to be initialised
466 * @type: the clock to associate with the timer
467 * @scale: the scale value for the timer
468 * @cb: the callback to call when the timer expires
469 * @opaque: the opaque pointer to pass to the callback
471 * Initialize a timer with the given scale on the default timer list
472 * associated with the clock.
473 * See timer_init_full for details.
475 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
476 QEMUTimerCB *cb, void *opaque)
478 timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
482 * timer_init_ns:
483 * @ts: the timer to be initialised
484 * @type: the clock to associate with the timer
485 * @cb: the callback to call when the timer expires
486 * @opaque: the opaque pointer to pass to the callback
488 * Initialize a timer with nanosecond scale on the default timer list
489 * associated with the clock.
490 * See timer_init_full for details.
492 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
493 QEMUTimerCB *cb, void *opaque)
495 timer_init(ts, type, SCALE_NS, cb, opaque);
499 * timer_init_us:
500 * @ts: the timer to be initialised
501 * @type: the clock to associate with the timer
502 * @cb: the callback to call when the timer expires
503 * @opaque: the opaque pointer to pass to the callback
505 * Initialize a timer with microsecond scale on the default timer list
506 * associated with the clock.
507 * See timer_init_full for details.
509 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
510 QEMUTimerCB *cb, void *opaque)
512 timer_init(ts, type, SCALE_US, cb, opaque);
516 * timer_init_ms:
517 * @ts: the timer to be initialised
518 * @type: the clock to associate with the timer
519 * @cb: the callback to call when the timer expires
520 * @opaque: the opaque pointer to pass to the callback
522 * Initialize a timer with millisecond scale on the default timer list
523 * associated with the clock.
524 * See timer_init_full for details.
526 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
527 QEMUTimerCB *cb, void *opaque)
529 timer_init(ts, type, SCALE_MS, cb, opaque);
533 * timer_new_full:
534 * @timer_list_group: (optional) the timer list group to attach the timer to
535 * @type: the clock type to use
536 * @scale: the scale value for the timer
537 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
538 * @cb: the callback to be called when the timer expires
539 * @opaque: the opaque pointer to be passed to the callback
541 * Create a new timer with the given scale and attributes,
542 * and associate it with timer list for given clock @type in @timer_list_group
543 * (or default timer list group, if NULL).
544 * The memory is allocated by the function.
546 * This is not the preferred interface unless you know you
547 * are going to call timer_free. Use timer_init or timer_init_full instead.
549 * The default timer list has one special feature: in icount mode,
550 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
551 * not true of other timer lists, which are typically associated
552 * with an AioContext---each of them runs its timer callbacks in its own
553 * AioContext thread.
555 * Returns: a pointer to the timer
557 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
558 QEMUClockType type,
559 int scale, int attributes,
560 QEMUTimerCB *cb, void *opaque)
562 QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
563 timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
564 return ts;
568 * timer_new:
569 * @type: the clock type to use
570 * @scale: the scale value for the timer
571 * @cb: the callback to be called when the timer expires
572 * @opaque: the opaque pointer to be passed to the callback
574 * Create a new timer with the given scale,
575 * and associate it with the default timer list for the clock type @type.
576 * See timer_new_full for details.
578 * Returns: a pointer to the timer
580 static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
581 QEMUTimerCB *cb, void *opaque)
583 return timer_new_full(NULL, type, scale, 0, cb, opaque);
587 * timer_new_ns:
588 * @type: the clock type to associate with the timer
589 * @cb: the callback to call when the timer expires
590 * @opaque: the opaque pointer to pass to the callback
592 * Create a new timer with nanosecond scale on the default timer list
593 * associated with the clock.
594 * See timer_new_full for details.
596 * Returns: a pointer to the newly created timer
598 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
599 void *opaque)
601 return timer_new(type, SCALE_NS, cb, opaque);
605 * timer_new_us:
606 * @type: the clock type to associate with the timer
607 * @cb: the callback to call when the timer expires
608 * @opaque: the opaque pointer to pass to the callback
610 * Create a new timer with microsecond scale on the default timer list
611 * associated with the clock.
612 * See timer_new_full for details.
614 * Returns: a pointer to the newly created timer
616 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
617 void *opaque)
619 return timer_new(type, SCALE_US, cb, opaque);
623 * timer_new_ms:
624 * @type: the clock type to associate with the timer
625 * @cb: the callback to call when the timer expires
626 * @opaque: the opaque pointer to pass to the callback
628 * Create a new timer with millisecond scale on the default timer list
629 * associated with the clock.
630 * See timer_new_full for details.
632 * Returns: a pointer to the newly created timer
634 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
635 void *opaque)
637 return timer_new(type, SCALE_MS, cb, opaque);
641 * timer_deinit:
642 * @ts: the timer to be de-initialised
644 * Deassociate the timer from any timerlist. You should
645 * call timer_del before. After this call, any further
646 * timer_del call cannot cause dangling pointer accesses
647 * even if the previously used timerlist is freed.
649 void timer_deinit(QEMUTimer *ts);
652 * timer_free:
653 * @ts: the timer
655 * Free a timer (it must not be on the active list)
657 static inline void timer_free(QEMUTimer *ts)
659 g_free(ts);
663 * timer_del:
664 * @ts: the timer
666 * Delete a timer from the active list.
668 * This function is thread-safe but the timer and its timer list must not be
669 * freed while this function is running.
671 void timer_del(QEMUTimer *ts);
674 * timer_mod_ns:
675 * @ts: the timer
676 * @expire_time: the expiry time in nanoseconds
678 * Modify a timer to expire at @expire_time
680 * This function is thread-safe but the timer and its timer list must not be
681 * freed while this function is running.
683 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
686 * timer_mod_anticipate_ns:
687 * @ts: the timer
688 * @expire_time: the expiry time in nanoseconds
690 * Modify a timer to expire at @expire_time or the current time,
691 * whichever comes earlier.
693 * This function is thread-safe but the timer and its timer list must not be
694 * freed while this function is running.
696 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
699 * timer_mod:
700 * @ts: the timer
701 * @expire_time: the expire time in the units associated with the timer
703 * Modify a timer to expiry at @expire_time, taking into
704 * account the scale associated with the timer.
706 * This function is thread-safe but the timer and its timer list must not be
707 * freed while this function is running.
709 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
712 * timer_mod_anticipate:
713 * @ts: the timer
714 * @expire_time: the expiry time in nanoseconds
716 * Modify a timer to expire at @expire_time or the current time, whichever
717 * comes earlier, taking into account the scale associated with the timer.
719 * This function is thread-safe but the timer and its timer list must not be
720 * freed while this function is running.
722 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
725 * timer_pending:
726 * @ts: the timer
728 * Determines whether a timer is pending (i.e. is on the
729 * active list of timers, whether or not it has not yet expired).
731 * Returns: true if the timer is pending
733 bool timer_pending(QEMUTimer *ts);
736 * timer_expired:
737 * @ts: the timer
738 * @current_time: the current time
740 * Determines whether a timer has expired.
742 * Returns: true if the timer has expired
744 bool timer_expired(QEMUTimer *timer_head, int64_t current_time);
747 * timer_expire_time_ns:
748 * @ts: the timer
750 * Determine the expiry time of a timer
752 * Returns: the expiry time in nanoseconds
754 uint64_t timer_expire_time_ns(QEMUTimer *ts);
757 * timer_get:
758 * @f: the file
759 * @ts: the timer
761 * Read a timer @ts from a file @f
763 void timer_get(QEMUFile *f, QEMUTimer *ts);
766 * timer_put:
767 * @f: the file
768 * @ts: the timer
770 void timer_put(QEMUFile *f, QEMUTimer *ts);
773 * General utility functions
777 * qemu_timeout_ns_to_ms:
778 * @ns: nanosecond timeout value
780 * Convert a nanosecond timeout value (or -1) to
781 * a millisecond value (or -1), always rounding up.
783 * Returns: millisecond timeout value
785 int qemu_timeout_ns_to_ms(int64_t ns);
788 * qemu_poll_ns:
789 * @fds: Array of file descriptors
790 * @nfds: number of file descriptors
791 * @timeout: timeout in nanoseconds
793 * Perform a poll like g_poll but with a timeout in nanoseconds.
794 * See g_poll documentation for further details.
796 * Returns: number of fds ready
798 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
801 * qemu_soonest_timeout:
802 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
803 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
805 * Calculates the soonest of two timeout values. -1 means infinite, which
806 * is later than any other value.
808 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
810 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
812 /* we can abuse the fact that -1 (which means infinite) is a maximal
813 * value when cast to unsigned. As this is disgusting, it's kept in
814 * one inline function.
816 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
820 * initclocks:
822 * Initialise the clock & timer infrastructure
824 void init_clocks(QEMUTimerListNotifyCB *notify_cb);
826 int64_t cpu_get_ticks(void);
827 /* Caller must hold BQL */
828 void cpu_enable_ticks(void);
829 /* Caller must hold BQL */
830 void cpu_disable_ticks(void);
832 static inline int64_t get_max_clock_jump(void)
834 /* This should be small enough to prevent excessive interrupts from being
835 * generated by the RTC on clock jumps, but large enough to avoid frequent
836 * unnecessary resets in idle VMs.
838 return 60 * NANOSECONDS_PER_SECOND;
842 * Low level clock functions
845 /* get host real time in nanosecond */
846 static inline int64_t get_clock_realtime(void)
848 struct timeval tv;
850 gettimeofday(&tv, NULL);
851 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
854 /* Warning: don't insert tracepoints into these functions, they are
855 also used by simpletrace backend and tracepoints would cause
856 an infinite recursion! */
857 #ifdef _WIN32
858 extern int64_t clock_freq;
860 static inline int64_t get_clock(void)
862 LARGE_INTEGER ti;
863 QueryPerformanceCounter(&ti);
864 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
867 #else
869 extern int use_rt_clock;
871 static inline int64_t get_clock(void)
873 #ifdef CLOCK_MONOTONIC
874 if (use_rt_clock) {
875 struct timespec ts;
876 clock_gettime(CLOCK_MONOTONIC, &ts);
877 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
878 } else
879 #endif
881 /* XXX: using gettimeofday leads to problems if the date
882 changes, so it should be avoided. */
883 return get_clock_realtime();
886 #endif
888 /* icount */
889 int64_t cpu_get_icount_raw(void);
890 int64_t cpu_get_icount(void);
891 int64_t cpu_get_clock(void);
892 int64_t cpu_icount_to_ns(int64_t icount);
893 void cpu_update_icount(CPUState *cpu);
895 /*******************************************/
896 /* host CPU ticks (if available) */
898 #if defined(_ARCH_PPC)
900 static inline int64_t cpu_get_host_ticks(void)
902 int64_t retval;
903 #ifdef _ARCH_PPC64
904 /* This reads timebase in one 64bit go and includes Cell workaround from:
905 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
907 __asm__ __volatile__ ("mftb %0\n\t"
908 "cmpwi %0,0\n\t"
909 "beq- $-8"
910 : "=r" (retval));
911 #else
912 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
913 unsigned long junk;
914 __asm__ __volatile__ ("mfspr %1,269\n\t" /* mftbu */
915 "mfspr %L0,268\n\t" /* mftb */
916 "mfspr %0,269\n\t" /* mftbu */
917 "cmpw %0,%1\n\t"
918 "bne $-16"
919 : "=r" (retval), "=r" (junk));
920 #endif
921 return retval;
924 #elif defined(__i386__)
926 static inline int64_t cpu_get_host_ticks(void)
928 int64_t val;
929 asm volatile ("rdtsc" : "=A" (val));
930 return val;
933 #elif defined(__x86_64__)
935 static inline int64_t cpu_get_host_ticks(void)
937 uint32_t low,high;
938 int64_t val;
939 asm volatile("rdtsc" : "=a" (low), "=d" (high));
940 val = high;
941 val <<= 32;
942 val |= low;
943 return val;
946 #elif defined(__hppa__)
948 static inline int64_t cpu_get_host_ticks(void)
950 int val;
951 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
952 return val;
955 #elif defined(__s390__)
957 static inline int64_t cpu_get_host_ticks(void)
959 int64_t val;
960 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
961 return val;
964 #elif defined(__sparc__)
966 static inline int64_t cpu_get_host_ticks (void)
968 #if defined(_LP64)
969 uint64_t rval;
970 asm volatile("rd %%tick,%0" : "=r"(rval));
971 return rval;
972 #else
973 /* We need an %o or %g register for this. For recent enough gcc
974 there is an "h" constraint for that. Don't bother with that. */
975 union {
976 uint64_t i64;
977 struct {
978 uint32_t high;
979 uint32_t low;
980 } i32;
981 } rval;
982 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
983 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
984 return rval.i64;
985 #endif
988 #elif defined(__mips__) && \
989 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
991 * binutils wants to use rdhwr only on mips32r2
992 * but as linux kernel emulate it, it's fine
993 * to use it.
996 #define MIPS_RDHWR(rd, value) { \
997 __asm__ __volatile__ (".set push\n\t" \
998 ".set mips32r2\n\t" \
999 "rdhwr %0, "rd"\n\t" \
1000 ".set pop" \
1001 : "=r" (value)); \
1004 static inline int64_t cpu_get_host_ticks(void)
1006 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
1007 uint32_t count;
1008 static uint32_t cyc_per_count = 0;
1010 if (!cyc_per_count) {
1011 MIPS_RDHWR("$3", cyc_per_count);
1014 MIPS_RDHWR("$2", count);
1015 return (int64_t)(count * cyc_per_count);
1018 #elif defined(__alpha__)
1020 static inline int64_t cpu_get_host_ticks(void)
1022 uint64_t cc;
1023 uint32_t cur, ofs;
1025 asm volatile("rpcc %0" : "=r"(cc));
1026 cur = cc;
1027 ofs = cc >> 32;
1028 return cur - ofs;
1031 #else
1032 /* The host CPU doesn't have an easily accessible cycle counter.
1033 Just return a monotonically increasing value. This will be
1034 totally wrong, but hopefully better than nothing. */
1035 static inline int64_t cpu_get_host_ticks(void)
1037 return get_clock();
1039 #endif
1041 #ifdef CONFIG_PROFILER
1042 static inline int64_t profile_getclock(void)
1044 return get_clock();
1047 extern int64_t dev_time;
1048 #endif
1050 #endif