4 #include "qemu/typedefs.h"
5 #include "qemu-common.h"
6 #include "qemu/notify.h"
10 #define SCALE_MS 1000000
17 * The following clock types are available:
19 * @QEMU_CLOCK_REALTIME: Real time clock
21 * The real time clock should be used only for stuff which does not
22 * change the virtual machine state, as it is run even if the virtual
23 * machine is stopped. The real time clock has a frequency of 1000
28 * @QEMU_CLOCK_VIRTUAL: virtual clock
30 * The virtual clock is only run during the emulation. It is stopped
31 * when the virtual machine is stopped. Virtual timers use a high
32 * precision clock, usually cpu cycles (use ticks_per_sec).
36 * @QEMU_CLOCK_HOST: host clock
38 * The host clock should be use for device models that emulate accurate
39 * real time sources. It will continue to run when the virtual machine
40 * is suspended, and it will reflect system time changes the host may
41 * undergo (e.g. due to NTP). The host clock has the same precision as
48 QEMU_CLOCK_REALTIME
= 0,
49 QEMU_CLOCK_VIRTUAL
= 1,
54 typedef struct QEMUClock QEMUClock
;
55 typedef struct QEMUTimerList QEMUTimerList
;
56 typedef void QEMUTimerCB(void *opaque
);
59 int64_t expire_time
; /* in nanoseconds */
60 QEMUTimerList
*timer_list
;
67 extern QEMUClock
*qemu_clocks
[QEMU_CLOCK_MAX
];
71 * @type: type of clock
73 * Translate a clock type into a pointer to QEMUClock object.
75 * Returns: a pointer to the QEMUClock object
77 static inline QEMUClock
*qemu_clock_ptr(QEMUClockType type
)
79 return qemu_clocks
[type
];
82 /* These three clocks are maintained here with separate variable
83 * names for compatibility only.
85 #define rt_clock (qemu_clock_ptr(QEMU_CLOCK_REALTIME))
86 #define vm_clock (qemu_clock_ptr(QEMU_CLOCK_VIRTUAL))
87 #define host_clock (qemu_clock_ptr(QEMU_CLOCK_HOST))
89 int64_t qemu_get_clock_ns(QEMUClock
*clock
);
90 bool qemu_clock_has_timers(QEMUClock
*clock
);
91 bool qemu_clock_expired(QEMUClock
*clock
);
92 int64_t qemu_clock_deadline(QEMUClock
*clock
);
95 * qemu_clock_deadline_ns:
96 * @clock: the clock to operate on
98 * Calculate the timeout of the earliest expiring timer
99 * in nanoseconds, or -1 if no timer is set to expire.
101 * Returns: time until expiry in nanoseconds or -1
103 int64_t qemu_clock_deadline_ns(QEMUClock
*clock
);
106 * qemu_clock_use_for_deadline:
107 * @clock: the clock to operate on
109 * Determine whether a clock should be used for deadline
110 * calculations. Some clocks, for instance vm_clock with
111 * use_icount set, do not count in nanoseconds. Such clocks
112 * are not used for deadline calculations, and are presumed
113 * to interrupt any poll using qemu_notify/aio_notify
116 * Returns: true if the clock runs in nanoseconds and
117 * should be used for a deadline.
119 bool qemu_clock_use_for_deadline(QEMUClock
*clock
);
122 * qemu_clock_get_main_loop_timerlist:
123 * @clock: the clock to operate on
125 * Return the default timer list assocatiated with a clock.
127 * Returns: the default timer list
129 QEMUTimerList
*qemu_clock_get_main_loop_timerlist(QEMUClock
*clock
);
133 * @type: the clock type to associate with the timerlist
135 * Create a new timerlist associated with the clock of
138 * Returns: a pointer to the QEMUTimerList created
140 QEMUTimerList
*timerlist_new(QEMUClockType type
);
144 * @timer_list: the timer list to free
146 * Frees a timer_list. It must have no active timers.
148 void timerlist_free(QEMUTimerList
*timer_list
);
151 * timerlist_has_timers:
152 * @timer_list: the timer list to operate on
154 * Determine whether a timer list has active timers
156 * Returns: true if the timer list has timers.
158 bool timerlist_has_timers(QEMUTimerList
*timer_list
);
162 * @timer_list: the timer list to operate on
164 * Determine whether a timer list has any timers which
167 * Returns: true if the timer list has timers which
170 bool timerlist_expired(QEMUTimerList
*timer_list
);
173 * timerlist_deadline:
174 * @timer_list: the timer list to operate on
176 * Determine the deadline for a timer_list. This is
177 * a legacy function which returns INT32_MAX if the
178 * timer list has no timers or if the earliest timer
179 * expires later than INT32_MAX nanoseconds away.
181 * Returns: the number of nanoseconds until the earliest
182 * timer expires or INT32_MAX in the situations listed
185 int64_t timerlist_deadline(QEMUTimerList
*timer_list
);
188 * timerlist_deadline_ns:
189 * @timer_list: the timer list to operate on
191 * Determine the deadline for a timer_list, i.e.
192 * the number of nanoseconds until the first timer
193 * expires. Return -1 if there are no timers.
195 * Returns: the number of nanoseconds until the earliest
196 * timer expires -1 if none
198 int64_t timerlist_deadline_ns(QEMUTimerList
*timer_list
);
201 * timerlist_getclock:
202 * @timer_list: the timer list to operate on
204 * Determine the clock associated with a timer list.
206 * Returns: the clock associated with the timer list.
208 QEMUClock
*timerlist_get_clock(QEMUTimerList
*timer_list
);
211 * timerlist_run_timers:
212 * @timer_list: the timer list to use
214 * Call all expired timers associated with the timer list.
216 * Returns: true if any timer expired
218 bool timerlist_run_timers(QEMUTimerList
*timer_list
);
221 * qemu_timeout_ns_to_ms:
222 * @ns: nanosecond timeout value
224 * Convert a nanosecond timeout value (or -1) to
225 * a millisecond value (or -1), always rounding up.
227 * Returns: millisecond timeout value
229 int qemu_timeout_ns_to_ms(int64_t ns
);
233 * @fds: Array of file descriptors
234 * @nfds: number of file descriptors
235 * @timeout: timeout in nanoseconds
237 * Perform a poll like g_poll but with a timeout in nanoseconds.
238 * See g_poll documentation for further details.
240 * Returns: number of fds ready
242 int qemu_poll_ns(GPollFD
*fds
, guint nfds
, int64_t timeout
);
243 void qemu_clock_enable(QEMUClock
*clock
, bool enabled
);
244 void qemu_clock_warp(QEMUClock
*clock
);
246 void qemu_register_clock_reset_notifier(QEMUClock
*clock
, Notifier
*notifier
);
247 void qemu_unregister_clock_reset_notifier(QEMUClock
*clock
,
250 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, int scale
,
251 QEMUTimerCB
*cb
, void *opaque
);
255 * @ts: the timer to be initialised
256 * @timer_list: the timer list to attach the timer to
257 * @scale: the scale value for the tiemr
258 * @cb: the callback to be called when the timer expires
259 * @opaque: the opaque pointer to be passed to the callback
261 * Initialise a new timer and associate it with @timer_list.
262 * The caller is responsible for allocating the memory.
264 * You need not call an explicit deinit call. Simply make
265 * sure it is not on a list with timer_del.
267 void timer_init(QEMUTimer
*ts
,
268 QEMUTimerList
*timer_list
, int scale
,
269 QEMUTimerCB
*cb
, void *opaque
);
273 * @timer_list: the timer list to attach the timer to
274 * @scale: the scale value for the tiemr
275 * @cb: the callback to be called when the timer expires
276 * @opaque: the opaque pointer to be passed to the callback
278 * Creeate a new timer and associate it with @timer_list.
279 * The memory is allocated by the function.
281 * This is not the preferred interface unless you know you
282 * are going to call timer_free. Use timer_init instead.
284 * Returns: a pointer to the timer
286 static inline QEMUTimer
*timer_new_tl(QEMUTimerList
*timer_list
,
291 QEMUTimer
*ts
= g_malloc0(sizeof(QEMUTimer
));
292 timer_init(ts
, timer_list
, scale
, cb
, opaque
);
296 void qemu_free_timer(QEMUTimer
*ts
);
297 void qemu_del_timer(QEMUTimer
*ts
);
298 void qemu_mod_timer_ns(QEMUTimer
*ts
, int64_t expire_time
);
299 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
);
300 bool timer_pending(QEMUTimer
*ts
);
301 bool timer_expired(QEMUTimer
*timer_head
, int64_t current_time
);
302 uint64_t timer_expire_time_ns(QEMUTimer
*ts
);
304 /* New format calling conventions for timers */
310 * Free a timer (it must not be on the active list)
312 static inline void timer_free(QEMUTimer
*ts
)
321 * Delete a timer from the active list.
323 static inline void timer_del(QEMUTimer
*ts
)
331 * @expire_time: the expiry time in nanoseconds
333 * Modify a timer to expire at @expire_time
335 static inline void timer_mod_ns(QEMUTimer
*ts
, int64_t expire_time
)
337 qemu_mod_timer_ns(ts
, expire_time
);
343 * @expire_time: the expire time in the units associated with the timer
345 * Modify a timer to expiry at @expire_time, taking into
346 * account the scale associated with the timer.
348 static inline void timer_mod(QEMUTimer
*ts
, int64_t expire_timer
)
350 qemu_mod_timer(ts
, expire_timer
);
355 * @clock: clock on which to operate
357 * Run all the timers associated with the default timer list
360 * Returns: true if any timer ran.
362 bool qemu_run_timers(QEMUClock
*clock
);
365 * qemu_run_all_timers:
367 * Run all the timers associated with the default timer list
370 * Returns: true if any timer ran.
372 bool qemu_run_all_timers(void);
374 void configure_alarms(char const *opt
);
375 void init_clocks(void);
376 int init_timer_alarm(void);
378 int64_t cpu_get_ticks(void);
379 void cpu_enable_ticks(void);
380 void cpu_disable_ticks(void);
383 * qemu_soonest_timeout:
384 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
385 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
387 * Calculates the soonest of two timeout values. -1 means infinite, which
388 * is later than any other value.
390 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
392 static inline int64_t qemu_soonest_timeout(int64_t timeout1
, int64_t timeout2
)
394 /* we can abuse the fact that -1 (which means infinite) is a maximal
395 * value when cast to unsigned. As this is disgusting, it's kept in
396 * one inline function.
398 return ((uint64_t) timeout1
< (uint64_t) timeout2
) ? timeout1
: timeout2
;
403 * @clock: the clock to associate with the timer
404 * @callback: the callback to call when the timer expires
405 * @opaque: the opaque pointer to pass to the callback
407 * Create a new timer with nanosecond scale on the default timer list
408 * associated with the clock.
410 * Returns: a pointer to the newly created timer
412 static inline QEMUTimer
*qemu_new_timer_ns(QEMUClock
*clock
, QEMUTimerCB
*cb
,
415 return qemu_new_timer(clock
, SCALE_NS
, cb
, opaque
);
420 * @clock: the clock to associate with the timer
421 * @callback: the callback to call when the timer expires
422 * @opaque: the opaque pointer to pass to the callback
424 * Create a new timer with microsecond scale on the default timer list
425 * associated with the clock.
427 * Returns: a pointer to the newly created timer
429 static inline QEMUTimer
*qemu_new_timer_us(QEMUClock
*clock
,
433 return qemu_new_timer(clock
, SCALE_US
, cb
, opaque
);
438 * @clock: the clock to associate with the timer
439 * @callback: the callback to call when the timer expires
440 * @opaque: the opaque pointer to pass to the callback
442 * Create a new timer with millisecond scale on the default timer list
443 * associated with the clock.
445 * Returns: a pointer to the newly created timer
447 static inline QEMUTimer
*qemu_new_timer_ms(QEMUClock
*clock
,
451 return qemu_new_timer(clock
, SCALE_MS
, cb
, opaque
);
454 static inline int64_t qemu_get_clock_ms(QEMUClock
*clock
)
456 return qemu_get_clock_ns(clock
) / SCALE_MS
;
459 static inline int64_t get_ticks_per_sec(void)
464 /* real time host monotonic timer */
465 static inline int64_t get_clock_realtime(void)
469 gettimeofday(&tv
, NULL
);
470 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
473 /* Warning: don't insert tracepoints into these functions, they are
474 also used by simpletrace backend and tracepoints would cause
475 an infinite recursion! */
477 extern int64_t clock_freq
;
479 static inline int64_t get_clock(void)
482 QueryPerformanceCounter(&ti
);
483 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
488 extern int use_rt_clock
;
490 static inline int64_t get_clock(void)
492 #ifdef CLOCK_MONOTONIC
495 clock_gettime(CLOCK_MONOTONIC
, &ts
);
496 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
500 /* XXX: using gettimeofday leads to problems if the date
501 changes, so it should be avoided. */
502 return get_clock_realtime();
507 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
);
508 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
);
511 int64_t cpu_get_icount(void);
512 int64_t cpu_get_clock(void);
514 /*******************************************/
515 /* host CPU ticks (if available) */
517 #if defined(_ARCH_PPC)
519 static inline int64_t cpu_get_real_ticks(void)
523 /* This reads timebase in one 64bit go and includes Cell workaround from:
524 http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
526 __asm__
__volatile__ ("mftb %0\n\t"
531 /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
533 __asm__
__volatile__ ("mfspr %1,269\n\t" /* mftbu */
534 "mfspr %L0,268\n\t" /* mftb */
535 "mfspr %0,269\n\t" /* mftbu */
538 : "=r" (retval
), "=r" (junk
));
543 #elif defined(__i386__)
545 static inline int64_t cpu_get_real_ticks(void)
548 asm volatile ("rdtsc" : "=A" (val
));
552 #elif defined(__x86_64__)
554 static inline int64_t cpu_get_real_ticks(void)
558 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
565 #elif defined(__hppa__)
567 static inline int64_t cpu_get_real_ticks(void)
570 asm volatile ("mfctl %%cr16, %0" : "=r"(val
));
574 #elif defined(__ia64)
576 static inline int64_t cpu_get_real_ticks(void)
579 asm volatile ("mov %0 = ar.itc" : "=r"(val
) :: "memory");
583 #elif defined(__s390__)
585 static inline int64_t cpu_get_real_ticks(void)
588 asm volatile("stck 0(%1)" : "=m" (val
) : "a" (&val
) : "cc");
592 #elif defined(__sparc__)
594 static inline int64_t cpu_get_real_ticks (void)
598 asm volatile("rd %%tick,%0" : "=r"(rval
));
601 /* We need an %o or %g register for this. For recent enough gcc
602 there is an "h" constraint for that. Don't bother with that. */
610 asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
611 : "=r"(rval
.i32
.high
), "=r"(rval
.i32
.low
) : : "g1");
616 #elif defined(__mips__) && \
617 ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
619 * binutils wants to use rdhwr only on mips32r2
620 * but as linux kernel emulate it, it's fine
624 #define MIPS_RDHWR(rd, value) { \
625 __asm__ __volatile__ (".set push\n\t" \
626 ".set mips32r2\n\t" \
627 "rdhwr %0, "rd"\n\t" \
632 static inline int64_t cpu_get_real_ticks(void)
634 /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
636 static uint32_t cyc_per_count
= 0;
638 if (!cyc_per_count
) {
639 MIPS_RDHWR("$3", cyc_per_count
);
642 MIPS_RDHWR("$2", count
);
643 return (int64_t)(count
* cyc_per_count
);
646 #elif defined(__alpha__)
648 static inline int64_t cpu_get_real_ticks(void)
653 asm volatile("rpcc %0" : "=r"(cc
));
660 /* The host CPU doesn't have an easily accessible cycle counter.
661 Just return a monotonically increasing value. This will be
662 totally wrong, but hopefully better than nothing. */
663 static inline int64_t cpu_get_real_ticks (void)
665 static int64_t ticks
= 0;
670 #ifdef CONFIG_PROFILER
671 static inline int64_t profile_getclock(void)
673 return cpu_get_real_ticks();
676 extern int64_t qemu_time
, qemu_time_start
;
677 extern int64_t tlb_flush_time
;
678 extern int64_t dev_time
;