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