master
h 979 lines 26.1 KB
Raw
1 #ifndef QEMU_TIMER_H
2 #define QEMU_TIMER_H
3
4 #include "qemu/bitops.h"
5 #include "qemu/notify.h"
6 #include "qemu/host-utils.h"
7
8 #define NANOSECONDS_PER_SECOND 1000000000LL
9 #define MICROSECONDS_PER_SECOND 1000000LL
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 /**
57 * QEMU Timer attributes:
58 *
59 * An individual timer may be given one or multiple attributes when initialized.
60 * Each attribute corresponds to one bit. Attributes modify the processing
61 * of timers when they fire.
62 *
63 * The following attributes are available:
64 *
65 * QEMU_TIMER_ATTR_EXTERNAL: drives external subsystem
66 * QEMU_TIMER_ATTR_ALL: mask for all existing attributes
67 *
68 * Timers with this attribute do not recorded in rr mode, therefore it could be
69 * used for the subsystems that operate outside the guest core. Applicable only
70 * with virtual clock type.
71 */
72
73 #define QEMU_TIMER_ATTR_EXTERNAL ((int)BIT(0))
74 #define QEMU_TIMER_ATTR_ALL 0xffffffff
75
76 typedef struct QEMUTimerList QEMUTimerList;
77
78 struct QEMUTimerListGroup {
79 QEMUTimerList *tl[QEMU_CLOCK_MAX];
80 };
81
82 typedef void QEMUTimerCB(void *opaque);
83 typedef void QEMUTimerListNotifyCB(void *opaque, QEMUClockType type);
84
85 struct QEMUTimer {
86 int64_t expire_time; /* in nanoseconds */
87 QEMUTimerList *timer_list;
88 QEMUTimerCB *cb;
89 void *opaque;
90 QEMUTimer *next;
91 int attributes;
92 int scale;
93 };
94
95 extern QEMUTimerListGroup main_loop_tlg;
96
97 /*
98 * qemu_clock_get_ns;
99 * @type: the clock type
100 *
101 * Get the nanosecond value of a clock with
102 * type @type
103 *
104 * Returns: the clock value in nanoseconds
105 */
106 int64_t qemu_clock_get_ns(QEMUClockType type);
107
108 /**
109 * qemu_clock_get_ms;
110 * @type: the clock type
111 *
112 * Get the millisecond value of a clock with
113 * type @type
114 *
115 * Returns: the clock value in milliseconds
116 */
117 static inline int64_t qemu_clock_get_ms(QEMUClockType type)
118 {
119 return qemu_clock_get_ns(type) / SCALE_MS;
120 }
121
122 /**
123 * qemu_clock_get_us;
124 * @type: the clock type
125 *
126 * Get the microsecond value of a clock with
127 * type @type
128 *
129 * Returns: the clock value in microseconds
130 */
131 static inline int64_t qemu_clock_get_us(QEMUClockType type)
132 {
133 return qemu_clock_get_ns(type) / SCALE_US;
134 }
135
136 /**
137 * qemu_clock_has_timers:
138 * @type: the clock type
139 *
140 * Determines whether a clock's default timer list
141 * has timers attached
142 *
143 * Note that this function should not be used when other threads also access
144 * the timer list. The return value may be outdated by the time it is acted
145 * upon.
146 *
147 * Returns: true if the clock's default timer list
148 * has timers attached
149 */
150 bool qemu_clock_has_timers(QEMUClockType type);
151
152 /**
153 * qemu_clock_expired:
154 * @type: the clock type
155 *
156 * Determines whether a clock's default timer list
157 * has an expired timer.
158 *
159 * Returns: true if the clock's default timer list has
160 * an expired timer
161 */
162 bool qemu_clock_expired(QEMUClockType type);
163
164 /**
165 * qemu_clock_use_for_deadline:
166 * @type: the clock type
167 *
168 * Determine whether a clock should be used for deadline
169 * calculations. Some clocks, for instance vm_clock with
170 * icount_enabled() set, do not count in nanoseconds.
171 * Such clocks are not used for deadline calculations, and are presumed
172 * to interrupt any poll using qemu_notify/aio_notify
173 * etc.
174 *
175 * Returns: true if the clock runs in nanoseconds and
176 * should be used for a deadline.
177 */
178 bool qemu_clock_use_for_deadline(QEMUClockType type);
179
180 /**
181 * qemu_clock_deadline_ns_all:
182 * @type: the clock type
183 * @attr_mask: mask for the timer attributes that are included
184 * in deadline calculation
185 *
186 * Calculate the deadline across all timer lists associated
187 * with a clock (as opposed to just the default one)
188 * in nanoseconds, or -1 if no timer is set to expire.
189 *
190 * Returns: time until expiry in nanoseconds or -1
191 */
192 int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask);
193
194 /**
195 * qemu_clock_nofify:
196 * @type: the clock type
197 *
198 * Call the notifier callback connected with the default timer
199 * list linked to the clock, or qemu_notify() if none.
200 */
201 void qemu_clock_notify(QEMUClockType type);
202
203 /**
204 * qemu_clock_enable:
205 * @type: the clock type
206 * @enabled: true to enable, false to disable
207 *
208 * Enable or disable a clock
209 * Disabling the clock will wait for related timerlists to stop
210 * executing qemu_run_timers. Thus, this functions should not
211 * be used from the callback of a timer that is based on @clock.
212 * Doing so would cause a deadlock.
213 *
214 * Caller should hold BQL.
215 */
216 void qemu_clock_enable(QEMUClockType type, bool enabled);
217
218 /**
219 * qemu_clock_run_timers:
220 * @type: clock on which to operate
221 *
222 * Run all the timers associated with the default timer list
223 * of a clock.
224 *
225 * Returns: true if any timer ran.
226 */
227 bool qemu_clock_run_timers(QEMUClockType type);
228
229 /**
230 * qemu_clock_run_all_timers:
231 *
232 * Run all the timers associated with the default timer list
233 * of every clock.
234 *
235 * Returns: true if any timer ran.
236 */
237 bool qemu_clock_run_all_timers(void);
238
239 /**
240 * qemu_clock_advance_virtual_time(): advance the virtual time tick
241 * @target_ns: target time in nanoseconds
242 *
243 * This function is used where the control of the flow of time has
244 * been delegated to outside the clock subsystem (be it qtest, icount
245 * or some other external source). You can ask the clock system to
246 * return @early at the first expired timer.
247 *
248 * Time can only move forward, attempts to reverse time would lead to
249 * an error.
250 *
251 * Returns: new virtual time.
252 */
253 int64_t qemu_clock_advance_virtual_time(int64_t target_ns);
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_run_timers:
322 * @timer_list: the timer list to use
323 *
324 * Call all expired timers associated with the timer list.
325 *
326 * Returns: true if any timer expired
327 */
328 bool timerlist_run_timers(QEMUTimerList *timer_list);
329
330 /**
331 * timerlist_notify:
332 * @timer_list: the timer list to use
333 *
334 * call the notifier callback associated with the timer list.
335 */
336 void timerlist_notify(QEMUTimerList *timer_list);
337
338 /*
339 * QEMUTimerListGroup
340 */
341
342 /**
343 * timerlistgroup_init:
344 * @tlg: the timer list group
345 * @cb: the callback to call when a notify is required
346 * @opaque: the opaque pointer to be passed to the callback.
347 *
348 * Initialise a timer list group. This must already be
349 * allocated in memory and zeroed. The notifier callback is
350 * called whenever a clock in the timer list group is
351 * reenabled or whenever a timer associated with any timer
352 * list is modified. If @cb is specified as null, qemu_notify()
353 * is used instead.
354 */
355 void timerlistgroup_init(QEMUTimerListGroup *tlg,
356 QEMUTimerListNotifyCB *cb, void *opaque);
357
358 /**
359 * timerlistgroup_deinit:
360 * @tlg: the timer list group
361 *
362 * Deinitialise a timer list group. This must already be
363 * initialised. Note the memory is not freed.
364 */
365 void timerlistgroup_deinit(QEMUTimerListGroup *tlg);
366
367 /**
368 * timerlistgroup_run_timers:
369 * @tlg: the timer list group
370 *
371 * Run the timers associated with a timer list group.
372 * This will run timers on multiple clocks.
373 *
374 * Returns: true if any timer callback ran
375 */
376 bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);
377
378 /**
379 * timerlistgroup_deadline_ns:
380 * @tlg: the timer list group
381 *
382 * Determine the deadline of the soonest timer to
383 * expire associated with any timer list linked to
384 * the timer list group. Only clocks suitable for
385 * deadline calculation are included.
386 *
387 * Returns: the deadline in nanoseconds or -1 if no
388 * timers are to expire.
389 */
390 int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);
391
392 /*
393 * QEMUTimer
394 */
395
396 /**
397 * timer_init_full:
398 * @ts: the timer to be initialised
399 * @timer_list_group: (optional) the timer list group to attach the timer to
400 * @type: the clock type to use
401 * @scale: the scale value for the timer
402 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
403 * @cb: the callback to be called when the timer expires
404 * @opaque: the opaque pointer to be passed to the callback
405 *
406 * Initialise a timer with the given scale and attributes,
407 * and associate it with timer list for given clock @type in @timer_list_group
408 * (or default timer list group, if NULL).
409 * The caller is responsible for allocating the memory.
410 *
411 * You need not call an explicit deinit call. Simply make
412 * sure it is not on a list with timer_del.
413 */
414 void timer_init_full(QEMUTimer *ts,
415 QEMUTimerListGroup *timer_list_group, QEMUClockType type,
416 int scale, int attributes,
417 QEMUTimerCB *cb, void *opaque);
418
419 /**
420 * timer_init:
421 * @ts: the timer to be initialised
422 * @type: the clock to associate with the timer
423 * @scale: the scale value for the timer
424 * @cb: the callback to call when the timer expires
425 * @opaque: the opaque pointer to pass to the callback
426 *
427 * Initialize a timer with the given scale on the default timer list
428 * associated with the clock.
429 * See timer_init_full for details.
430 */
431 static inline void timer_init(QEMUTimer *ts, QEMUClockType type, int scale,
432 QEMUTimerCB *cb, void *opaque)
433 {
434 timer_init_full(ts, NULL, type, scale, 0, cb, opaque);
435 }
436
437 /**
438 * timer_init_ns:
439 * @ts: the timer to be initialised
440 * @type: the clock to associate with the timer
441 * @cb: the callback to call when the timer expires
442 * @opaque: the opaque pointer to pass to the callback
443 *
444 * Initialize a timer with nanosecond scale on the default timer list
445 * associated with the clock.
446 * See timer_init_full for details.
447 */
448 static inline void timer_init_ns(QEMUTimer *ts, QEMUClockType type,
449 QEMUTimerCB *cb, void *opaque)
450 {
451 timer_init(ts, type, SCALE_NS, cb, opaque);
452 }
453
454 /**
455 * timer_init_us:
456 * @ts: the timer to be initialised
457 * @type: the clock to associate with the timer
458 * @cb: the callback to call when the timer expires
459 * @opaque: the opaque pointer to pass to the callback
460 *
461 * Initialize a timer with microsecond scale on the default timer list
462 * associated with the clock.
463 * See timer_init_full for details.
464 */
465 static inline void timer_init_us(QEMUTimer *ts, QEMUClockType type,
466 QEMUTimerCB *cb, void *opaque)
467 {
468 timer_init(ts, type, SCALE_US, cb, opaque);
469 }
470
471 /**
472 * timer_init_ms:
473 * @ts: the timer to be initialised
474 * @type: the clock to associate with the timer
475 * @cb: the callback to call when the timer expires
476 * @opaque: the opaque pointer to pass to the callback
477 *
478 * Initialize a timer with millisecond scale on the default timer list
479 * associated with the clock.
480 * See timer_init_full for details.
481 */
482 static inline void timer_init_ms(QEMUTimer *ts, QEMUClockType type,
483 QEMUTimerCB *cb, void *opaque)
484 {
485 timer_init(ts, type, SCALE_MS, cb, opaque);
486 }
487
488 /**
489 * timer_new_full:
490 * @timer_list_group: (optional) the timer list group to attach the timer to
491 * @type: the clock type to use
492 * @scale: the scale value for the timer
493 * @attributes: 0, or one or more OR'ed QEMU_TIMER_ATTR_<id> values
494 * @cb: the callback to be called when the timer expires
495 * @opaque: the opaque pointer to be passed to the callback
496 *
497 * Create a new timer with the given scale and attributes,
498 * and associate it with timer list for given clock @type in @timer_list_group
499 * (or default timer list group, if NULL).
500 * The memory is allocated by the function.
501 *
502 * This is not the preferred interface unless you know you
503 * are going to call timer_free. Use timer_init or timer_init_full instead.
504 *
505 * The default timer list has one special feature: in icount mode,
506 * %QEMU_CLOCK_VIRTUAL timers are run in the vCPU thread. This is
507 * not true of other timer lists, which are typically associated
508 * with an AioContext---each of them runs its timer callbacks in its own
509 * AioContext thread.
510 *
511 * The timer returned must be freed using timer_free().
512 *
513 * Returns: a pointer to the timer
514 */
515 static inline QEMUTimer *timer_new_full(QEMUTimerListGroup *timer_list_group,
516 QEMUClockType type,
517 int scale, int attributes,
518 QEMUTimerCB *cb, void *opaque)
519 {
520 QEMUTimer *ts = g_new0(QEMUTimer, 1);
521 timer_init_full(ts, timer_list_group, type, scale, attributes, cb, opaque);
522 return ts;
523 }
524
525 /**
526 * timer_new:
527 * @type: the clock type to use
528 * @scale: the scale value for the timer
529 * @cb: the callback to be called when the timer expires
530 * @opaque: the opaque pointer to be passed to the callback
531 *
532 * Create a new timer with the given scale,
533 * and associate it with the default timer list for the clock type @type.
534 * See timer_new_full for details.
535 *
536 * The timer returned must be freed using timer_free().
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_full(NULL, type, scale, 0, cb, opaque);
544 }
545
546 /**
547 * timer_new_ns:
548 * @type: the clock type to associate with the timer
549 * @cb: 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 * See timer_new_full for details.
555 *
556 * The timer returned must be freed using timer_free().
557 *
558 * Returns: a pointer to the newly created timer
559 */
560 static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
561 void *opaque)
562 {
563 return timer_new(type, SCALE_NS, cb, opaque);
564 }
565
566 /**
567 * timer_new_us:
568 * @type: the clock type to associate with the timer
569 * @cb: the callback to call when the timer expires
570 * @opaque: the opaque pointer to pass to the callback
571 *
572 * Create a new timer with microsecond scale on the default timer list
573 * associated with the clock.
574 * See timer_new_full for details.
575 *
576 * The timer returned must be freed using timer_free().
577 *
578 * Returns: a pointer to the newly created timer
579 */
580 static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
581 void *opaque)
582 {
583 return timer_new(type, SCALE_US, cb, opaque);
584 }
585
586 /**
587 * timer_new_ms:
588 * @type: the clock type to associate with the timer
589 * @cb: the callback to call when the timer expires
590 * @opaque: the opaque pointer to pass to the callback
591 *
592 * Create a new timer with millisecond scale on the default timer list
593 * associated with the clock.
594 * See timer_new_full for details.
595 *
596 * The timer returned must be freed using timer_free().
597 *
598 * Returns: a pointer to the newly created timer
599 */
600 static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
601 void *opaque)
602 {
603 return timer_new(type, SCALE_MS, cb, opaque);
604 }
605
606 /**
607 * timer_deinit:
608 * @ts: the timer to be de-initialised
609 *
610 * Deassociate the timer from any timerlist. You should
611 * call timer_del before. After this call, any further
612 * timer_del call cannot cause dangling pointer accesses
613 * even if the previously used timerlist is freed.
614 */
615 void timer_deinit(QEMUTimer *ts);
616
617 /**
618 * timer_del:
619 * @ts: the timer
620 *
621 * Delete a timer from the active list.
622 *
623 * This function is thread-safe but the timer and its timer list must not be
624 * freed while this function is running.
625 */
626 void timer_del(QEMUTimer *ts);
627
628 /**
629 * timer_free:
630 * @ts: the timer
631 *
632 * Free a timer. This will call timer_del() for you to remove
633 * the timer from the active list if it was still active.
634 */
635 static inline void timer_free(QEMUTimer *ts)
636 {
637 if (ts) {
638 timer_del(ts);
639 g_free(ts);
640 }
641 }
642
643 /**
644 * timer_mod_ns:
645 * @ts: the timer
646 * @expire_time: the expiry time in nanoseconds
647 *
648 * Modify a timer to expire at @expire_time
649 *
650 * This function is thread-safe but the timer and its timer list must not be
651 * freed while this function is running.
652 */
653 void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
654
655 /**
656 * timer_mod_anticipate_ns:
657 * @ts: the timer
658 * @expire_time: the expiry time in nanoseconds
659 *
660 * Modify a timer to expire at @expire_time or the current time,
661 * whichever comes earlier.
662 *
663 * This function is thread-safe but the timer and its timer list must not be
664 * freed while this function is running.
665 */
666 void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time);
667
668 /**
669 * timer_mod:
670 * @ts: the timer
671 * @expire_time: the expire time in the units associated with the timer
672 *
673 * Modify a timer to expiry at @expire_time, taking into
674 * account the scale associated with the timer.
675 *
676 * This function is thread-safe but the timer and its timer list must not be
677 * freed while this function is running.
678 */
679 void timer_mod(QEMUTimer *ts, int64_t expire_timer);
680
681 /**
682 * timer_mod_anticipate:
683 * @ts: the timer
684 * @expire_time: the expire time in the units associated with the timer
685 *
686 * Modify a timer to expire at @expire_time or the current time, whichever
687 * comes earlier, taking into account the scale associated with the timer.
688 *
689 * This function is thread-safe but the timer and its timer list must not be
690 * freed while this function is running.
691 */
692 void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time);
693
694 /**
695 * timer_pending:
696 * @ts: the timer
697 *
698 * Determines whether a timer is pending (i.e. is on the
699 * active list of timers, whether or not it has not yet expired).
700 *
701 * Returns: true if the timer is pending
702 */
703 bool timer_pending(const QEMUTimer *ts);
704
705 /**
706 * timer_expired:
707 * @ts: the timer
708 * @current_time: the current time
709 *
710 * Determines whether a timer has expired.
711 *
712 * Returns: true if the timer has expired
713 */
714 bool timer_expired(const QEMUTimer *timer_head, int64_t current_time);
715
716 /**
717 * timer_expire_time_ns:
718 * @ts: the timer
719 *
720 * Determine the expiry time of a timer
721 *
722 * Returns: the expiry time in nanoseconds
723 */
724 uint64_t timer_expire_time_ns(const QEMUTimer *ts);
725
726 /**
727 * timer_get:
728 * @f: the file
729 * @ts: the timer
730 *
731 * Read a timer @ts from a file @f
732 */
733 void timer_get(QEMUFile *f, QEMUTimer *ts);
734
735 /**
736 * timer_put:
737 * @f: the file
738 * @ts: the timer
739 */
740 void timer_put(QEMUFile *f, QEMUTimer *ts);
741
742 /*
743 * General utility functions
744 */
745
746 /**
747 * qemu_timeout_ns_to_ms:
748 * @ns: nanosecond timeout value
749 *
750 * Convert a nanosecond timeout value (or -1) to
751 * a millisecond value (or -1), always rounding up.
752 *
753 * Returns: millisecond timeout value
754 */
755 int qemu_timeout_ns_to_ms(int64_t ns);
756
757 /**
758 * qemu_poll_ns:
759 * @fds: Array of file descriptors
760 * @nfds: number of file descriptors
761 * @timeout: timeout in nanoseconds
762 *
763 * Perform a poll like g_poll but with a timeout in nanoseconds.
764 * See g_poll documentation for further details.
765 *
766 * Returns: number of fds ready
767 */
768 int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
769
770 /**
771 * qemu_soonest_timeout:
772 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
773 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
774 *
775 * Calculates the soonest of two timeout values. -1 means infinite, which
776 * is later than any other value.
777 *
778 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
779 */
780 static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
781 {
782 /* we can abuse the fact that -1 (which means infinite) is a maximal
783 * value when cast to unsigned. As this is disgusting, it's kept in
784 * one inline function.
785 */
786 return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
787 }
788
789 /**
790 * qemu_init_clocks:
791 * @notify_cb: optional call-back for timer expiry
792 *
793 * Initialise the clock & timer infrastructure
794 */
795 void qemu_init_clocks(QEMUTimerListNotifyCB *notify_cb);
796
797 static inline int64_t get_max_clock_jump(void)
798 {
799 /* This should be small enough to prevent excessive interrupts from being
800 * generated by the RTC on clock jumps, but large enough to avoid frequent
801 * unnecessary resets in idle VMs.
802 */
803 return 60 * NANOSECONDS_PER_SECOND;
804 }
805
806 /*
807 * Low level clock functions
808 */
809
810 /* get host real time in nanosecond */
811 static inline int64_t get_clock_realtime(void)
812 {
813 struct timeval tv;
814
815 gettimeofday(&tv, NULL);
816 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
817 }
818
819 extern int64_t clock_start;
820
821 /* Warning: don't insert tracepoints into these functions, they are
822 also used by simpletrace backend and tracepoints would cause
823 an infinite recursion! */
824 #ifdef _WIN32
825 extern int64_t clock_freq;
826
827 static inline int64_t get_clock(void)
828 {
829 LARGE_INTEGER ti;
830 QueryPerformanceCounter(&ti);
831 return muldiv64(ti.QuadPart, NANOSECONDS_PER_SECOND, clock_freq);
832 }
833
834 #else
835
836 extern int use_rt_clock;
837
838 static inline int64_t get_clock(void)
839 {
840 if (use_rt_clock) {
841 struct timespec ts;
842 clock_gettime(CLOCK_MONOTONIC, &ts);
843 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
844 } else {
845 /* XXX: using gettimeofday leads to problems if the date
846 changes, so it should be avoided. */
847 return get_clock_realtime();
848 }
849 }
850 #endif
851
852 /*******************************************/
853 /* host CPU ticks (if available) */
854
855 #if defined(_ARCH_PPC64)
856
857 static inline int64_t cpu_get_host_ticks(void)
858 {
859 int64_t retval;
860 /* This reads timebase in one 64bit go and includes Cell workaround from:
861 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
862 */
863 __asm__ __volatile__ ("mftb %0\n\t"
864 "cmpwi %0,0\n\t"
865 "beq- $-8"
866 : "=r" (retval));
867 return retval;
868 }
869
870 #elif defined(__x86_64__)
871
872 static inline int64_t cpu_get_host_ticks(void)
873 {
874 uint32_t low,high;
875 int64_t val;
876 asm volatile("rdtsc" : "=a" (low), "=d" (high));
877 val = high;
878 val <<= 32;
879 val |= low;
880 return val;
881 }
882
883 #elif defined(__hppa__)
884
885 static inline int64_t cpu_get_host_ticks(void)
886 {
887 int val;
888 asm volatile ("mfctl %%cr16, %0" : "=r"(val));
889 return val;
890 }
891
892 #elif defined(__s390x__)
893
894 static inline int64_t cpu_get_host_ticks(void)
895 {
896 int64_t val;
897 asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
898 return val;
899 }
900
901 #elif defined(__sparc__)
902
903 static inline int64_t cpu_get_host_ticks (void)
904 {
905 #if defined(_LP64)
906 uint64_t rval;
907 asm volatile("rd %%tick,%0" : "=r"(rval));
908 return rval;
909 #else
910 /* We need an %o or %g register for this. For recent enough gcc
911 there is an "h" constraint for that. Don't bother with that. */
912 union {
913 uint64_t i64;
914 struct {
915 uint32_t high;
916 uint32_t low;
917 } i32;
918 } rval;
919 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
920 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
921 return rval.i64;
922 #endif
923 }
924
925 #elif defined(__alpha__)
926
927 static inline int64_t cpu_get_host_ticks(void)
928 {
929 uint64_t cc;
930 uint32_t cur, ofs;
931
932 asm volatile("rpcc %0" : "=r"(cc));
933 cur = cc;
934 ofs = cc >> 32;
935 return cur - ofs;
936 }
937
938 #elif defined(__riscv) && __riscv_xlen == 32
939 static inline int64_t cpu_get_host_ticks(void)
940 {
941 uint32_t lo, hi, tmph;
942 do {
943 asm volatile("RDTIMEH %0\n\t"
944 "RDTIME %1\n\t"
945 "RDTIMEH %2"
946 : "=r"(hi), "=r"(lo), "=r"(tmph));
947 } while (unlikely(tmph != hi));
948 return lo | (uint64_t)hi << 32;
949 }
950
951 #elif defined(__riscv) && __riscv_xlen > 32
952 static inline int64_t cpu_get_host_ticks(void)
953 {
954 int64_t val;
955
956 asm volatile("RDTIME %0" : "=r"(val));
957 return val;
958 }
959
960 #elif defined(__loongarch64)
961 static inline int64_t cpu_get_host_ticks(void)
962 {
963 uint64_t val;
964
965 asm volatile("rdtime.d %0, $zero" : "=r"(val));
966 return val;
967 }
968
969 #else
970 /* The host CPU doesn't have an easily accessible cycle counter.
971 Just return a monotonically increasing value. This will be
972 totally wrong, but hopefully better than nothing. */
973 static inline int64_t cpu_get_host_ticks(void)
974 {
975 return get_clock();
976 }
977 #endif
978
979 #endif