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