timer.h 26.5 KB
Newer Older
pbrook's avatar
pbrook committed
1 2 3
#ifndef QEMU_TIMER_H
#define QEMU_TIMER_H

4
#include "qemu/typedefs.h"
Blue Swirl's avatar
Blue Swirl committed
5
#include "qemu-common.h"
6
#include "qemu/notify.h"
Blue Swirl's avatar
Blue Swirl committed
7

pbrook's avatar
pbrook committed
8 9
/* timers */

10 11 12 13
#define SCALE_MS 1000000
#define SCALE_US 1000
#define SCALE_NS 1

14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**
 * QEMUClockType:
 *
 * The following clock types are available:
 *
 * @QEMU_CLOCK_REALTIME: Real time clock
 *
 * The real time clock should be used only for stuff which does not
 * change the virtual machine state, as it is run even if the virtual
 * machine is stopped. The real time clock has a frequency of 1000
 * Hz.
 *
 * @QEMU_CLOCK_VIRTUAL: virtual clock
 *
 * The virtual clock is only run during the emulation. It is stopped
 * when the virtual machine is stopped. Virtual timers use a high
 * precision clock, usually cpu cycles (use ticks_per_sec).
 *
 * @QEMU_CLOCK_HOST: host clock
 *
 * The host clock should be use for device models that emulate accurate
 * real time sources. It will continue to run when the virtual machine
 * is suspended, and it will reflect system time changes the host may
 * undergo (e.g. due to NTP). The host clock has the same precision as
 * the virtual clock.
 */

typedef enum {
    QEMU_CLOCK_REALTIME = 0,
    QEMU_CLOCK_VIRTUAL = 1,
    QEMU_CLOCK_HOST = 2,
    QEMU_CLOCK_MAX
} QEMUClockType;
47

pbrook's avatar
pbrook committed
48
typedef struct QEMUClock QEMUClock;
49
typedef struct QEMUTimerList QEMUTimerList;
50 51 52 53 54

struct QEMUTimerListGroup {
    QEMUTimerList *tl[QEMU_CLOCK_MAX];
};

pbrook's avatar
pbrook committed
55
typedef void QEMUTimerCB(void *opaque);
56
typedef void QEMUTimerListNotifyCB(void *opaque);
pbrook's avatar
pbrook committed
57

58 59 60 61 62 63 64 65 66
struct QEMUTimer {
    int64_t expire_time;        /* in nanoseconds */
    QEMUTimerList *timer_list;
    QEMUTimerCB *cb;
    void *opaque;
    QEMUTimer *next;
    int scale;
};

67
extern QEMUTimerListGroup main_loop_tlg;
68
extern QEMUClock *qemu_clocks[QEMU_CLOCK_MAX];
pbrook's avatar
pbrook committed
69

70 71 72 73
/*
 * QEMUClock & QEMUClockType
 */

74 75 76 77 78 79 80 81 82 83 84 85
/**
 * qemu_clock_ptr:
 * @type: type of clock
 *
 * Translate a clock type into a pointer to QEMUClock object.
 *
 * Returns: a pointer to the QEMUClock object
 */
static inline QEMUClock *qemu_clock_ptr(QEMUClockType type)
{
    return qemu_clocks[type];
}
pbrook's avatar
pbrook committed
86

87 88 89 90 91 92 93 94 95
/**
 * qemu_clock_get_ns;
 * @type: the clock type
 *
 * Get the nanosecond value of a clock with
 * type @type
 *
 * Returns: the clock value in nanoseconds
 */
96
int64_t qemu_clock_get_ns(QEMUClockType type);
97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/**
 * qemu_clock_get_ms;
 * @type: the clock type
 *
 * Get the millisecond value of a clock with
 * type @type
 *
 * Returns: the clock value in milliseconds
 */
static inline int64_t qemu_clock_get_ms(QEMUClockType type)
{
    return qemu_clock_get_ns(type) / SCALE_MS;
}

/**
 * qemu_clock_get_us;
 * @type: the clock type
 *
 * Get the microsecond value of a clock with
 * type @type
 *
 * Returns: the clock value in microseconds
 */
static inline int64_t qemu_clock_get_us(QEMUClockType type)
{
    return qemu_clock_get_ns(type) / SCALE_US;
}

126 127
/**
 * qemu_clock_has_timers:
128
 * @type: the clock type
129 130 131 132 133 134 135
 *
 * Determines whether a clock's default timer list
 * has timers attached
 *
 * Returns: true if the clock's default timer list
 * has timers attached
 */
136
bool qemu_clock_has_timers(QEMUClockType type);
137 138 139

/**
 * qemu_clock_expired:
140
 * @type: the clock type
141 142 143 144 145 146 147
 *
 * Determines whether a clock's default timer list
 * has an expired clock.
 *
 * Returns: true if the clock's default timer list has
 * an expired timer
 */
148
bool qemu_clock_expired(QEMUClockType type);
149

150 151
/**
 * qemu_clock_use_for_deadline:
152
 * @type: the clock type
153 154 155 156 157 158 159 160 161 162 163
 *
 * Determine whether a clock should be used for deadline
 * calculations. Some clocks, for instance vm_clock with
 * use_icount set, do not count in nanoseconds. Such clocks
 * are not used for deadline calculations, and are presumed
 * to interrupt any poll using qemu_notify/aio_notify
 * etc.
 *
 * Returns: true if the clock runs in nanoseconds and
 * should be used for a deadline.
 */
164
bool qemu_clock_use_for_deadline(QEMUClockType type);
165

166
/**
167 168
 * qemu_clock_deadline_ns_all:
 * @type: the clock type
169 170 171 172 173 174 175
 *
 * Calculate the deadline across all timer lists associated
 * with a clock (as opposed to just the default one)
 * in nanoseconds, or -1 if no timer is set to expire.
 *
 * Returns: time until expiry in nanoseconds or -1
 */
176
int64_t qemu_clock_deadline_ns_all(QEMUClockType type);
177

178 179
/**
 * qemu_clock_get_main_loop_timerlist:
180
 * @type: the clock type
181 182 183 184 185
 *
 * Return the default timer list assocatiated with a clock.
 *
 * Returns: the default timer list
 */
186
QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type);
187

188 189
/**
 * qemu_clock_nofify:
190
 * @type: the clock type
191 192 193 194
 *
 * Call the notifier callback connected with the default timer
 * list linked to the clock, or qemu_notify() if none.
 */
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
void qemu_clock_notify(QEMUClockType type);

/**
 * qemu_clock_enable:
 * @type: the clock type
 * @enabled: true to enable, false to disable
 *
 * Enable or disable a clock
 */
void qemu_clock_enable(QEMUClockType type, bool enabled);

/**
 * qemu_clock_warp:
 * @type: the clock type
 *
 * Warp a clock to a new value
 */
void qemu_clock_warp(QEMUClockType type);

/**
 * qemu_clock_register_reset_notifier:
 * @type: the clock type
 * @notifier: the notifier function
 *
 * Register a notifier function to call when the clock
 * concerned is reset.
 */
void qemu_clock_register_reset_notifier(QEMUClockType type,
                                        Notifier *notifier);

/**
 * qemu_clock_unregister_reset_notifier:
 * @type: the clock type
 * @notifier: the notifier function
 *
 * Unregister a notifier function to call when the clock
 * concerned is reset.
 */
void qemu_clock_unregister_reset_notifier(QEMUClockType type,
                                          Notifier *notifier);

/**
 * qemu_clock_run_timers:
 * @type: clock on which to operate
 *
 * Run all the timers associated with the default timer list
 * of a clock.
 *
 * Returns: true if any timer ran.
 */
bool qemu_clock_run_timers(QEMUClockType type);

/**
 * qemu_clock_run_all_timers:
 *
 * Run all the timers associated with the default timer list
 * of every clock.
 *
 * Returns: true if any timer ran.
 */
bool qemu_clock_run_all_timers(void);

/*
 * QEMUTimerList
 */
260

261 262 263
/**
 * timerlist_new:
 * @type: the clock type to associate with the timerlist
264 265
 * @cb: the callback to call on notification
 * @opaque: the opaque pointer to pass to the callback
266 267 268 269 270 271
 *
 * Create a new timerlist associated with the clock of
 * type @type.
 *
 * Returns: a pointer to the QEMUTimerList created
 */
272 273
QEMUTimerList *timerlist_new(QEMUClockType type,
                             QEMUTimerListNotifyCB *cb, void *opaque);
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

/**
 * timerlist_free:
 * @timer_list: the timer list to free
 *
 * Frees a timer_list. It must have no active timers.
 */
void timerlist_free(QEMUTimerList *timer_list);

/**
 * timerlist_has_timers:
 * @timer_list: the timer list to operate on
 *
 * Determine whether a timer list has active timers
 *
 * Returns: true if the timer list has timers.
 */
bool timerlist_has_timers(QEMUTimerList *timer_list);

/**
 * timerlist_expired:
 * @timer_list: the timer list to operate on
 *
 * Determine whether a timer list has any timers which
 * are expired.
 *
 * Returns: true if the timer list has timers which
 * have expired.
 */
bool timerlist_expired(QEMUTimerList *timer_list);

/**
 * timerlist_deadline_ns:
 * @timer_list: the timer list to operate on
 *
 * Determine the deadline for a timer_list, i.e.
 * the number of nanoseconds until the first timer
 * expires. Return -1 if there are no timers.
 *
 * Returns: the number of nanoseconds until the earliest
 * timer expires -1 if none
 */
int64_t timerlist_deadline_ns(QEMUTimerList *timer_list);

/**
319
 * timerlist_get_clock:
320 321
 * @timer_list: the timer list to operate on
 *
322
 * Determine the clock type associated with a timer list.
323
 *
324 325
 * Returns: the clock type associated with the
 * timer list.
326
 */
327
QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list);
328 329 330 331 332 333 334 335 336 337 338

/**
 * timerlist_run_timers:
 * @timer_list: the timer list to use
 *
 * Call all expired timers associated with the timer list.
 *
 * Returns: true if any timer expired
 */
bool timerlist_run_timers(QEMUTimerList *timer_list);

339 340 341 342 343 344 345 346
/**
 * timerlist_notify:
 * @timer_list: the timer list to use
 *
 * call the notifier callback associated with the timer list.
 */
void timerlist_notify(QEMUTimerList *timer_list);

347 348 349 350
/*
 * QEMUTimerListGroup
 */

351 352 353
/**
 * timerlistgroup_init:
 * @tlg: the timer list group
354 355
 * @cb: the callback to call when a notify is required
 * @opaque: the opaque pointer to be passed to the callback.
356 357
 *
 * Initialise a timer list group. This must already be
358 359 360 361 362
 * allocated in memory and zeroed. The notifier callback is
 * called whenever a clock in the timer list group is
 * reenabled or whenever a timer associated with any timer
 * list is modified. If @cb is specified as null, qemu_notify()
 * is used instead.
363
 */
364 365
void timerlistgroup_init(QEMUTimerListGroup *tlg,
                         QEMUTimerListNotifyCB *cb, void *opaque);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387

/**
 * timerlistgroup_deinit:
 * @tlg: the timer list group
 *
 * Deinitialise a timer list group. This must already be
 * initialised. Note the memory is not freed.
 */
void timerlistgroup_deinit(QEMUTimerListGroup *tlg);

/**
 * timerlistgroup_run_timers:
 * @tlg: the timer list group
 *
 * Run the timers associated with a timer list group.
 * This will run timers on multiple clocks.
 *
 * Returns: true if any timer callback ran
 */
bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg);

/**
388
 * timerlistgroup_deadline_ns:
389 390 391 392 393 394 395 396 397 398 399 400
 * @tlg: the timer list group
 *
 * Determine the deadline of the soonest timer to
 * expire associated with any timer list linked to
 * the timer list group. Only clocks suitable for
 * deadline calculation are included.
 *
 * Returns: the deadline in nanoseconds or -1 if no
 * timers are to expire.
 */
int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg);

401 402
/*
 * QEMUTimer
403
 */
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447

/**
 * timer_init:
 * @ts: the timer to be initialised
 * @timer_list: the timer list to attach the timer to
 * @scale: the scale value for the tiemr
 * @cb: the callback to be called when the timer expires
 * @opaque: the opaque pointer to be passed to the callback
 *
 * Initialise a new timer and associate it with @timer_list.
 * The caller is responsible for allocating the memory.
 *
 * You need not call an explicit deinit call. Simply make
 * sure it is not on a list with timer_del.
 */
void timer_init(QEMUTimer *ts,
                QEMUTimerList *timer_list, int scale,
                QEMUTimerCB *cb, void *opaque);

/**
 * timer_new_tl:
 * @timer_list: the timer list to attach the timer to
 * @scale: the scale value for the tiemr
 * @cb: the callback to be called when the timer expires
 * @opaque: the opaque pointer to be passed to the callback
 *
 * Creeate a new timer and associate it with @timer_list.
 * The memory is allocated by the function.
 *
 * This is not the preferred interface unless you know you
 * are going to call timer_free. Use timer_init instead.
 *
 * Returns: a pointer to the timer
 */
static inline QEMUTimer *timer_new_tl(QEMUTimerList *timer_list,
                                      int scale,
                                      QEMUTimerCB *cb,
                                      void *opaque)
{
    QEMUTimer *ts = g_malloc0(sizeof(QEMUTimer));
    timer_init(ts, timer_list, scale, cb, opaque);
    return ts;
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
/**
 * timer_new:
 * @type: the clock type to use
 * @scale: the scale value for the tiemr
 * @cb: the callback to be called when the timer expires
 * @opaque: the opaque pointer to be passed to the callback
 *
 * Creeate a new timer and associate it with the default
 * timer list for the clock type @type.
 *
 * Returns: a pointer to the timer
 */
static inline QEMUTimer *timer_new(QEMUClockType type, int scale,
                                   QEMUTimerCB *cb, void *opaque)
{
    return timer_new_tl(main_loop_tlg.tl[type], scale, cb, opaque);
}

466
/**
467 468 469 470
 * timer_new_ns:
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
471
 *
472 473
 * Create a new timer with nanosecond scale on the default timer list
 * associated with the clock.
474
 *
475
 * Returns: a pointer to the newly created timer
476
 */
477 478
static inline QEMUTimer *timer_new_ns(QEMUClockType type, QEMUTimerCB *cb,
                                      void *opaque)
479
{
480
    return timer_new(type, SCALE_NS, cb, opaque);
481 482
}

483
/**
484 485 486 487
 * timer_new_us:
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
488
 *
489 490
 * Create a new timer with microsecond scale on the default timer list
 * associated with the clock.
491
 *
492
 * Returns: a pointer to the newly created timer
493
 */
494 495 496 497 498
static inline QEMUTimer *timer_new_us(QEMUClockType type, QEMUTimerCB *cb,
                                      void *opaque)
{
    return timer_new(type, SCALE_US, cb, opaque);
}
499

500
/**
501 502 503 504
 * timer_new_ms:
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
505
 *
506 507 508 509
 * Create a new timer with millisecond scale on the default timer list
 * associated with the clock.
 *
 * Returns: a pointer to the newly created timer
510
 */
511 512
static inline QEMUTimer *timer_new_ms(QEMUClockType type, QEMUTimerCB *cb,
                                      void *opaque)
513
{
514
    return timer_new(type, SCALE_MS, cb, opaque);
515 516
}

517
/**
518 519
 * timer_free:
 * @ts: the timer
520
 *
521
 * Free a timer (it must not be on the active list)
522
 */
523
void timer_free(QEMUTimer *ts);
524

525
/**
526 527
 * timer_del:
 * @ts: the timer
528
 *
529
 * Delete a timer from the active list.
530
 */
531
void timer_del(QEMUTimer *ts);
532

533
/**
534 535 536
 * timer_mod_ns:
 * @ts: the timer
 * @expire_time: the expiry time in nanoseconds
537
 *
538
 * Modify a timer to expire at @expire_time
539
 */
540
void timer_mod_ns(QEMUTimer *ts, int64_t expire_time);
541

542 543
/**
 * timer_mod:
544 545
 * @ts: the timer
 * @expire_time: the expire time in the units associated with the timer
546
 *
547 548
 * Modify a timer to expiry at @expire_time, taking into
 * account the scale associated with the timer.
549
 */
550
void timer_mod(QEMUTimer *ts, int64_t expire_timer);
551

552 553
/**
 * timer_pending:
554
 * @ts: the timer
555 556 557 558 559 560 561 562 563 564
 *
 * Determines whether a timer is pending (i.e. is on the
 * active list of timers, whether or not it has not yet expired).
 *
 * Returns: true if the timer is pending
 */
bool timer_pending(QEMUTimer *ts);

/**
 * timer_expired:
565
 * @ts: the timer
566 567 568 569 570 571 572 573 574
 *
 * Determines whether a timer has expired.
 *
 * Returns: true if the timer has expired
 */
bool timer_expired(QEMUTimer *timer_head, int64_t current_time);

/**
 * timer_expire_time_ns:
575
 * @ts: the timer
576
 *
577
 * Determine the expiry time of a timer
578
 *
579
 * Returns: the expiry time in nanoseconds
580 581 582
 */
uint64_t timer_expire_time_ns(QEMUTimer *ts);

583
/**
584 585 586
 * timer_get:
 * @f: the file
 * @ts: the timer
587
 *
588
 * Read a timer @ts from a file @f
589
 */
590
void timer_get(QEMUFile *f, QEMUTimer *ts);
591 592

/**
593 594 595 596 597 598 599 600 601 602 603 604 605
 * timer_put:
 * @f: the file
 * @ts: the timer
 */
void timer_put(QEMUFile *f, QEMUTimer *ts);

/*
 * General utility functions
 */

/**
 * qemu_timeout_ns_to_ms:
 * @ns: nanosecond timeout value
606
 *
607 608
 * Convert a nanosecond timeout value (or -1) to
 * a millisecond value (or -1), always rounding up.
609
 *
610
 * Returns: millisecond timeout value
611
 */
612
int qemu_timeout_ns_to_ms(int64_t ns);
613

614
/**
615 616 617 618
 * qemu_poll_ns:
 * @fds: Array of file descriptors
 * @nfds: number of file descriptors
 * @timeout: timeout in nanoseconds
619
 *
620 621 622 623
 * Perform a poll like g_poll but with a timeout in nanoseconds.
 * See g_poll documentation for further details.
 *
 * Returns: number of fds ready
624
 */
625
int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout);
626

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
/**
 * qemu_soonest_timeout:
 * @timeout1: first timeout in nanoseconds (or -1 for infinite)
 * @timeout2: second timeout in nanoseconds (or -1 for infinite)
 *
 * Calculates the soonest of two timeout values. -1 means infinite, which
 * is later than any other value.
 *
 * Returns: soonest timeout value in nanoseconds (or -1 for infinite)
 */
static inline int64_t qemu_soonest_timeout(int64_t timeout1, int64_t timeout2)
{
    /* we can abuse the fact that -1 (which means infinite) is a maximal
     * value when cast to unsigned. As this is disgusting, it's kept in
     * one inline function.
     */
    return ((uint64_t) timeout1 < (uint64_t) timeout2) ? timeout1 : timeout2;
}

646
/**
647
 * initclocks:
648
 *
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
 * Initialise the clock & timer infrastructure
 */
void init_clocks(void);

int64_t cpu_get_ticks(void);
void cpu_enable_ticks(void);
void cpu_disable_ticks(void);

static inline int64_t get_ticks_per_sec(void)
{
    return 1000000000LL;
}

/**************************************************
 * LEGACY API SECTION
664
 *
665
 * All these calls will be deleted in due course
666
 */
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

/* These three clocks are maintained here with separate variable
 * names for compatibility only.
 */
#define rt_clock (qemu_clock_ptr(QEMU_CLOCK_REALTIME))
#define vm_clock (qemu_clock_ptr(QEMU_CLOCK_VIRTUAL))
#define host_clock (qemu_clock_ptr(QEMU_CLOCK_HOST))

/** LEGACY
 * qemu_get_clock_ns:
 * @clock: the clock to operate on
 *
 * Get the nanosecond value of a clock
 *
 * Returns: the clock value in nanoseconds
 */
int64_t qemu_get_clock_ns(QEMUClock *clock);

/** LEGACY
 * qemu_get_clock_ms:
 * @clock: the clock to operate on
 *
 * Get the millisecond value of a clock
 *
 * Returns: the clock value in milliseconds
 */
static inline int64_t qemu_get_clock_ms(QEMUClock *clock)
694
{
695
    return qemu_get_clock_ns(clock) / SCALE_MS;
696 697
}

698 699 700 701
/** LEGACY
 * qemu_register_clock_reset_notifier:
 * @clock: the clock to operate on
 * @notifier: the notifier function
702
 *
703 704 705 706 707 708 709 710 711 712
 * Register a notifier function to call when the clock
 * concerned is reset.
 */
void qemu_register_clock_reset_notifier(QEMUClock *clock,
                                        Notifier *notifier);

/** LEGACY
 * qemu_unregister_clock_reset_notifier:
 * @clock: the clock to operate on
 * @notifier: the notifier function
713
 *
714 715
 * Unregister a notifier function to call when the clock
 * concerned is reset.
716
 */
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
void qemu_unregister_clock_reset_notifier(QEMUClock *clock,
                                          Notifier *notifier);

/** LEGACY
 * qemu_new_timer:
 * @clock: the clock to operate on
 * @scale: the scale of the clock
 * @cb: the callback function to call when the timer expires
 * @opaque: an opaque pointer to pass to the callback
 *
 * Produce a new timer attached to clock @clock. This is a legacy
 * function. Use timer_new instead.
 *
 * Returns: a pointer to the new timer allocated.
 */
QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
                          QEMUTimerCB *cb, void *opaque);

/** LEGACY
 * qemu_free_timer:
 * @ts: the timer to operate on
 *
 * free the timer @ts. @ts must not be active.
 *
 * This is a legacy function. Use timer_free instead.
 */
static inline void qemu_free_timer(QEMUTimer *ts)
744
{
745
    timer_free(ts);
746 747
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
/** LEGACY
 * qemu_del_timer:
 * @ts: the timer to operate on
 *
 * Delete a timer. This makes it inactive. It does not free
 * memory.
 *
 * This is a legacy function. Use timer_del instead.
 */
static inline void qemu_del_timer(QEMUTimer *ts)
{
    timer_del(ts);
}

/** LEGACY
 * qemu_mod_timer_ns:
 * @ts: the timer to operate on
 * @expire_time: the expiry time in nanoseconds
 *
 * Modify a timer such that the expiry time is @expire_time
 * as measured in nanoseconds
 *
 * This is a legacy function. Use timer_mod_ns.
 */
static inline void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
{
    timer_mod_ns(ts, expire_time);
}

/** LEGACY
 * qemu_mod_timer:
 * @ts: the timer to operate on
 * @expire_time: the expiry time
 *
 * Modify a timer such that the expiry time is @expire_time
 * as measured in the timer's scale
 *
 * This is a legacy function. Use timer_mod.
 */
static inline void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
{
    timer_mod(ts, expire_time);
}

/** LEGACY
 * qemu_run_timers:
 * @clock: clock on which to operate
 *
 * Run all the timers associated with the default timer list
 * of a clock.
 *
 * Returns: true if any timer ran.
 */
bool qemu_run_timers(QEMUClock *clock);

/** LEGACY
 * qemu_new_timer_ns:
805 806 807 808
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
 *
809
 * Create a new timer with nanosecond scale on the default timer list
810 811 812 813
 * associated with the clock.
 *
 * Returns: a pointer to the newly created timer
 */
814
static inline QEMUTimer *qemu_new_timer_ns(QEMUClock *clock, QEMUTimerCB *cb,
815 816
                                           void *opaque)
{
817
    return qemu_new_timer(clock, SCALE_NS, cb, opaque);
818 819
}

820 821
/** LEGACY
 * qemu_new_timer_us:
822 823 824 825 826 827 828 829 830
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
 *
 * Create a new timer with microsecond scale on the default timer list
 * associated with the clock.
 *
 * Returns: a pointer to the newly created timer
 */
831 832 833
static inline QEMUTimer *qemu_new_timer_us(QEMUClock *clock,
                                           QEMUTimerCB *cb,
                                           void *opaque)
834
{
835
    return qemu_new_timer(clock, SCALE_US, cb, opaque);
836 837
}

838
/** LEGACY
839 840 841 842 843 844 845 846 847 848 849 850
 * qemu_new_timer_ms:
 * @clock: the clock to associate with the timer
 * @callback: the callback to call when the timer expires
 * @opaque: the opaque pointer to pass to the callback
 *
 * Create a new timer with millisecond scale on the default timer list
 * associated with the clock.
 *
 * Returns: a pointer to the newly created timer
 */
static inline QEMUTimer *qemu_new_timer_ms(QEMUClock *clock,
                                           QEMUTimerCB *cb,
851 852
                                           void *opaque)
{
853
    return qemu_new_timer(clock, SCALE_MS, cb, opaque);
854 855
}

856 857
/****************************************************
 * END OF LEGACY API SECTION
858 859
 */

860

861 862 863
/*
 * Low level clock functions
 */
pbrook's avatar
pbrook committed
864

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
/* real time host monotonic timer */
static inline int64_t get_clock_realtime(void)
{
    struct timeval tv;

    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
}

/* Warning: don't insert tracepoints into these functions, they are
   also used by simpletrace backend and tracepoints would cause
   an infinite recursion! */
#ifdef _WIN32
extern int64_t clock_freq;

static inline int64_t get_clock(void)
{
    LARGE_INTEGER ti;
    QueryPerformanceCounter(&ti);
    return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
}

#else

extern int use_rt_clock;

static inline int64_t get_clock(void)
{
893
#ifdef CLOCK_MONOTONIC
894 895 896 897 898 899 900 901 902 903 904 905 906
    if (use_rt_clock) {
        struct timespec ts;
        clock_gettime(CLOCK_MONOTONIC, &ts);
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
    } else
#endif
    {
        /* XXX: using gettimeofday leads to problems if the date
           changes, so it should be avoided. */
        return get_clock_realtime();
    }
}
#endif
Paolo Bonzini's avatar
Paolo Bonzini committed
907

Blue Swirl's avatar
Blue Swirl committed
908 909
/* icount */
int64_t cpu_get_icount(void);
910
int64_t cpu_get_clock(void);
Blue Swirl's avatar
Blue Swirl committed
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930

/*******************************************/
/* host CPU ticks (if available) */

#if defined(_ARCH_PPC)

static inline int64_t cpu_get_real_ticks(void)
{
    int64_t retval;
#ifdef _ARCH_PPC64
    /* This reads timebase in one 64bit go and includes Cell workaround from:
       http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html
    */
    __asm__ __volatile__ ("mftb    %0\n\t"
                          "cmpwi   %0,0\n\t"
                          "beq-    $-8"
                          : "=r" (retval));
#else
    /* http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html */
    unsigned long junk;
931 932 933
    __asm__ __volatile__ ("mfspr   %1,269\n\t"  /* mftbu */
                          "mfspr   %L0,268\n\t" /* mftb */
                          "mfspr   %0,269\n\t"  /* mftbu */
Blue Swirl's avatar
Blue Swirl committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
                          "cmpw    %0,%1\n\t"
                          "bne     $-16"
                          : "=r" (retval), "=r" (junk));
#endif
    return retval;
}

#elif defined(__i386__)

static inline int64_t cpu_get_real_ticks(void)
{
    int64_t val;
    asm volatile ("rdtsc" : "=A" (val));
    return val;
}

#elif defined(__x86_64__)

static inline int64_t cpu_get_real_ticks(void)
{
    uint32_t low,high;
    int64_t val;
    asm volatile("rdtsc" : "=a" (low), "=d" (high));
    val = high;
    val <<= 32;
    val |= low;
    return val;
}

#elif defined(__hppa__)

static inline int64_t cpu_get_real_ticks(void)
{
    int val;
    asm volatile ("mfctl %%cr16, %0" : "=r"(val));
    return val;
}

#elif defined(__ia64)

static inline int64_t cpu_get_real_ticks(void)
{
    int64_t val;
    asm volatile ("mov %0 = ar.itc" : "=r"(val) :: "memory");
    return val;
}

#elif defined(__s390__)

static inline int64_t cpu_get_real_ticks(void)
{
    int64_t val;
    asm volatile("stck 0(%1)" : "=m" (val) : "a" (&val) : "cc");
    return val;
}

990
#elif defined(__sparc__)
Blue Swirl's avatar
Blue Swirl committed
991 992 993 994 995 996 997 998

static inline int64_t cpu_get_real_ticks (void)
{
#if defined(_LP64)
    uint64_t        rval;
    asm volatile("rd %%tick,%0" : "=r"(rval));
    return rval;
#else
999 1000
    /* We need an %o or %g register for this.  For recent enough gcc
       there is an "h" constraint for that.  Don't bother with that.  */
Blue Swirl's avatar
Blue Swirl committed
1001 1002 1003 1004 1005 1006 1007
    union {
        uint64_t i64;
        struct {
            uint32_t high;
            uint32_t low;
        }       i32;
    } rval;
1008 1009
    asm volatile("rd %%tick,%%g1; srlx %%g1,32,%0; mov %%g1,%1"
                 : "=r"(rval.i32.high), "=r"(rval.i32.low) : : "g1");
Blue Swirl's avatar
Blue Swirl committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
    return rval.i64;
#endif
}

#elif defined(__mips__) && \
    ((defined(__mips_isa_rev) && __mips_isa_rev >= 2) || defined(__linux__))
/*
 * binutils wants to use rdhwr only on mips32r2
 * but as linux kernel emulate it, it's fine
 * to use it.
 *
 */
#define MIPS_RDHWR(rd, value) {                         \
        __asm__ __volatile__ (".set   push\n\t"         \
                              ".set mips32r2\n\t"       \
                              "rdhwr  %0, "rd"\n\t"     \
                              ".set   pop"              \
                              : "=r" (value));          \
    }

static inline int64_t cpu_get_real_ticks(void)
{
    /* On kernels >= 2.6.25 rdhwr <reg>, $2 and $3 are emulated */
    uint32_t count;
    static uint32_t cyc_per_count = 0;

    if (!cyc_per_count) {
        MIPS_RDHWR("$3", cyc_per_count);
    }

    MIPS_RDHWR("$2", count);
    return (int64_t)(count * cyc_per_count);
}

1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
#elif defined(__alpha__)

static inline int64_t cpu_get_real_ticks(void)
{
    uint64_t cc;
    uint32_t cur, ofs;

    asm volatile("rpcc %0" : "=r"(cc));
    cur = cc;
    ofs = cc >> 32;
    return cur - ofs;
}

Blue Swirl's avatar
Blue Swirl committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
#else
/* The host CPU doesn't have an easily accessible cycle counter.
   Just return a monotonically increasing value.  This will be
   totally wrong, but hopefully better than nothing.  */
static inline int64_t cpu_get_real_ticks (void)
{
    static int64_t ticks = 0;
    return ticks++;
}
#endif

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
#ifdef CONFIG_PROFILER
static inline int64_t profile_getclock(void)
{
    return cpu_get_real_ticks();
}

extern int64_t qemu_time, qemu_time_start;
extern int64_t tlb_flush_time;
extern int64_t dev_time;
#endif

pbrook's avatar
pbrook committed
1079
#endif