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 | |
| 25 | #include "sysemu.h" |
| 26 | #include "net.h" |
| 27 | #include "monitor.h" |
| 28 | #include "console.h" |
| 29 | |
| 30 | #include "hw/hw.h" |
| 31 | |
Stefan Weil | bff9f8b | 2012-04-20 10:27:06 +0200 | [diff] [blame] | 32 | #include "qemu-timer.h" |
| 33 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 34 | #ifdef _WIN32 |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 35 | #include <mmsystem.h> |
| 36 | #endif |
| 37 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 38 | /***********************************************************/ |
| 39 | /* timers */ |
| 40 | |
| 41 | #define QEMU_CLOCK_REALTIME 0 |
| 42 | #define QEMU_CLOCK_VIRTUAL 1 |
| 43 | #define QEMU_CLOCK_HOST 2 |
| 44 | |
| 45 | struct QEMUClock { |
Paolo Bonzini | 688eb38 | 2011-09-13 11:42:26 +0200 | [diff] [blame] | 46 | QEMUTimer *active_timers; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 47 | |
| 48 | NotifierList reset_notifiers; |
| 49 | int64_t last; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 50 | |
| 51 | int type; |
| 52 | bool enabled; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | struct QEMUTimer { |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 56 | int64_t expire_time; /* in nanoseconds */ |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 57 | QEMUClock *clock; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 58 | QEMUTimerCB *cb; |
| 59 | void *opaque; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 60 | QEMUTimer *next; |
| 61 | int scale; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | struct qemu_alarm_timer { |
| 65 | char const *name; |
| 66 | int (*start)(struct qemu_alarm_timer *t); |
| 67 | void (*stop)(struct qemu_alarm_timer *t); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 68 | void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns); |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 69 | #if defined(__linux__) |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 70 | timer_t timer; |
Stefan Weil | 9a14b29 | 2012-04-20 11:51:58 +0200 | [diff] [blame] | 71 | int fd; |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 72 | #elif defined(_WIN32) |
| 73 | HANDLE timer; |
| 74 | #endif |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 75 | bool expired; |
| 76 | bool pending; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | static struct qemu_alarm_timer *alarm_timer; |
| 80 | |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 81 | static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
| 82 | { |
| 83 | return timer_head && (timer_head->expire_time <= current_time); |
| 84 | } |
| 85 | |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 86 | static int64_t qemu_next_alarm_deadline(void) |
| 87 | { |
Stefano Stabellini | 4ffd16f | 2012-04-13 19:35:03 +0100 | [diff] [blame] | 88 | int64_t delta = INT64_MAX; |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 89 | int64_t rtdelta; |
| 90 | |
Stefano Stabellini | 4ffd16f | 2012-04-13 19:35:03 +0100 | [diff] [blame] | 91 | if (!use_icount && vm_clock->enabled && vm_clock->active_timers) { |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 92 | delta = vm_clock->active_timers->expire_time - |
| 93 | qemu_get_clock_ns(vm_clock); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 94 | } |
Stefano Stabellini | 4ffd16f | 2012-04-13 19:35:03 +0100 | [diff] [blame] | 95 | if (host_clock->enabled && host_clock->active_timers) { |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 96 | int64_t hdelta = host_clock->active_timers->expire_time - |
| 97 | qemu_get_clock_ns(host_clock); |
| 98 | if (hdelta < delta) { |
| 99 | delta = hdelta; |
| 100 | } |
| 101 | } |
Stefano Stabellini | 4ffd16f | 2012-04-13 19:35:03 +0100 | [diff] [blame] | 102 | if (rt_clock->enabled && rt_clock->active_timers) { |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 103 | rtdelta = (rt_clock->active_timers->expire_time - |
| 104 | qemu_get_clock_ns(rt_clock)); |
| 105 | if (rtdelta < delta) { |
| 106 | delta = rtdelta; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return delta; |
| 111 | } |
| 112 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 113 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 114 | { |
Stefano Stabellini | 8227421 | 2012-05-29 03:35:24 +0000 | [diff] [blame] | 115 | int64_t nearest_delta_ns = qemu_next_alarm_deadline(); |
| 116 | if (nearest_delta_ns < INT64_MAX) { |
| 117 | t->rearm(t, nearest_delta_ns); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 118 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 121 | /* TODO: MIN_TIMER_REARM_NS should be optimized */ |
| 122 | #define MIN_TIMER_REARM_NS 250000 |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 123 | |
| 124 | #ifdef _WIN32 |
| 125 | |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 126 | static int mm_start_timer(struct qemu_alarm_timer *t); |
| 127 | static void mm_stop_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 128 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 129 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 130 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 131 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 132 | static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 133 | |
| 134 | #else |
| 135 | |
| 136 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 137 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 138 | static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 139 | |
| 140 | #ifdef __linux__ |
| 141 | |
| 142 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 143 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 144 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 145 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 146 | #endif /* __linux__ */ |
| 147 | |
| 148 | #endif /* _WIN32 */ |
| 149 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 150 | static struct qemu_alarm_timer alarm_timers[] = { |
| 151 | #ifndef _WIN32 |
| 152 | #ifdef __linux__ |
| 153 | {"dynticks", dynticks_start_timer, |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 154 | dynticks_stop_timer, dynticks_rearm_timer}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 155 | #endif |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 156 | {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 157 | #else |
Paolo Bonzini | cca5de7 | 2011-11-09 12:46:56 +0100 | [diff] [blame] | 158 | {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer}, |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 159 | {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 160 | #endif |
| 161 | {NULL, } |
| 162 | }; |
| 163 | |
| 164 | static void show_available_alarms(void) |
| 165 | { |
| 166 | int i; |
| 167 | |
| 168 | printf("Available alarm timers, in order of precedence:\n"); |
| 169 | for (i = 0; alarm_timers[i].name; i++) |
| 170 | printf("%s\n", alarm_timers[i].name); |
| 171 | } |
| 172 | |
| 173 | void configure_alarms(char const *opt) |
| 174 | { |
| 175 | int i; |
| 176 | int cur = 0; |
| 177 | int count = ARRAY_SIZE(alarm_timers) - 1; |
| 178 | char *arg; |
| 179 | char *name; |
| 180 | struct qemu_alarm_timer tmp; |
| 181 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 182 | if (is_help_option(opt)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 183 | show_available_alarms(); |
| 184 | exit(0); |
| 185 | } |
| 186 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 187 | arg = g_strdup(opt); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 188 | |
| 189 | /* Reorder the array */ |
| 190 | name = strtok(arg, ","); |
| 191 | while (name) { |
| 192 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
| 193 | if (!strcmp(alarm_timers[i].name, name)) |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | if (i == count) { |
| 198 | fprintf(stderr, "Unknown clock %s\n", name); |
| 199 | goto next; |
| 200 | } |
| 201 | |
| 202 | if (i < cur) |
| 203 | /* Ignore */ |
| 204 | goto next; |
| 205 | |
| 206 | /* Swap */ |
| 207 | tmp = alarm_timers[i]; |
| 208 | alarm_timers[i] = alarm_timers[cur]; |
| 209 | alarm_timers[cur] = tmp; |
| 210 | |
| 211 | cur++; |
| 212 | next: |
| 213 | name = strtok(NULL, ","); |
| 214 | } |
| 215 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 216 | g_free(arg); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 217 | |
| 218 | if (cur) { |
| 219 | /* Disable remaining timers */ |
| 220 | for (i = cur; i < count; i++) |
| 221 | alarm_timers[i].name = NULL; |
| 222 | } else { |
| 223 | show_available_alarms(); |
| 224 | exit(1); |
| 225 | } |
| 226 | } |
| 227 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 228 | QEMUClock *rt_clock; |
| 229 | QEMUClock *vm_clock; |
| 230 | QEMUClock *host_clock; |
| 231 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 232 | static QEMUClock *qemu_new_clock(int type) |
| 233 | { |
| 234 | QEMUClock *clock; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 235 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 236 | clock = g_malloc0(sizeof(QEMUClock)); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 237 | clock->type = type; |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 238 | clock->enabled = true; |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 239 | clock->last = INT64_MIN; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 240 | notifier_list_init(&clock->reset_notifiers); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 241 | return clock; |
| 242 | } |
| 243 | |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 244 | void qemu_clock_enable(QEMUClock *clock, bool enabled) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 245 | { |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 246 | bool old = clock->enabled; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 247 | clock->enabled = enabled; |
Paolo Bonzini | fbdc14e | 2011-09-27 18:23:14 +0200 | [diff] [blame] | 248 | if (enabled && !old) { |
| 249 | qemu_rearm_alarm_timer(alarm_timer); |
| 250 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Paolo Bonzini | dc2dfcf | 2011-09-12 15:50:16 +0200 | [diff] [blame] | 253 | int64_t qemu_clock_has_timers(QEMUClock *clock) |
| 254 | { |
| 255 | return !!clock->active_timers; |
| 256 | } |
| 257 | |
| 258 | int64_t qemu_clock_expired(QEMUClock *clock) |
| 259 | { |
| 260 | return (clock->active_timers && |
| 261 | clock->active_timers->expire_time < qemu_get_clock_ns(clock)); |
| 262 | } |
| 263 | |
| 264 | int64_t qemu_clock_deadline(QEMUClock *clock) |
| 265 | { |
| 266 | /* To avoid problems with overflow limit this to 2^32. */ |
| 267 | int64_t delta = INT32_MAX; |
| 268 | |
| 269 | if (clock->active_timers) { |
| 270 | delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock); |
| 271 | } |
| 272 | if (delta < 0) { |
| 273 | delta = 0; |
| 274 | } |
| 275 | return delta; |
| 276 | } |
| 277 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 278 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, |
| 279 | QEMUTimerCB *cb, void *opaque) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 280 | { |
| 281 | QEMUTimer *ts; |
| 282 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 283 | ts = g_malloc0(sizeof(QEMUTimer)); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 284 | ts->clock = clock; |
| 285 | ts->cb = cb; |
| 286 | ts->opaque = opaque; |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 287 | ts->scale = scale; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 288 | return ts; |
| 289 | } |
| 290 | |
| 291 | void qemu_free_timer(QEMUTimer *ts) |
| 292 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 293 | g_free(ts); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /* stop a timer, but do not dealloc it */ |
| 297 | void qemu_del_timer(QEMUTimer *ts) |
| 298 | { |
| 299 | QEMUTimer **pt, *t; |
| 300 | |
| 301 | /* NOTE: this code must be signal safe because |
| 302 | qemu_timer_expired() can be called from a signal. */ |
Paolo Bonzini | 688eb38 | 2011-09-13 11:42:26 +0200 | [diff] [blame] | 303 | pt = &ts->clock->active_timers; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 304 | for(;;) { |
| 305 | t = *pt; |
| 306 | if (!t) |
| 307 | break; |
| 308 | if (t == ts) { |
| 309 | *pt = t->next; |
| 310 | break; |
| 311 | } |
| 312 | pt = &t->next; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /* modify the current timer so that it will be fired when current_time |
| 317 | >= expire_time. The corresponding callback will be called. */ |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 318 | void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 319 | { |
| 320 | QEMUTimer **pt, *t; |
| 321 | |
| 322 | qemu_del_timer(ts); |
| 323 | |
| 324 | /* add the timer in the sorted list */ |
| 325 | /* NOTE: this code must be signal safe because |
| 326 | qemu_timer_expired() can be called from a signal. */ |
Paolo Bonzini | 688eb38 | 2011-09-13 11:42:26 +0200 | [diff] [blame] | 327 | pt = &ts->clock->active_timers; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 328 | for(;;) { |
| 329 | t = *pt; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 330 | if (!qemu_timer_expired_ns(t, expire_time)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 331 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 332 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 333 | pt = &t->next; |
| 334 | } |
| 335 | ts->expire_time = expire_time; |
| 336 | ts->next = *pt; |
| 337 | *pt = ts; |
| 338 | |
| 339 | /* Rearm if necessary */ |
Paolo Bonzini | 688eb38 | 2011-09-13 11:42:26 +0200 | [diff] [blame] | 340 | if (pt == &ts->clock->active_timers) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 341 | if (!alarm_timer->pending) { |
| 342 | qemu_rearm_alarm_timer(alarm_timer); |
| 343 | } |
| 344 | /* Interrupt execution to force deadline recalculation. */ |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 345 | qemu_clock_warp(ts->clock); |
| 346 | if (use_icount) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 347 | qemu_notify_event(); |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 348 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 352 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 353 | { |
| 354 | qemu_mod_timer_ns(ts, expire_time * ts->scale); |
| 355 | } |
| 356 | |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 357 | bool qemu_timer_pending(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 358 | { |
| 359 | QEMUTimer *t; |
Paolo Bonzini | 688eb38 | 2011-09-13 11:42:26 +0200 | [diff] [blame] | 360 | for (t = ts->clock->active_timers; t != NULL; t = t->next) { |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 361 | if (t == ts) { |
| 362 | return true; |
| 363 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 364 | } |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 365 | return false; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 366 | } |
| 367 | |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 368 | bool qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 369 | { |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 370 | return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Paolo Bonzini | 8156be5 | 2012-03-28 15:42:04 +0200 | [diff] [blame] | 373 | void qemu_run_timers(QEMUClock *clock) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 374 | { |
Paolo Bonzini | 144b97c | 2012-09-19 15:52:44 +0200 | [diff] [blame] | 375 | QEMUTimer *ts; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 376 | int64_t current_time; |
| 377 | |
| 378 | if (!clock->enabled) |
| 379 | return; |
| 380 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 381 | current_time = qemu_get_clock_ns(clock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 382 | for(;;) { |
Paolo Bonzini | 144b97c | 2012-09-19 15:52:44 +0200 | [diff] [blame] | 383 | ts = clock->active_timers; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 384 | if (!qemu_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 */ |
Paolo Bonzini | 144b97c | 2012-09-19 15:52:44 +0200 | [diff] [blame] | 388 | clock->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); |
| 393 | } |
| 394 | } |
| 395 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 396 | int64_t qemu_get_clock_ns(QEMUClock *clock) |
| 397 | { |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 398 | int64_t now, last; |
| 399 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 400 | switch(clock->type) { |
| 401 | case QEMU_CLOCK_REALTIME: |
| 402 | return get_clock(); |
| 403 | default: |
| 404 | case QEMU_CLOCK_VIRTUAL: |
| 405 | if (use_icount) { |
| 406 | return cpu_get_icount(); |
| 407 | } else { |
| 408 | return cpu_get_clock(); |
| 409 | } |
| 410 | case QEMU_CLOCK_HOST: |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 411 | now = get_clock_realtime(); |
| 412 | last = clock->last; |
| 413 | clock->last = now; |
| 414 | if (now < last) { |
| 415 | notifier_list_notify(&clock->reset_notifiers, &now); |
| 416 | } |
| 417 | return now; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 421 | void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) |
| 422 | { |
| 423 | notifier_list_add(&clock->reset_notifiers, notifier); |
| 424 | } |
| 425 | |
| 426 | void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) |
| 427 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 428 | notifier_remove(notifier); |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 431 | void init_clocks(void) |
| 432 | { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 433 | rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
| 434 | vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
| 435 | host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 438 | uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 439 | { |
Paolo Bonzini | 2ff68d0 | 2011-09-12 16:21:44 +0200 | [diff] [blame] | 440 | return qemu_timer_pending(ts) ? ts->expire_time : -1; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 441 | } |
| 442 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 443 | void qemu_run_all_timers(void) |
| 444 | { |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 445 | alarm_timer->pending = false; |
Paolo Bonzini | ca5a2a4 | 2010-03-19 11:30:35 +0100 | [diff] [blame] | 446 | |
Peter Portante | 158fd3c | 2012-04-05 11:00:45 -0400 | [diff] [blame] | 447 | /* vm time timers */ |
| 448 | qemu_run_timers(vm_clock); |
| 449 | qemu_run_timers(rt_clock); |
| 450 | qemu_run_timers(host_clock); |
| 451 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 452 | /* rearm timer, if not periodic */ |
| 453 | if (alarm_timer->expired) { |
Stefan Weil | 5e1ec7b | 2012-04-20 10:45:48 +0200 | [diff] [blame] | 454 | alarm_timer->expired = false; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 455 | qemu_rearm_alarm_timer(alarm_timer); |
| 456 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | #ifdef _WIN32 |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 460 | static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 461 | #else |
| 462 | static void host_alarm_handler(int host_signum) |
| 463 | #endif |
| 464 | { |
| 465 | struct qemu_alarm_timer *t = alarm_timer; |
| 466 | if (!t) |
| 467 | return; |
| 468 | |
Stefan Weil | 8205199 | 2012-04-20 11:27:24 +0200 | [diff] [blame] | 469 | t->expired = true; |
| 470 | t->pending = true; |
| 471 | qemu_notify_event(); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 472 | } |
| 473 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 474 | #if defined(__linux__) |
| 475 | |
Jan Kiszka | d25f89c | 2011-06-17 11:25:49 +0200 | [diff] [blame] | 476 | #include "compatfd.h" |
| 477 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 478 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 479 | { |
| 480 | struct sigevent ev; |
| 481 | timer_t host_timer; |
| 482 | struct sigaction act; |
| 483 | |
| 484 | sigfillset(&act.sa_mask); |
| 485 | act.sa_flags = 0; |
| 486 | act.sa_handler = host_alarm_handler; |
| 487 | |
| 488 | sigaction(SIGALRM, &act, NULL); |
| 489 | |
| 490 | /* |
| 491 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 492 | * about uninitialized data in timer_create call |
| 493 | */ |
| 494 | memset(&ev, 0, sizeof(ev)); |
| 495 | ev.sigev_value.sival_int = 0; |
| 496 | ev.sigev_notify = SIGEV_SIGNAL; |
Jan Kiszka | d25f89c | 2011-06-17 11:25:49 +0200 | [diff] [blame] | 497 | #ifdef SIGEV_THREAD_ID |
| 498 | if (qemu_signalfd_available()) { |
| 499 | ev.sigev_notify = SIGEV_THREAD_ID; |
| 500 | ev._sigev_un._tid = qemu_get_thread_id(); |
| 501 | } |
| 502 | #endif /* SIGEV_THREAD_ID */ |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 503 | ev.sigev_signo = SIGALRM; |
| 504 | |
| 505 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 506 | perror("timer_create"); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 507 | return -1; |
| 508 | } |
| 509 | |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 510 | t->timer = host_timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 516 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 517 | timer_t host_timer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 518 | |
| 519 | timer_delete(host_timer); |
| 520 | } |
| 521 | |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 522 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, |
| 523 | int64_t nearest_delta_ns) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 524 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 525 | timer_t host_timer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 526 | struct itimerspec timeout; |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 527 | int64_t current_ns; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 528 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 529 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
| 530 | nearest_delta_ns = MIN_TIMER_REARM_NS; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 531 | |
| 532 | /* check whether a timer is already running */ |
| 533 | if (timer_gettime(host_timer, &timeout)) { |
| 534 | perror("gettime"); |
| 535 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 536 | exit(1); |
| 537 | } |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 538 | current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; |
| 539 | if (current_ns && current_ns <= nearest_delta_ns) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 540 | return; |
| 541 | |
| 542 | timeout.it_interval.tv_sec = 0; |
| 543 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 544 | timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; |
| 545 | timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 546 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 547 | perror("settime"); |
| 548 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 549 | exit(1); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | #endif /* defined(__linux__) */ |
| 554 | |
Stefan Weil | f26e5a5 | 2011-02-04 22:01:32 +0100 | [diff] [blame] | 555 | #if !defined(_WIN32) |
| 556 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 557 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 558 | { |
| 559 | struct sigaction act; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 560 | |
| 561 | /* timer signal */ |
| 562 | sigfillset(&act.sa_mask); |
| 563 | act.sa_flags = 0; |
| 564 | act.sa_handler = host_alarm_handler; |
| 565 | |
| 566 | sigaction(SIGALRM, &act, NULL); |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 567 | return 0; |
| 568 | } |
| 569 | |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 570 | static void unix_rearm_timer(struct qemu_alarm_timer *t, |
| 571 | int64_t nearest_delta_ns) |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 572 | { |
| 573 | struct itimerval itv; |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 574 | int err; |
| 575 | |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 576 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
| 577 | nearest_delta_ns = MIN_TIMER_REARM_NS; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 578 | |
| 579 | itv.it_interval.tv_sec = 0; |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 580 | itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */ |
| 581 | itv.it_value.tv_sec = nearest_delta_ns / 1000000000; |
| 582 | itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 583 | err = setitimer(ITIMER_REAL, &itv, NULL); |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 584 | if (err) { |
| 585 | perror("setitimer"); |
| 586 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 587 | exit(1); |
| 588 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 592 | { |
| 593 | struct itimerval itv; |
| 594 | |
| 595 | memset(&itv, 0, sizeof(itv)); |
| 596 | setitimer(ITIMER_REAL, &itv, NULL); |
| 597 | } |
| 598 | |
| 599 | #endif /* !defined(_WIN32) */ |
| 600 | |
| 601 | |
| 602 | #ifdef _WIN32 |
| 603 | |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 604 | static MMRESULT mm_timer; |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 605 | static TIMECAPS mm_tc; |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 606 | |
| 607 | static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg, |
| 608 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 609 | DWORD_PTR dw2) |
| 610 | { |
| 611 | struct qemu_alarm_timer *t = alarm_timer; |
| 612 | if (!t) { |
| 613 | return; |
| 614 | } |
Stefan Weil | 8205199 | 2012-04-20 11:27:24 +0200 | [diff] [blame] | 615 | t->expired = true; |
| 616 | t->pending = true; |
| 617 | qemu_notify_event(); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static int mm_start_timer(struct qemu_alarm_timer *t) |
| 621 | { |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 622 | timeGetDevCaps(&mm_tc, sizeof(mm_tc)); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 623 | |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 624 | timeBeginPeriod(mm_tc.wPeriodMin); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 625 | |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 626 | mm_timer = timeSetEvent(mm_tc.wPeriodMin, /* interval (ms) */ |
| 627 | mm_tc.wPeriodMin, /* resolution */ |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 628 | mm_alarm_handler, /* function */ |
| 629 | (DWORD_PTR)t, /* parameter */ |
Stefan Weil | 8205199 | 2012-04-20 11:27:24 +0200 | [diff] [blame] | 630 | TIME_ONESHOT | TIME_CALLBACK_FUNCTION); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 631 | |
| 632 | if (!mm_timer) { |
Stefan Weil | 52ef651 | 2012-05-08 19:14:43 +0200 | [diff] [blame] | 633 | fprintf(stderr, "Failed to initialize win32 alarm timer\n"); |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 634 | timeEndPeriod(mm_tc.wPeriodMin); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static void mm_stop_timer(struct qemu_alarm_timer *t) |
| 642 | { |
| 643 | timeKillEvent(mm_timer); |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 644 | timeEndPeriod(mm_tc.wPeriodMin); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 647 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta) |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 648 | { |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 649 | int64_t nearest_delta_ms = delta / 1000000; |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 650 | if (nearest_delta_ms < mm_tc.wPeriodMin) { |
| 651 | nearest_delta_ms = mm_tc.wPeriodMin; |
| 652 | } else if (nearest_delta_ms > mm_tc.wPeriodMax) { |
| 653 | nearest_delta_ms = mm_tc.wPeriodMax; |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 654 | } |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 655 | |
| 656 | timeKillEvent(mm_timer); |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 657 | mm_timer = timeSetEvent((UINT)nearest_delta_ms, |
| 658 | mm_tc.wPeriodMin, |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 659 | mm_alarm_handler, |
| 660 | (DWORD_PTR)t, |
| 661 | TIME_ONESHOT | TIME_CALLBACK_FUNCTION); |
| 662 | |
| 663 | if (!mm_timer) { |
Stefan Weil | 52ef651 | 2012-05-08 19:14:43 +0200 | [diff] [blame] | 664 | fprintf(stderr, "Failed to re-arm win32 alarm timer\n"); |
Stefan Weil | 40f08e8 | 2012-04-27 05:34:40 +0000 | [diff] [blame] | 665 | timeEndPeriod(mm_tc.wPeriodMin); |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 666 | exit(1); |
| 667 | } |
| 668 | } |
| 669 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 670 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 671 | { |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 672 | HANDLE hTimer; |
| 673 | BOOLEAN success; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 674 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 675 | /* If you call ChangeTimerQueueTimer on a one-shot timer (its period |
| 676 | is zero) that has already expired, the timer is not updated. Since |
| 677 | creating a new timer is relatively expensive, set a bogus one-hour |
| 678 | interval in the dynticks case. */ |
| 679 | success = CreateTimerQueueTimer(&hTimer, |
| 680 | NULL, |
| 681 | host_alarm_handler, |
| 682 | t, |
| 683 | 1, |
Stefan Weil | 8205199 | 2012-04-20 11:27:24 +0200 | [diff] [blame] | 684 | 3600000, |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 685 | WT_EXECUTEINTIMERTHREAD); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 686 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 687 | if (!success) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 688 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
| 689 | GetLastError()); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 690 | return -1; |
| 691 | } |
| 692 | |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 693 | t->timer = hTimer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 698 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 699 | HANDLE hTimer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 700 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 701 | if (hTimer) { |
| 702 | DeleteTimerQueueTimer(NULL, hTimer, NULL); |
| 703 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 704 | } |
| 705 | |
Paolo Bonzini | f3fc6e2 | 2011-03-14 09:45:38 +0100 | [diff] [blame] | 706 | static void win32_rearm_timer(struct qemu_alarm_timer *t, |
| 707 | int64_t nearest_delta_ns) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 708 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 709 | HANDLE hTimer = t->timer; |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 710 | int64_t nearest_delta_ms; |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 711 | BOOLEAN success; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 712 | |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 713 | nearest_delta_ms = nearest_delta_ns / 1000000; |
Paolo Bonzini | cfced5b | 2011-03-12 17:43:49 +0100 | [diff] [blame] | 714 | if (nearest_delta_ms < 1) { |
| 715 | nearest_delta_ms = 1; |
| 716 | } |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 717 | /* ULONG_MAX can be 32 bit */ |
| 718 | if (nearest_delta_ms > ULONG_MAX) { |
| 719 | nearest_delta_ms = ULONG_MAX; |
| 720 | } |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 721 | success = ChangeTimerQueueTimer(NULL, |
| 722 | hTimer, |
Stefano Stabellini | 5bfb723 | 2012-04-13 19:35:02 +0100 | [diff] [blame] | 723 | (unsigned long) nearest_delta_ms, |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 724 | 3600000); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 725 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 726 | if (!success) { |
| 727 | fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n", |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 728 | GetLastError()); |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 729 | exit(-1); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 730 | } |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 731 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | #endif /* _WIN32 */ |
| 735 | |
Paolo Bonzini | 4260a73 | 2011-09-19 10:18:51 +0200 | [diff] [blame] | 736 | static void quit_timers(void) |
| 737 | { |
| 738 | struct qemu_alarm_timer *t = alarm_timer; |
| 739 | alarm_timer = NULL; |
| 740 | t->stop(t); |
| 741 | } |
| 742 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 743 | int init_timer_alarm(void) |
| 744 | { |
| 745 | struct qemu_alarm_timer *t = NULL; |
| 746 | int i, err = -1; |
| 747 | |
| 748 | for (i = 0; alarm_timers[i].name; i++) { |
| 749 | t = &alarm_timers[i]; |
| 750 | |
| 751 | err = t->start(t); |
| 752 | if (!err) |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | if (err) { |
| 757 | err = -ENOENT; |
| 758 | goto fail; |
| 759 | } |
| 760 | |
Paolo Bonzini | 4260a73 | 2011-09-19 10:18:51 +0200 | [diff] [blame] | 761 | atexit(quit_timers); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 762 | alarm_timer = t; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 763 | return 0; |
| 764 | |
| 765 | fail: |
| 766 | return err; |
| 767 | } |
| 768 | |