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