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