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