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