Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 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 | |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 25 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 83c9089 | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 26 | #include "monitor/monitor.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 27 | #include "ui/console.h" |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 28 | |
| 29 | #include "hw/hw.h" |
| 30 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 31 | #include "qemu/timer.h" |
Anthony Liguori | 30ea833 | 2012-11-02 16:12:53 -0500 | [diff] [blame] | 32 | #ifdef CONFIG_POSIX |
| 33 | #include <pthread.h> |
| 34 | #endif |
Stefan Weil | bff9f8b | 2012-04-20 10:27:06 +0200 | [diff] [blame] | 35 | |
Alex Bligh | 4e0c652 | 2013-08-21 16:02:43 +0100 | [diff] [blame] | 36 | #ifdef CONFIG_PPOLL |
| 37 | #include <poll.h> |
| 38 | #endif |
| 39 | |
Alex Bligh | cd758dd | 2013-08-21 16:02:44 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 41 | #include <sys/prctl.h> |
| 42 | #endif |
| 43 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 44 | /***********************************************************/ |
| 45 | /* timers */ |
| 46 | |
Alex Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 47 | typedef struct QEMUClock { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 48 | QLIST_HEAD(, QEMUTimerList) timerlists; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 49 | |
| 50 | NotifierList reset_notifiers; |
| 51 | int64_t last; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 52 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 53 | QEMUClockType type; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 54 | bool enabled; |
Alex Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 55 | } QEMUClock; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 56 | |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 57 | QEMUTimerListGroup main_loop_tlg; |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 58 | QEMUClock qemu_clocks[QEMU_CLOCK_MAX]; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 59 | |
| 60 | /* A QEMUTimerList is a list of timers attached to a clock. More |
| 61 | * than one QEMUTimerList can be attached to each clock, for instance |
| 62 | * used by different AioContexts / threads. Each clock also has |
| 63 | * a list of the QEMUTimerLists associated with it, in order that |
| 64 | * reenabling the clock can call all the notifiers. |
| 65 | */ |
| 66 | |
| 67 | struct QEMUTimerList { |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 68 | QEMUClock *clock; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 69 | QEMUTimer *active_timers; |
| 70 | QLIST_ENTRY(QEMUTimerList) list; |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 71 | QEMUTimerListNotifyCB *notify_cb; |
| 72 | void *notify_opaque; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 75 | /** |
| 76 | * qemu_clock_ptr: |
| 77 | * @type: type of clock |
| 78 | * |
| 79 | * Translate a clock type into a pointer to QEMUClock object. |
| 80 | * |
| 81 | * Returns: a pointer to the QEMUClock object |
| 82 | */ |
Alex Bligh | b4049b7 | 2013-08-21 16:03:09 +0100 | [diff] [blame] | 83 | static inline QEMUClock *qemu_clock_ptr(QEMUClockType type) |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 84 | { |
| 85 | return &qemu_clocks[type]; |
| 86 | } |
| 87 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 88 | static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 89 | { |
| 90 | return timer_head && (timer_head->expire_time <= current_time); |
| 91 | } |
| 92 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 93 | QEMUTimerList *timerlist_new(QEMUClockType type, |
| 94 | QEMUTimerListNotifyCB *cb, |
| 95 | void *opaque) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 96 | { |
| 97 | QEMUTimerList *timer_list; |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 98 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 99 | |
| 100 | timer_list = g_malloc0(sizeof(QEMUTimerList)); |
| 101 | timer_list->clock = clock; |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 102 | timer_list->notify_cb = cb; |
| 103 | timer_list->notify_opaque = opaque; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 104 | QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list); |
| 105 | return timer_list; |
| 106 | } |
| 107 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 108 | void timerlist_free(QEMUTimerList *timer_list) |
| 109 | { |
| 110 | assert(!timerlist_has_timers(timer_list)); |
| 111 | if (timer_list->clock) { |
| 112 | QLIST_REMOVE(timer_list, list); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 113 | } |
| 114 | g_free(timer_list); |
| 115 | } |
| 116 | |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 117 | static void qemu_clock_init(QEMUClockType type) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 118 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 119 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 120 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 121 | clock->type = type; |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 122 | clock->enabled = true; |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 123 | clock->last = INT64_MIN; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 124 | QLIST_INIT(&clock->timerlists); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 125 | notifier_list_init(&clock->reset_notifiers); |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 126 | main_loop_tlg.tl[type] = timerlist_new(type, NULL, NULL); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 129 | bool qemu_clock_use_for_deadline(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 130 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 131 | return !(use_icount && (type == QEMU_CLOCK_VIRTUAL)); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 134 | void qemu_clock_notify(QEMUClockType type) |
Alex Bligh | b1bbfe7 | 2013-08-21 16:02:55 +0100 | [diff] [blame] | 135 | { |
| 136 | QEMUTimerList *timer_list; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 137 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | b1bbfe7 | 2013-08-21 16:02:55 +0100 | [diff] [blame] | 138 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { |
| 139 | timerlist_notify(timer_list); |
| 140 | } |
| 141 | } |
| 142 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 143 | void qemu_clock_enable(QEMUClockType type, bool enabled) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 144 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 145 | QEMUClock *clock = qemu_clock_ptr(type); |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 146 | bool old = clock->enabled; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 147 | clock->enabled = enabled; |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 148 | if (enabled && !old) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 149 | qemu_clock_notify(type); |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 150 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 153 | bool timerlist_has_timers(QEMUTimerList *timer_list) |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 154 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 155 | return !!timer_list->active_timers; |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 158 | bool qemu_clock_has_timers(QEMUClockType type) |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 159 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 160 | return timerlist_has_timers( |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 161 | main_loop_tlg.tl[type]); |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 162 | } |
| 163 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 164 | bool timerlist_expired(QEMUTimerList *timer_list) |
| 165 | { |
| 166 | return (timer_list->active_timers && |
| 167 | timer_list->active_timers->expire_time < |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 168 | qemu_clock_get_ns(timer_list->clock->type)); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 171 | bool qemu_clock_expired(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 172 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 173 | return timerlist_expired( |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 174 | main_loop_tlg.tl[type]); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 177 | /* |
| 178 | * As above, but return -1 for no deadline, and do not cap to 2^32 |
| 179 | * as we know the result is always positive. |
| 180 | */ |
| 181 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 182 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 183 | { |
| 184 | int64_t delta; |
| 185 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 186 | if (!timer_list->clock->enabled || !timer_list->active_timers) { |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 190 | delta = timer_list->active_timers->expire_time - |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 191 | qemu_clock_get_ns(timer_list->clock->type); |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 192 | |
| 193 | if (delta <= 0) { |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | return delta; |
| 198 | } |
| 199 | |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 200 | /* Calculate the soonest deadline across all timerlists attached |
| 201 | * to the clock. This is used for the icount timeout so we |
| 202 | * ignore whether or not the clock should be used in deadline |
| 203 | * calculations. |
| 204 | */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 205 | int64_t qemu_clock_deadline_ns_all(QEMUClockType type) |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 206 | { |
| 207 | int64_t deadline = -1; |
| 208 | QEMUTimerList *timer_list; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 209 | QEMUClock *clock = qemu_clock_ptr(type); |
Alex Bligh | ac70aaf | 2013-08-21 16:02:57 +0100 | [diff] [blame] | 210 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { |
| 211 | deadline = qemu_soonest_timeout(deadline, |
| 212 | timerlist_deadline_ns(timer_list)); |
| 213 | } |
| 214 | return deadline; |
| 215 | } |
| 216 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 217 | QEMUClockType timerlist_get_clock(QEMUTimerList *timer_list) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 218 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 219 | return timer_list->clock->type; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 222 | QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClockType type) |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 223 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 224 | return main_loop_tlg.tl[type]; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 227 | void timerlist_notify(QEMUTimerList *timer_list) |
| 228 | { |
| 229 | if (timer_list->notify_cb) { |
| 230 | timer_list->notify_cb(timer_list->notify_opaque); |
| 231 | } else { |
| 232 | qemu_notify_event(); |
| 233 | } |
| 234 | } |
| 235 | |
Alex Bligh | 02a03a9 | 2013-08-21 16:02:41 +0100 | [diff] [blame] | 236 | /* Transition function to convert a nanosecond timeout to ms |
| 237 | * This is used where a system does not support ppoll |
| 238 | */ |
| 239 | int qemu_timeout_ns_to_ms(int64_t ns) |
| 240 | { |
| 241 | int64_t ms; |
| 242 | if (ns < 0) { |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (!ns) { |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* Always round up, because it's better to wait too long than to wait too |
| 251 | * little and effectively busy-wait |
| 252 | */ |
| 253 | ms = (ns + SCALE_MS - 1) / SCALE_MS; |
| 254 | |
| 255 | /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */ |
| 256 | if (ms > (int64_t) INT32_MAX) { |
| 257 | ms = INT32_MAX; |
| 258 | } |
| 259 | |
| 260 | return (int) ms; |
| 261 | } |
| 262 | |
| 263 | |
Alex Bligh | 4e0c652 | 2013-08-21 16:02:43 +0100 | [diff] [blame] | 264 | /* qemu implementation of g_poll which uses a nanosecond timeout but is |
| 265 | * otherwise identical to g_poll |
| 266 | */ |
| 267 | int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout) |
| 268 | { |
| 269 | #ifdef CONFIG_PPOLL |
| 270 | if (timeout < 0) { |
| 271 | return ppoll((struct pollfd *)fds, nfds, NULL, NULL); |
| 272 | } else { |
| 273 | struct timespec ts; |
| 274 | ts.tv_sec = timeout / 1000000000LL; |
| 275 | ts.tv_nsec = timeout % 1000000000LL; |
| 276 | return ppoll((struct pollfd *)fds, nfds, &ts, NULL); |
| 277 | } |
| 278 | #else |
| 279 | return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout)); |
| 280 | #endif |
| 281 | } |
| 282 | |
| 283 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 284 | void timer_init(QEMUTimer *ts, |
| 285 | QEMUTimerList *timer_list, int scale, |
| 286 | QEMUTimerCB *cb, void *opaque) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 287 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 288 | ts->timer_list = timer_list; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 289 | ts->cb = cb; |
| 290 | ts->opaque = opaque; |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 291 | ts->scale = scale; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 294 | void timer_free(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 295 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 296 | g_free(ts); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* stop a timer, but do not dealloc it */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 300 | void timer_del(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 301 | { |
| 302 | QEMUTimer **pt, *t; |
| 303 | |
| 304 | /* NOTE: this code must be signal safe because |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 305 | timer_expired() can be called from a signal. */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 306 | pt = &ts->timer_list->active_timers; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 307 | for(;;) { |
| 308 | t = *pt; |
| 309 | if (!t) |
| 310 | break; |
| 311 | if (t == ts) { |
| 312 | *pt = t->next; |
| 313 | break; |
| 314 | } |
| 315 | pt = &t->next; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* modify the current timer so that it will be fired when current_time |
| 320 | >= expire_time. The corresponding callback will be called. */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 321 | void timer_mod_ns(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 322 | { |
| 323 | QEMUTimer **pt, *t; |
| 324 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 325 | timer_del(ts); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 326 | |
| 327 | /* add the timer in the sorted list */ |
| 328 | /* NOTE: this code must be signal safe because |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 329 | timer_expired() can be called from a signal. */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 330 | pt = &ts->timer_list->active_timers; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 331 | for(;;) { |
| 332 | t = *pt; |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 333 | if (!timer_expired_ns(t, expire_time)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 334 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 335 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 336 | pt = &t->next; |
| 337 | } |
| 338 | ts->expire_time = expire_time; |
| 339 | ts->next = *pt; |
| 340 | *pt = ts; |
| 341 | |
| 342 | /* Rearm if necessary */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 343 | if (pt == &ts->timer_list->active_timers) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 344 | /* Interrupt execution to force deadline recalculation. */ |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 345 | qemu_clock_warp(ts->timer_list->clock->type); |
Alex Bligh | b1bbfe7 | 2013-08-21 16:02:55 +0100 | [diff] [blame] | 346 | timerlist_notify(ts->timer_list); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 350 | void timer_mod(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 351 | { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 352 | timer_mod_ns(ts, expire_time * ts->scale); |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 353 | } |
| 354 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 355 | bool timer_pending(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 356 | { |
| 357 | QEMUTimer *t; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 358 | for (t = ts->timer_list->active_timers; t != NULL; t = t->next) { |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 359 | if (t == ts) { |
| 360 | return true; |
| 361 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 362 | } |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 363 | return false; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 366 | bool timer_expired(QEMUTimer *timer_head, int64_t current_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 367 | { |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 368 | return timer_expired_ns(timer_head, current_time * timer_head->scale); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 371 | bool timerlist_run_timers(QEMUTimerList *timer_list) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 372 | { |
Paolo Bonzini | 144b97c | 2012-09-19 15:52:44 +0200 | [diff] [blame] | 373 | QEMUTimer *ts; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 374 | int64_t current_time; |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 375 | bool progress = false; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 376 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 377 | if (!timer_list->clock->enabled) { |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 378 | return progress; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 379 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 380 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 381 | current_time = qemu_clock_get_ns(timer_list->clock->type); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 382 | for(;;) { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 383 | ts = timer_list->active_timers; |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 384 | if (!timer_expired_ns(ts, current_time)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 385 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 386 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 387 | /* remove timer from the list before calling the callback */ |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 388 | timer_list->active_timers = ts->next; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 389 | ts->next = NULL; |
| 390 | |
| 391 | /* run the callback (the timer list can be modified) */ |
| 392 | ts->cb(ts->opaque); |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 393 | progress = true; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 394 | } |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 395 | return progress; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 398 | bool qemu_clock_run_timers(QEMUClockType type) |
| 399 | { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 400 | return timerlist_run_timers(main_loop_tlg.tl[type]); |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 403 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
| 404 | QEMUTimerListNotifyCB *cb, void *opaque) |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 405 | { |
| 406 | QEMUClockType type; |
| 407 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | d5541d8 | 2013-08-21 16:02:50 +0100 | [diff] [blame] | 408 | tlg->tl[type] = timerlist_new(type, cb, opaque); |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
| 412 | void timerlistgroup_deinit(QEMUTimerListGroup *tlg) |
| 413 | { |
| 414 | QEMUClockType type; |
| 415 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 416 | timerlist_free(tlg->tl[type]); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg) |
| 421 | { |
| 422 | QEMUClockType type; |
| 423 | bool progress = false; |
| 424 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
| 425 | progress |= timerlist_run_timers(tlg->tl[type]); |
| 426 | } |
| 427 | return progress; |
| 428 | } |
| 429 | |
| 430 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg) |
| 431 | { |
| 432 | int64_t deadline = -1; |
| 433 | QEMUClockType type; |
| 434 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 435 | if (qemu_clock_use_for_deadline(tlg->tl[type]->clock->type)) { |
Alex Bligh | 754d6a5 | 2013-08-21 16:02:48 +0100 | [diff] [blame] | 436 | deadline = qemu_soonest_timeout(deadline, |
| 437 | timerlist_deadline_ns( |
| 438 | tlg->tl[type])); |
| 439 | } |
| 440 | } |
| 441 | return deadline; |
| 442 | } |
| 443 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 444 | int64_t qemu_clock_get_ns(QEMUClockType type) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 445 | { |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 446 | int64_t now, last; |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 447 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 448 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 449 | switch (type) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 450 | case QEMU_CLOCK_REALTIME: |
| 451 | return get_clock(); |
| 452 | default: |
| 453 | case QEMU_CLOCK_VIRTUAL: |
| 454 | if (use_icount) { |
| 455 | return cpu_get_icount(); |
| 456 | } else { |
| 457 | return cpu_get_clock(); |
| 458 | } |
| 459 | case QEMU_CLOCK_HOST: |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 460 | now = get_clock_realtime(); |
| 461 | last = clock->last; |
| 462 | clock->last = now; |
| 463 | if (now < last) { |
| 464 | notifier_list_notify(&clock->reset_notifiers, &now); |
| 465 | } |
| 466 | return now; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 470 | void qemu_clock_register_reset_notifier(QEMUClockType type, |
| 471 | Notifier *notifier) |
| 472 | { |
| 473 | QEMUClock *clock = qemu_clock_ptr(type); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 474 | notifier_list_add(&clock->reset_notifiers, notifier); |
| 475 | } |
| 476 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 477 | void qemu_clock_unregister_reset_notifier(QEMUClockType type, |
| 478 | Notifier *notifier) |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 479 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 480 | notifier_remove(notifier); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 483 | void init_clocks(void) |
| 484 | { |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 485 | QEMUClockType type; |
| 486 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | 7bf8fbd | 2013-08-21 16:03:03 +0100 | [diff] [blame] | 487 | qemu_clock_init(type); |
Paolo Bonzini | 744ca8e | 2012-10-29 15:26:28 +0100 | [diff] [blame] | 488 | } |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 489 | |
Alex Bligh | cd758dd | 2013-08-21 16:02:44 +0100 | [diff] [blame] | 490 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
| 491 | prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); |
| 492 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 493 | } |
| 494 | |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 495 | uint64_t timer_expire_time_ns(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 496 | { |
Alex Bligh | e93379b | 2013-08-21 16:02:39 +0100 | [diff] [blame] | 497 | return timer_pending(ts) ? ts->expire_time : -1; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 498 | } |
| 499 | |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 500 | bool qemu_clock_run_all_timers(void) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 501 | { |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 502 | bool progress = false; |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 503 | QEMUClockType type; |
Alex Bligh | 6d32717 | 2013-08-21 16:02:59 +0100 | [diff] [blame] | 504 | |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 505 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { |
Alex Bligh | 40daca5 | 2013-08-21 16:03:02 +0100 | [diff] [blame] | 506 | progress |= qemu_clock_run_timers(type); |
Alex Bligh | ff83c66 | 2013-08-21 16:02:46 +0100 | [diff] [blame] | 507 | } |
Peter Portante | 158fd3c | 2012-04-05 11:00:45 -0400 | [diff] [blame] | 508 | |
Alex Bligh | f9a976b | 2013-08-21 16:02:45 +0100 | [diff] [blame] | 509 | return progress; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 510 | } |