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