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