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