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