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