| 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #include "qemu/osdep.h" |
| 26 | #include "qemu/main-loop.h" |
| 27 | #include "qemu/timer.h" |
| 28 | #include "qemu/lockable.h" |
| 29 | #include "system/cpu-timers.h" |
| 30 | #include "exec/icount.h" |
| 31 | #include "system/replay.h" |
| 32 | #include "system/cpus.h" |
| 33 | #include "hw/core/cpu.h" |
| 34 | |
| 35 | #ifdef CONFIG_POSIX |
| 36 | #include <pthread.h> |
| 37 | #endif |
| 38 | |
| 39 | #ifdef CONFIG_PPOLL |
| 40 | #include <poll.h> |
| 41 | #endif |
| 42 | |
| 43 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 44 | #include <sys/prctl.h> |
| 45 | #endif |
| 46 | |
| 47 | /***********************************************************/ |
| 48 | /* timers */ |
| 49 | |
| 50 | typedef struct QEMUClock { |
| 51 | /* We rely on BQL to protect the timerlists */ |
| 52 | QLIST_HEAD(, QEMUTimerList) timerlists; |
| 53 | |
| 54 | QEMUClockType type; |
| 55 | bool enabled; |
| 56 | } QEMUClock; |
| 57 | |
| 58 | QEMUTimerListGroup main_loop_tlg; |
| 59 | static QEMUClock qemu_clocks[QEMU_CLOCK_MAX]; |
| 60 | |
| 61 | /* A QEMUTimerList is a list of timers attached to a clock. More |
| 62 | * than one QEMUTimerList can be attached to each clock, for instance |
| 63 | * used by different AioContexts / threads. Each clock also has |
| 64 | * a list of the QEMUTimerLists associated with it, in order that |
| 65 | * reenabling the clock can call all the notifiers. |
| 66 | */ |
| 67 | |
| 68 | struct QEMUTimerList { |
| 69 | QEMUClock *clock; |
| 70 | QemuMutex active_timers_lock; |
| 71 | QEMUTimer *active_timers; |
| 72 | QLIST_ENTRY(QEMUTimerList) list; |
| 73 | QEMUTimerListNotifyCB *notify_cb; |
| 74 | void *notify_opaque; |
| 75 | |
| 76 | /* lightweight method to mark the end of timerlist's running */ |
| 77 | QemuEvent timers_done_ev; |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * qemu_clock_ptr: |
| 82 | * @type: type of clock |
| 83 | * |
| 84 | * Translate a clock type into a pointer to QEMUClock object. |
| 85 | * |
| 86 | * Returns: a pointer to the QEMUClock object |
| 87 | */ |
| 88 | static inline QEMUClock *qemu_clock_ptr(QEMUClockType type) |
| 89 | { |
| 90 | return &qemu_clocks[type]; |
| 91 | } |
| 92 | |
| 93 | static bool timer_expired_ns(const QEMUTimer *timer_head, int64_t current_time) |
| 94 | { |
| 95 | return timer_head && (timer_head->expire_time <= current_time); |
| 96 | } |
| 97 | |
| 98 | QEMUTimerList *timerlist_new(QEMUClockType type, |
| 99 | QEMUTimerListNotifyCB *cb, |
| 100 | void *opaque) |
| 101 | { |
| 102 | QEMUTimerList *timer_list; |
| 103 | QEMUClock *clock = qemu_clock_ptr(type); |
| 104 | |
| 105 | timer_list = g_new0(QEMUTimerList, 1); |
| 106 | qemu_event_init(&timer_list->timers_done_ev, true); |
| 107 | timer_list->clock = clock; |
| 108 | timer_list->notify_cb = cb; |
| 109 | timer_list->notify_opaque = opaque; |
| 110 | qemu_mutex_init(&timer_list->active_timers_lock); |
| 111 | QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list); |
| 112 | return timer_list; |
| 113 | } |
| 114 | |
| 115 | void timerlist_free(QEMUTimerList *timer_list) |
| 116 | { |
| 117 | assert(!timerlist_has_timers(timer_list)); |
| 118 | if (timer_list->clock) { |
| 119 | QLIST_REMOVE(timer_list, list); |
| 120 | } |
| 121 | qemu_mutex_destroy(&timer_list->active_timers_lock); |
| 122 | g_free(timer_list); |
| 123 | } |
| 124 | |
| 125 | static void qemu_clock_init(QEMUClockType type, QEMUTimerListNotifyCB *notify_cb) |
| 126 | { |
| 127 | QEMUClock *clock = qemu_clock_ptr(type); |
| 128 | |
| 129 | /* Assert that the clock of type TYPE has not been initialized yet. */ |
| 130 | assert(main_loop_tlg.tl[type] == NULL); |
| 131 | |
| 132 | clock->type = type; |
| 133 | clock->enabled = (type == QEMU_CLOCK_VIRTUAL ? false : true); |
| 134 | QLIST_INIT(&clock->timerlists); |
| 135 | main_loop_tlg.tl[type] = timerlist_new(type, notify_cb, NULL); |
| 136 | } |
| 137 | |
| 138 | bool qemu_clock_use_for_deadline(QEMUClockType type) |
| 139 | { |
| 140 | return !(icount_enabled() && (type == QEMU_CLOCK_VIRTUAL)); |
| 141 | } |
| 142 | |
| 143 | void qemu_clock_notify(QEMUClockType type) |
| 144 | { |
| 145 | QEMUTimerList *timer_list; |
| 146 | QEMUClock *clock = qemu_clock_ptr(type); |
| 147 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { |
| 148 | timerlist_notify(timer_list); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /* Disabling the clock will wait for related timerlists to stop |
| 153 | * executing qemu_run_timers. Thus, this functions should not |
| 154 | * be used from the callback of a timer that is based on @clock. |
| 155 | * Doing so would cause a deadlock. |
| 156 | * |
| 157 | * Caller should hold BQL. |
| 158 | */ |
| 159 | void qemu_clock_enable(QEMUClockType type, bool enabled) |
| 160 | { |
| 161 | QEMUClock *clock = qemu_clock_ptr(type); |
| 162 | QEMUTimerList *tl; |
| 163 | bool old = clock->enabled; |
| 164 | clock->enabled = enabled; |
| 165 | if (enabled && !old) { |
| 166 | qemu_clock_notify(type); |
| 167 | } else if (!enabled && old) { |
| 168 | QLIST_FOREACH(tl, &clock->timerlists, list) { |
| 169 | qemu_event_wait(&tl->timers_done_ev); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool timerlist_has_timers(QEMUTimerList *timer_list) |
| 175 | { |
| 176 | return !!qatomic_read(&timer_list->active_timers); |
| 177 | } |
| 178 | |
| 179 | bool qemu_clock_has_timers(QEMUClockType type) |
| 180 | { |
| 181 | return timerlist_has_timers( |
| 182 | main_loop_tlg.tl[type]); |
| 183 | } |
| 184 | |
| 185 | bool timerlist_expired(QEMUTimerList *timer_list) |
| 186 | { |
| 187 | int64_t expire_time = 0; |
| 188 | |
| 189 | if (!qatomic_read(&timer_list->active_timers)) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) { |
| 194 | if (!timer_list->active_timers) { |
| 195 | return false; |
| 196 | } |
| 197 | expire_time = timer_list->active_timers->expire_time; |
| 198 | } |
| 199 | |
| 200 | return expire_time <= qemu_clock_get_ns(timer_list->clock->type); |
| 201 | } |
| 202 | |
| 203 | bool qemu_clock_expired(QEMUClockType type) |
| 204 | { |
| 205 | return timerlist_expired( |
| 206 | main_loop_tlg.tl[type]); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * As above, but return -1 for no deadline, and do not cap to 2^32 |
| 211 | * as we know the result is always positive. |
| 212 | */ |
| 213 | |
| 214 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) |
| 215 | { |
| 216 | int64_t delta; |
| 217 | int64_t expire_time = 0; |
| 218 | |
| 219 | if (!qatomic_read(&timer_list->active_timers)) { |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | if (!timer_list->clock->enabled) { |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | /* The active timers list may be modified before the caller uses our return |
| 228 | * value but ->notify_cb() is called when the deadline changes. Therefore |
| 229 | * the caller should notice the change and there is no race condition. |
| 230 | */ |
| 231 | WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) { |
| 232 | if (!timer_list->active_timers) { |
| 233 | return -1; |
| 234 | } |
| 235 | expire_time = timer_list->active_timers->expire_time; |
| 236 | } |
| 237 | |
| 238 | delta = expire_time - qemu_clock_get_ns(timer_list->clock->type); |
| 239 | |
| 240 | if (delta <= 0) { |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | return delta; |
| 245 | } |
| 246 | |
| 247 | /* Calculate the soonest deadline across all timerlists attached |
| 248 | * to the clock. This is used for the icount timeout so we |
| 249 | * ignore whether or not the clock should be used in deadline |
| 250 | * calculations. |
| 251 | */ |
| 252 | int64_t qemu_clock_deadline_ns_all(QEMUClockType type, int attr_mask) |
| 253 | { |
| 254 | int64_t deadline = -1; |
| 255 | int64_t delta; |
| 256 | int64_t expire_time; |
| 257 | QEMUTimer *ts; |
| 258 | QEMUTimerList *timer_list; |
| 259 | QEMUClock *clock = qemu_clock_ptr(type); |
| 260 | |
| 261 | if (!clock->enabled) { |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { |
| 266 | if (!qatomic_read(&timer_list->active_timers)) { |
| 267 | continue; |
| 268 | } |
| 269 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 270 | ts = timer_list->active_timers; |
| 271 | /* Skip all external timers */ |
| 272 | while (ts && (ts->attributes & ~attr_mask)) { |
| 273 | ts = ts->next; |
| 274 | } |
| 275 | if (!ts) { |
| 276 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 277 | continue; |
| 278 | } |
| 279 | expire_time = ts->expire_time; |
| 280 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 281 | |
| 282 | delta = expire_time - qemu_clock_get_ns(type); |
| 283 | if (delta <= 0) { |
| 284 | delta = 0; |
| 285 | } |
| 286 | deadline = qemu_soonest_timeout(deadline, delta); |
| 287 | } |
| 288 | return deadline; |
| 289 | } |
| 290 | |
| 291 | void timerlist_notify(QEMUTimerList *timer_list) |
| 292 | { |
| 293 | if (timer_list->notify_cb) { |
| 294 | timer_list->notify_cb(timer_list->notify_opaque, timer_list->clock->type); |
| 295 | } else { |
| 296 | qemu_notify_event(); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /* Transition function to convert a nanosecond timeout to ms |
| 301 | * This is used where a system does not support ppoll |
| 302 | */ |
| 303 | int qemu_timeout_ns_to_ms(int64_t ns) |
| 304 | { |
| 305 | int64_t ms; |
| 306 | if (ns < 0) { |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | if (!ns) { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* Always round up, because it's better to wait too long than to wait too |
| 315 | * little and effectively busy-wait |
| 316 | */ |
| 317 | ms = DIV_ROUND_UP(ns, SCALE_MS); |
| 318 | |
| 319 | /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */ |
| 320 | return MIN(ms, INT32_MAX); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* qemu implementation of g_poll which uses a nanosecond timeout but is |
| 325 | * otherwise identical to g_poll |
| 326 | */ |
| 327 | int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout) |
| 328 | { |
| 329 | #ifdef CONFIG_PPOLL |
| 330 | if (timeout < 0) { |
| 331 | return ppoll((struct pollfd *)fds, nfds, NULL, NULL); |
| 332 | } else { |
| 333 | struct timespec ts; |
| 334 | int64_t tvsec = timeout / 1000000000LL; |
| 335 | /* Avoid possibly overflowing and specifying a negative number of |
| 336 | * seconds, which would turn a very long timeout into a busy-wait. |
| 337 | */ |
| 338 | if (tvsec > (int64_t)INT32_MAX) { |
| 339 | tvsec = INT32_MAX; |
| 340 | } |
| 341 | ts.tv_sec = tvsec; |
| 342 | ts.tv_nsec = timeout % 1000000000LL; |
| 343 | return ppoll((struct pollfd *)fds, nfds, &ts, NULL); |
| 344 | } |
| 345 | #else |
| 346 | return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout)); |
| 347 | #endif |
| 348 | } |
| 349 | |
| 350 | |
| 351 | void timer_init_full(QEMUTimer *ts, |
| 352 | QEMUTimerListGroup *timer_list_group, QEMUClockType type, |
| 353 | int scale, int attributes, |
| 354 | QEMUTimerCB *cb, void *opaque) |
| 355 | { |
| 356 | if (!timer_list_group) { |
| 357 | timer_list_group = &main_loop_tlg; |
| 358 | } |
| 359 | ts->timer_list = timer_list_group->tl[type]; |
| 360 | ts->cb = cb; |
| 361 | ts->opaque = opaque; |
| 362 | ts->scale = scale; |
| 363 | ts->attributes = attributes; |
| 364 | ts->expire_time = -1; |
| 365 | } |
| 366 | |
| 367 | void timer_deinit(QEMUTimer *ts) |
| 368 | { |
| 369 | assert(ts->expire_time == -1); |
| 370 | ts->timer_list = NULL; |
| 371 | } |
| 372 | |
| 373 | static void timer_del_locked(QEMUTimerList *timer_list, QEMUTimer *ts) |
| 374 | { |
| 375 | QEMUTimer **pt, *t; |
| 376 | |
| 377 | ts->expire_time = -1; |
| 378 | pt = &timer_list->active_timers; |
| 379 | for(;;) { |
| 380 | t = *pt; |
| 381 | if (!t) |
| 382 | break; |
| 383 | if (t == ts) { |
| 384 | qatomic_set(pt, t->next); |
| 385 | break; |
| 386 | } |
| 387 | pt = &t->next; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | static void timer_fire(CPUState *cpu, run_on_cpu_data data) |
| 392 | { |
| 393 | QEMUTimer *t = data.host_ptr; |
| 394 | |
| 395 | t->cb(t->opaque); |
| 396 | } |
| 397 | |
| 398 | static bool timer_mod_ns_locked(QEMUTimerList *timer_list, |
| 399 | QEMUTimer *ts, int64_t expire_time) |
| 400 | { |
| 401 | QEMUTimer **pt, *t; |
| 402 | |
| 403 | /* |
| 404 | * Normally during record-replay virtual clock timers and CPU work are |
| 405 | * deterministically ordered. This is because the virtual clock can be |
| 406 | * advanced only by instructions running on a CPU. |
| 407 | * |
| 408 | * A notable exception are timers that are armed already expired. Their |
| 409 | * expiration is not constrained by instruction execution, and, therefore, |
| 410 | * their ordering relative to CPU work is affected by what the |
| 411 | * record-replay thread is doing when they are armed. This introduces |
| 412 | * non-determinism. |
| 413 | * |
| 414 | * Convert such timers to CPU work in order to avoid it. |
| 415 | */ |
| 416 | if (replay_mode != REPLAY_MODE_NONE && |
| 417 | timer_list->clock->type == QEMU_CLOCK_VIRTUAL && |
| 418 | !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL) && |
| 419 | expire_time <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) { |
| 420 | async_run_on_cpu(first_cpu, timer_fire, |
| 421 | RUN_ON_CPU_HOST_PTR(ts)); |
| 422 | return false; |
| 423 | } |
| 424 | |
| 425 | /* add the timer in the sorted list */ |
| 426 | pt = &timer_list->active_timers; |
| 427 | for (;;) { |
| 428 | t = *pt; |
| 429 | if (!timer_expired_ns(t, expire_time)) { |
| 430 | break; |
| 431 | } |
| 432 | pt = &t->next; |
| 433 | } |
| 434 | ts->expire_time = MAX(expire_time, 0); |
| 435 | ts->next = *pt; |
| 436 | qatomic_set(pt, ts); |
| 437 | |
| 438 | return pt == &timer_list->active_timers; |
| 439 | } |
| 440 | |
| 441 | static void timerlist_rearm(QEMUTimerList *timer_list) |
| 442 | { |
| 443 | timerlist_notify(timer_list); |
| 444 | } |
| 445 | |
| 446 | /* stop a timer, but do not dealloc it */ |
| 447 | void timer_del(QEMUTimer *ts) |
| 448 | { |
| 449 | QEMUTimerList *timer_list = ts->timer_list; |
| 450 | |
| 451 | if (timer_list) { |
| 452 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 453 | timer_del_locked(timer_list, ts); |
| 454 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* modify the current timer so that it will be fired when current_time |
| 459 | >= expire_time. The corresponding callback will be called. */ |
| 460 | void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) |
| 461 | { |
| 462 | QEMUTimerList *timer_list = ts->timer_list; |
| 463 | bool rearm; |
| 464 | |
| 465 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 466 | timer_del_locked(timer_list, ts); |
| 467 | rearm = timer_mod_ns_locked(timer_list, ts, expire_time); |
| 468 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 469 | |
| 470 | if (rearm) { |
| 471 | timerlist_rearm(timer_list); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* modify the current timer so that it will be fired when current_time |
| 476 | >= expire_time or the current deadline, whichever comes earlier. |
| 477 | The corresponding callback will be called. */ |
| 478 | void timer_mod_anticipate_ns(QEMUTimer *ts, int64_t expire_time) |
| 479 | { |
| 480 | QEMUTimerList *timer_list = ts->timer_list; |
| 481 | bool rearm = false; |
| 482 | |
| 483 | WITH_QEMU_LOCK_GUARD(&timer_list->active_timers_lock) { |
| 484 | if (ts->expire_time == -1 || ts->expire_time > expire_time) { |
| 485 | if (ts->expire_time != -1) { |
| 486 | timer_del_locked(timer_list, ts); |
| 487 | } |
| 488 | rearm = timer_mod_ns_locked(timer_list, ts, expire_time); |
| 489 | } else { |
| 490 | rearm = false; |
| 491 | } |
| 492 | } |
| 493 | if (rearm) { |
| 494 | timerlist_rearm(timer_list); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void timer_mod(QEMUTimer *ts, int64_t expire_time) |
| 499 | { |
| 500 | timer_mod_ns(ts, expire_time * ts->scale); |
| 501 | } |
| 502 | |
| 503 | void timer_mod_anticipate(QEMUTimer *ts, int64_t expire_time) |
| 504 | { |
| 505 | timer_mod_anticipate_ns(ts, expire_time * ts->scale); |
| 506 | } |
| 507 | |
| 508 | bool timer_pending(const QEMUTimer *ts) |
| 509 | { |
| 510 | return ts->expire_time >= 0; |
| 511 | } |
| 512 | |
| 513 | bool timer_expired(const QEMUTimer *timer_head, int64_t current_time) |
| 514 | { |
| 515 | return timer_expired_ns(timer_head, current_time * timer_head->scale); |
| 516 | } |
| 517 | |
| 518 | bool timerlist_run_timers(QEMUTimerList *timer_list) |
| 519 | { |
| 520 | QEMUTimer *ts; |
| 521 | int64_t current_time; |
| 522 | bool progress = false; |
| 523 | QEMUTimerCB *cb; |
| 524 | void *opaque; |
| 525 | |
| 526 | if (!qatomic_read(&timer_list->active_timers)) { |
| 527 | return false; |
| 528 | } |
| 529 | |
| 530 | qemu_event_reset(&timer_list->timers_done_ev); |
| 531 | if (!timer_list->clock->enabled) { |
| 532 | goto out; |
| 533 | } |
| 534 | |
| 535 | switch (timer_list->clock->type) { |
| 536 | case QEMU_CLOCK_REALTIME: |
| 537 | break; |
| 538 | default: |
| 539 | case QEMU_CLOCK_VIRTUAL: |
| 540 | break; |
| 541 | case QEMU_CLOCK_HOST: |
| 542 | if (!replay_checkpoint(CHECKPOINT_CLOCK_HOST)) { |
| 543 | goto out; |
| 544 | } |
| 545 | break; |
| 546 | case QEMU_CLOCK_VIRTUAL_RT: |
| 547 | if (!replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL_RT)) { |
| 548 | goto out; |
| 549 | } |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | /* |
| 554 | * Extract expired timers from active timers list and process them. |
| 555 | * |
| 556 | * In rr mode we need "filtered" checkpointing for virtual clock. The |
| 557 | * checkpoint must be recorded/replayed before processing any non-EXTERNAL timer, |
| 558 | * and that must only be done once since the clock value stays the same. Because |
| 559 | * non-EXTERNAL timers may appear in the timers list while it being processed, |
| 560 | * the checkpoint can be issued at a time until no timers are left and we are |
| 561 | * done". |
| 562 | */ |
| 563 | current_time = qemu_clock_get_ns(timer_list->clock->type); |
| 564 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 565 | while ((ts = timer_list->active_timers)) { |
| 566 | if (!timer_expired_ns(ts, current_time)) { |
| 567 | /* No expired timers left. The checkpoint can be skipped |
| 568 | * if no timers fired or they were all external. |
| 569 | */ |
| 570 | break; |
| 571 | } |
| 572 | /* Checkpoint for virtual clock is redundant in cases where |
| 573 | * it's being triggered with only non-EXTERNAL timers, because |
| 574 | * these timers don't change guest state directly. |
| 575 | */ |
| 576 | if (replay_mode != REPLAY_MODE_NONE |
| 577 | && timer_list->clock->type == QEMU_CLOCK_VIRTUAL |
| 578 | && !(ts->attributes & QEMU_TIMER_ATTR_EXTERNAL) |
| 579 | && !replay_checkpoint(CHECKPOINT_CLOCK_VIRTUAL)) { |
| 580 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 581 | goto out; |
| 582 | } |
| 583 | |
| 584 | /* remove timer from the list before calling the callback */ |
| 585 | timer_list->active_timers = ts->next; |
| 586 | ts->next = NULL; |
| 587 | ts->expire_time = -1; |
| 588 | cb = ts->cb; |
| 589 | opaque = ts->opaque; |
| 590 | |
| 591 | /* run the callback (the timer list can be modified) */ |
| 592 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 593 | cb(opaque); |
| 594 | qemu_mutex_lock(&timer_list->active_timers_lock); |
| 595 | |
| 596 | progress = true; |
| 597 | } |
| 598 | qemu_mutex_unlock(&timer_list->active_timers_lock); |
| 599 | |
| 600 | out: |
| 601 | qemu_event_set(&timer_list->timers_done_ev); |
| 602 | return progress; |
| 603 | } |
| 604 | |
| 605 | bool qemu_clock_run_timers(QEMUClockType type) |
| 606 | { |
| 607 | return timerlist_run_timers(main_loop_tlg.tl[type]); |
| 608 | } |
| 609 | |
| 610 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
| 611 | QEMUTimerListNotifyCB *cb, void *opaque) |
| 612 | { |
| 613 | QEMUClockType type; |
| 614 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 615 | tlg->tl[type] = timerlist_new(type, cb, opaque); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void timerlistgroup_deinit(QEMUTimerListGroup *tlg) |
| 620 | { |
| 621 | QEMUClockType type; |
| 622 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 623 | timerlist_free(tlg->tl[type]); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg) |
| 628 | { |
| 629 | QEMUClockType type; |
| 630 | bool progress = false; |
| 631 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 632 | progress |= timerlist_run_timers(tlg->tl[type]); |
| 633 | } |
| 634 | return progress; |
| 635 | } |
| 636 | |
| 637 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg) |
| 638 | { |
| 639 | int64_t deadline = -1; |
| 640 | QEMUClockType type; |
| 641 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 642 | if (qemu_clock_use_for_deadline(type)) { |
| 643 | deadline = qemu_soonest_timeout(deadline, |
| 644 | timerlist_deadline_ns(tlg->tl[type])); |
| 645 | } |
| 646 | } |
| 647 | return deadline; |
| 648 | } |
| 649 | |
| 650 | int64_t qemu_clock_get_ns(QEMUClockType type) |
| 651 | { |
| 652 | switch (type) { |
| 653 | case QEMU_CLOCK_REALTIME: |
| 654 | return get_clock(); |
| 655 | default: |
| 656 | case QEMU_CLOCK_VIRTUAL: |
| 657 | return cpus_get_virtual_clock(); |
| 658 | case QEMU_CLOCK_HOST: |
| 659 | return REPLAY_CLOCK(REPLAY_CLOCK_HOST, get_clock_realtime()); |
| 660 | case QEMU_CLOCK_VIRTUAL_RT: |
| 661 | return REPLAY_CLOCK(REPLAY_CLOCK_VIRTUAL_RT, cpu_get_clock()); |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | static void qemu_virtual_clock_set_ns(int64_t time) |
| 666 | { |
| 667 | return cpus_set_virtual_clock(time); |
| 668 | } |
| 669 | |
| 670 | void qemu_init_clocks(QEMUTimerListNotifyCB *notify_cb) |
| 671 | { |
| 672 | QEMUClockType type; |
| 673 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 674 | qemu_clock_init(type, notify_cb); |
| 675 | } |
| 676 | |
| 677 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 678 | prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); |
| 679 | #endif |
| 680 | } |
| 681 | |
| 682 | uint64_t timer_expire_time_ns(const QEMUTimer *ts) |
| 683 | { |
| 684 | return timer_pending(ts) ? ts->expire_time : -1; |
| 685 | } |
| 686 | |
| 687 | bool qemu_clock_run_all_timers(void) |
| 688 | { |
| 689 | bool progress = false; |
| 690 | QEMUClockType type; |
| 691 | |
| 692 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 693 | if (qemu_clock_use_for_deadline(type)) { |
| 694 | progress |= qemu_clock_run_timers(type); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | return progress; |
| 699 | } |
| 700 | |
| 701 | int64_t qemu_clock_advance_virtual_time(int64_t dest) |
| 702 | { |
| 703 | int64_t clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 704 | AioContext *aio_context; |
| 705 | aio_context = qemu_get_aio_context(); |
| 706 | while (clock < dest) { |
| 707 | int64_t deadline = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL, |
| 708 | QEMU_TIMER_ATTR_ALL); |
| 709 | int64_t warp = qemu_soonest_timeout(dest - clock, deadline); |
| 710 | |
| 711 | qemu_virtual_clock_set_ns(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + warp); |
| 712 | |
| 713 | qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL); |
| 714 | timerlist_run_timers(aio_context->tlg.tl[QEMU_CLOCK_VIRTUAL]); |
| 715 | clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); |
| 716 | } |
| 717 | qemu_clock_notify(QEMU_CLOCK_VIRTUAL); |
| 718 | |
| 719 | return clock; |
| 720 | } |