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