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 | |
| 32 | #include <unistd.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <time.h> |
| 35 | #include <errno.h> |
| 36 | #include <sys/time.h> |
| 37 | #include <signal.h> |
Juergen Lock | 4445934 | 2010-03-25 22:35:03 +0100 | [diff] [blame] | 38 | #ifdef __FreeBSD__ |
| 39 | #include <sys/param.h> |
| 40 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 41 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 42 | #ifdef _WIN32 |
| 43 | #include <windows.h> |
| 44 | #include <mmsystem.h> |
| 45 | #endif |
| 46 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 47 | #include "qemu-timer.h" |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 48 | |
| 49 | /* Conversion factor from emulated instructions to virtual clock ticks. */ |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 50 | int icount_time_shift; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 51 | /* Arbitrarily pick 1MIPS as the minimum allowable speed. */ |
| 52 | #define MAX_ICOUNT_SHIFT 10 |
| 53 | /* Compensate for varying guest execution speed. */ |
Blue Swirl | 29e922b | 2010-03-29 19:24:00 +0000 | [diff] [blame] | 54 | int64_t qemu_icount_bias; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 55 | static QEMUTimer *icount_rt_timer; |
| 56 | static QEMUTimer *icount_vm_timer; |
| 57 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 58 | /***********************************************************/ |
| 59 | /* guest cycle counter */ |
| 60 | |
| 61 | typedef struct TimersState { |
| 62 | int64_t cpu_ticks_prev; |
| 63 | int64_t cpu_ticks_offset; |
| 64 | int64_t cpu_clock_offset; |
| 65 | int32_t cpu_ticks_enabled; |
| 66 | int64_t dummy; |
| 67 | } TimersState; |
| 68 | |
| 69 | TimersState timers_state; |
| 70 | |
| 71 | /* return the host CPU cycle counter and handle stop/restart */ |
| 72 | int64_t cpu_get_ticks(void) |
| 73 | { |
| 74 | if (use_icount) { |
| 75 | return cpu_get_icount(); |
| 76 | } |
| 77 | if (!timers_state.cpu_ticks_enabled) { |
| 78 | return timers_state.cpu_ticks_offset; |
| 79 | } else { |
| 80 | int64_t ticks; |
| 81 | ticks = cpu_get_real_ticks(); |
| 82 | if (timers_state.cpu_ticks_prev > ticks) { |
| 83 | /* Note: non increasing ticks may happen if the host uses |
| 84 | software suspend */ |
| 85 | timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks; |
| 86 | } |
| 87 | timers_state.cpu_ticks_prev = ticks; |
| 88 | return ticks + timers_state.cpu_ticks_offset; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* return the host CPU monotonic timer and handle stop/restart */ |
| 93 | static int64_t cpu_get_clock(void) |
| 94 | { |
| 95 | int64_t ti; |
| 96 | if (!timers_state.cpu_ticks_enabled) { |
| 97 | return timers_state.cpu_clock_offset; |
| 98 | } else { |
| 99 | ti = get_clock(); |
| 100 | return ti + timers_state.cpu_clock_offset; |
| 101 | } |
| 102 | } |
| 103 | |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 104 | #ifndef CONFIG_IOTHREAD |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 105 | static int64_t qemu_icount_delta(void) |
| 106 | { |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 107 | if (!use_icount) { |
| 108 | return 5000 * (int64_t) 1000000; |
| 109 | } else if (use_icount == 1) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 110 | /* When not using an adaptive execution frequency |
| 111 | we tend to get badly out of sync with real time, |
| 112 | so just delay for a reasonable amount of time. */ |
| 113 | return 0; |
| 114 | } else { |
| 115 | return cpu_get_icount() - cpu_get_clock(); |
| 116 | } |
| 117 | } |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 118 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 119 | |
| 120 | /* enable cpu_get_ticks() */ |
| 121 | void cpu_enable_ticks(void) |
| 122 | { |
| 123 | if (!timers_state.cpu_ticks_enabled) { |
| 124 | timers_state.cpu_ticks_offset -= cpu_get_real_ticks(); |
| 125 | timers_state.cpu_clock_offset -= get_clock(); |
| 126 | timers_state.cpu_ticks_enabled = 1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* disable cpu_get_ticks() : the clock is stopped. You must not call |
| 131 | cpu_get_ticks() after that. */ |
| 132 | void cpu_disable_ticks(void) |
| 133 | { |
| 134 | if (timers_state.cpu_ticks_enabled) { |
| 135 | timers_state.cpu_ticks_offset = cpu_get_ticks(); |
| 136 | timers_state.cpu_clock_offset = cpu_get_clock(); |
| 137 | timers_state.cpu_ticks_enabled = 0; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /***********************************************************/ |
| 142 | /* timers */ |
| 143 | |
| 144 | #define QEMU_CLOCK_REALTIME 0 |
| 145 | #define QEMU_CLOCK_VIRTUAL 1 |
| 146 | #define QEMU_CLOCK_HOST 2 |
| 147 | |
| 148 | struct QEMUClock { |
| 149 | int type; |
| 150 | int enabled; |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 151 | |
| 152 | QEMUTimer *warp_timer; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 153 | |
| 154 | NotifierList reset_notifiers; |
| 155 | int64_t last; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | struct QEMUTimer { |
| 159 | QEMUClock *clock; |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 160 | int64_t expire_time; /* in nanoseconds */ |
| 161 | int scale; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 162 | QEMUTimerCB *cb; |
| 163 | void *opaque; |
| 164 | struct QEMUTimer *next; |
| 165 | }; |
| 166 | |
| 167 | struct qemu_alarm_timer { |
| 168 | char const *name; |
| 169 | int (*start)(struct qemu_alarm_timer *t); |
| 170 | void (*stop)(struct qemu_alarm_timer *t); |
| 171 | void (*rearm)(struct qemu_alarm_timer *t); |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 172 | #if defined(__linux__) |
| 173 | int fd; |
| 174 | timer_t timer; |
| 175 | #elif defined(_WIN32) |
| 176 | HANDLE timer; |
| 177 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 178 | char expired; |
| 179 | char pending; |
| 180 | }; |
| 181 | |
| 182 | static struct qemu_alarm_timer *alarm_timer; |
| 183 | |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 184 | static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
| 185 | { |
| 186 | return timer_head && (timer_head->expire_time <= current_time); |
| 187 | } |
| 188 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 189 | int qemu_alarm_pending(void) |
| 190 | { |
| 191 | return alarm_timer->pending; |
| 192 | } |
| 193 | |
| 194 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
| 195 | { |
| 196 | return !!t->rearm; |
| 197 | } |
| 198 | |
| 199 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
| 200 | { |
| 201 | if (!alarm_has_dynticks(t)) |
| 202 | return; |
| 203 | |
| 204 | t->rearm(t); |
| 205 | } |
| 206 | |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 207 | /* TODO: MIN_TIMER_REARM_NS should be optimized */ |
| 208 | #define MIN_TIMER_REARM_NS 250000 |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 209 | |
| 210 | #ifdef _WIN32 |
| 211 | |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 212 | static int mm_start_timer(struct qemu_alarm_timer *t); |
| 213 | static void mm_stop_timer(struct qemu_alarm_timer *t); |
| 214 | static void mm_rearm_timer(struct qemu_alarm_timer *t); |
| 215 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 216 | static int win32_start_timer(struct qemu_alarm_timer *t); |
| 217 | static void win32_stop_timer(struct qemu_alarm_timer *t); |
| 218 | static void win32_rearm_timer(struct qemu_alarm_timer *t); |
| 219 | |
| 220 | #else |
| 221 | |
| 222 | static int unix_start_timer(struct qemu_alarm_timer *t); |
| 223 | static void unix_stop_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 224 | static void unix_rearm_timer(struct qemu_alarm_timer *t); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 225 | |
| 226 | #ifdef __linux__ |
| 227 | |
| 228 | static int dynticks_start_timer(struct qemu_alarm_timer *t); |
| 229 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
| 230 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
| 231 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 232 | #endif /* __linux__ */ |
| 233 | |
| 234 | #endif /* _WIN32 */ |
| 235 | |
| 236 | /* Correlation between real and virtual time is always going to be |
| 237 | fairly approximate, so ignore small variation. |
| 238 | When the guest is idle real and virtual time will be aligned in |
| 239 | the IO wait loop. */ |
| 240 | #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10) |
| 241 | |
| 242 | static void icount_adjust(void) |
| 243 | { |
| 244 | int64_t cur_time; |
| 245 | int64_t cur_icount; |
| 246 | int64_t delta; |
| 247 | static int64_t last_delta; |
| 248 | /* If the VM is not running, then do nothing. */ |
| 249 | if (!vm_running) |
| 250 | return; |
| 251 | |
| 252 | cur_time = cpu_get_clock(); |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 253 | cur_icount = qemu_get_clock_ns(vm_clock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 254 | delta = cur_icount - cur_time; |
| 255 | /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */ |
| 256 | if (delta > 0 |
| 257 | && last_delta + ICOUNT_WOBBLE < delta * 2 |
| 258 | && icount_time_shift > 0) { |
| 259 | /* The guest is getting too far ahead. Slow time down. */ |
| 260 | icount_time_shift--; |
| 261 | } |
| 262 | if (delta < 0 |
| 263 | && last_delta - ICOUNT_WOBBLE > delta * 2 |
| 264 | && icount_time_shift < MAX_ICOUNT_SHIFT) { |
| 265 | /* The guest is getting too far behind. Speed time up. */ |
| 266 | icount_time_shift++; |
| 267 | } |
| 268 | last_delta = delta; |
| 269 | qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
| 270 | } |
| 271 | |
| 272 | static void icount_adjust_rt(void * opaque) |
| 273 | { |
| 274 | qemu_mod_timer(icount_rt_timer, |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 275 | qemu_get_clock_ms(rt_clock) + 1000); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 276 | icount_adjust(); |
| 277 | } |
| 278 | |
| 279 | static void icount_adjust_vm(void * opaque) |
| 280 | { |
| 281 | qemu_mod_timer(icount_vm_timer, |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 282 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 283 | icount_adjust(); |
| 284 | } |
| 285 | |
| 286 | int64_t qemu_icount_round(int64_t count) |
| 287 | { |
| 288 | return (count + (1 << icount_time_shift) - 1) >> icount_time_shift; |
| 289 | } |
| 290 | |
| 291 | static struct qemu_alarm_timer alarm_timers[] = { |
| 292 | #ifndef _WIN32 |
| 293 | #ifdef __linux__ |
| 294 | {"dynticks", dynticks_start_timer, |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 295 | dynticks_stop_timer, dynticks_rearm_timer}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 296 | #endif |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 297 | {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 298 | #else |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 299 | {"mmtimer", mm_start_timer, mm_stop_timer, NULL}, |
| 300 | {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer}, |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 301 | {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer}, |
| 302 | {"win32", win32_start_timer, win32_stop_timer, NULL}, |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 303 | #endif |
| 304 | {NULL, } |
| 305 | }; |
| 306 | |
| 307 | static void show_available_alarms(void) |
| 308 | { |
| 309 | int i; |
| 310 | |
| 311 | printf("Available alarm timers, in order of precedence:\n"); |
| 312 | for (i = 0; alarm_timers[i].name; i++) |
| 313 | printf("%s\n", alarm_timers[i].name); |
| 314 | } |
| 315 | |
| 316 | void configure_alarms(char const *opt) |
| 317 | { |
| 318 | int i; |
| 319 | int cur = 0; |
| 320 | int count = ARRAY_SIZE(alarm_timers) - 1; |
| 321 | char *arg; |
| 322 | char *name; |
| 323 | struct qemu_alarm_timer tmp; |
| 324 | |
| 325 | if (!strcmp(opt, "?")) { |
| 326 | show_available_alarms(); |
| 327 | exit(0); |
| 328 | } |
| 329 | |
| 330 | arg = qemu_strdup(opt); |
| 331 | |
| 332 | /* Reorder the array */ |
| 333 | name = strtok(arg, ","); |
| 334 | while (name) { |
| 335 | for (i = 0; i < count && alarm_timers[i].name; i++) { |
| 336 | if (!strcmp(alarm_timers[i].name, name)) |
| 337 | break; |
| 338 | } |
| 339 | |
| 340 | if (i == count) { |
| 341 | fprintf(stderr, "Unknown clock %s\n", name); |
| 342 | goto next; |
| 343 | } |
| 344 | |
| 345 | if (i < cur) |
| 346 | /* Ignore */ |
| 347 | goto next; |
| 348 | |
| 349 | /* Swap */ |
| 350 | tmp = alarm_timers[i]; |
| 351 | alarm_timers[i] = alarm_timers[cur]; |
| 352 | alarm_timers[cur] = tmp; |
| 353 | |
| 354 | cur++; |
| 355 | next: |
| 356 | name = strtok(NULL, ","); |
| 357 | } |
| 358 | |
| 359 | qemu_free(arg); |
| 360 | |
| 361 | if (cur) { |
| 362 | /* Disable remaining timers */ |
| 363 | for (i = cur; i < count; i++) |
| 364 | alarm_timers[i].name = NULL; |
| 365 | } else { |
| 366 | show_available_alarms(); |
| 367 | exit(1); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | #define QEMU_NUM_CLOCKS 3 |
| 372 | |
| 373 | QEMUClock *rt_clock; |
| 374 | QEMUClock *vm_clock; |
| 375 | QEMUClock *host_clock; |
| 376 | |
| 377 | static QEMUTimer *active_timers[QEMU_NUM_CLOCKS]; |
| 378 | |
| 379 | static QEMUClock *qemu_new_clock(int type) |
| 380 | { |
| 381 | QEMUClock *clock; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 382 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 383 | clock = qemu_mallocz(sizeof(QEMUClock)); |
| 384 | clock->type = type; |
| 385 | clock->enabled = 1; |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 386 | notifier_list_init(&clock->reset_notifiers); |
| 387 | /* required to detect & report backward jumps */ |
| 388 | if (type == QEMU_CLOCK_HOST) { |
| 389 | clock->last = get_clock_realtime(); |
| 390 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 391 | return clock; |
| 392 | } |
| 393 | |
| 394 | void qemu_clock_enable(QEMUClock *clock, int enabled) |
| 395 | { |
| 396 | clock->enabled = enabled; |
| 397 | } |
| 398 | |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 399 | static int64_t vm_clock_warp_start; |
| 400 | |
| 401 | static void icount_warp_rt(void *opaque) |
| 402 | { |
| 403 | if (vm_clock_warp_start == -1) { |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (vm_running) { |
| 408 | int64_t clock = qemu_get_clock_ns(rt_clock); |
| 409 | int64_t warp_delta = clock - vm_clock_warp_start; |
| 410 | if (use_icount == 1) { |
| 411 | qemu_icount_bias += warp_delta; |
| 412 | } else { |
| 413 | /* |
| 414 | * In adaptive mode, do not let the vm_clock run too |
| 415 | * far ahead of real time. |
| 416 | */ |
| 417 | int64_t cur_time = cpu_get_clock(); |
| 418 | int64_t cur_icount = qemu_get_clock_ns(vm_clock); |
| 419 | int64_t delta = cur_time - cur_icount; |
| 420 | qemu_icount_bias += MIN(warp_delta, delta); |
| 421 | } |
| 422 | if (qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL], |
| 423 | qemu_get_clock_ns(vm_clock))) { |
| 424 | qemu_notify_event(); |
| 425 | } |
| 426 | } |
| 427 | vm_clock_warp_start = -1; |
| 428 | } |
| 429 | |
| 430 | void qemu_clock_warp(QEMUClock *clock) |
| 431 | { |
| 432 | int64_t deadline; |
| 433 | |
| 434 | if (!clock->warp_timer) { |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * There are too many global variables to make the "warp" behavior |
| 440 | * applicable to other clocks. But a clock argument removes the |
| 441 | * need for if statements all over the place. |
| 442 | */ |
| 443 | assert(clock == vm_clock); |
| 444 | |
| 445 | /* |
| 446 | * If the CPUs have been sleeping, advance the vm_clock timer now. This |
| 447 | * ensures that the deadline for the timer is computed correctly below. |
| 448 | * This also makes sure that the insn counter is synchronized before the |
| 449 | * CPU starts running, in case the CPU is woken by an event other than |
| 450 | * the earliest vm_clock timer. |
| 451 | */ |
| 452 | icount_warp_rt(NULL); |
| 453 | if (!all_cpu_threads_idle() || !active_timers[clock->type]) { |
| 454 | qemu_del_timer(clock->warp_timer); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | vm_clock_warp_start = qemu_get_clock_ns(rt_clock); |
Paolo Bonzini | cb842c9 | 2011-04-13 10:03:46 +0200 | [diff] [blame] | 459 | deadline = qemu_next_icount_deadline(); |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 460 | if (deadline > 0) { |
| 461 | /* |
| 462 | * Ensure the vm_clock proceeds even when the virtual CPU goes to |
| 463 | * sleep. Otherwise, the CPU might be waiting for a future timer |
| 464 | * interrupt to wake it up, but the interrupt never comes because |
| 465 | * the vCPU isn't running any insns and thus doesn't advance the |
| 466 | * vm_clock. |
| 467 | * |
| 468 | * An extreme solution for this problem would be to never let VCPUs |
| 469 | * sleep in icount mode if there is a pending vm_clock timer; rather |
| 470 | * time could just advance to the next vm_clock event. Instead, we |
| 471 | * do stop VCPUs and only advance vm_clock after some "real" time, |
| 472 | * (related to the time left until the next event) has passed. This |
| 473 | * rt_clock timer will do this. This avoids that the warps are too |
| 474 | * visible externally---for example, you will not be sending network |
| 475 | * packets continously instead of every 100ms. |
| 476 | */ |
| 477 | qemu_mod_timer(clock->warp_timer, vm_clock_warp_start + deadline); |
| 478 | } else { |
| 479 | qemu_notify_event(); |
| 480 | } |
| 481 | } |
| 482 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 483 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, |
| 484 | QEMUTimerCB *cb, void *opaque) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 485 | { |
| 486 | QEMUTimer *ts; |
| 487 | |
| 488 | ts = qemu_mallocz(sizeof(QEMUTimer)); |
| 489 | ts->clock = clock; |
| 490 | ts->cb = cb; |
| 491 | ts->opaque = opaque; |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 492 | ts->scale = scale; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 493 | return ts; |
| 494 | } |
| 495 | |
| 496 | void qemu_free_timer(QEMUTimer *ts) |
| 497 | { |
| 498 | qemu_free(ts); |
| 499 | } |
| 500 | |
| 501 | /* stop a timer, but do not dealloc it */ |
| 502 | void qemu_del_timer(QEMUTimer *ts) |
| 503 | { |
| 504 | QEMUTimer **pt, *t; |
| 505 | |
| 506 | /* NOTE: this code must be signal safe because |
| 507 | qemu_timer_expired() can be called from a signal. */ |
| 508 | pt = &active_timers[ts->clock->type]; |
| 509 | for(;;) { |
| 510 | t = *pt; |
| 511 | if (!t) |
| 512 | break; |
| 513 | if (t == ts) { |
| 514 | *pt = t->next; |
| 515 | break; |
| 516 | } |
| 517 | pt = &t->next; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* modify the current timer so that it will be fired when current_time |
| 522 | >= expire_time. The corresponding callback will be called. */ |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 523 | static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 524 | { |
| 525 | QEMUTimer **pt, *t; |
| 526 | |
| 527 | qemu_del_timer(ts); |
| 528 | |
| 529 | /* add the timer in the sorted list */ |
| 530 | /* NOTE: this code must be signal safe because |
| 531 | qemu_timer_expired() can be called from a signal. */ |
| 532 | pt = &active_timers[ts->clock->type]; |
| 533 | for(;;) { |
| 534 | t = *pt; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 535 | if (!qemu_timer_expired_ns(t, expire_time)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 536 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 537 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 538 | pt = &t->next; |
| 539 | } |
| 540 | ts->expire_time = expire_time; |
| 541 | ts->next = *pt; |
| 542 | *pt = ts; |
| 543 | |
| 544 | /* Rearm if necessary */ |
| 545 | if (pt == &active_timers[ts->clock->type]) { |
| 546 | if (!alarm_timer->pending) { |
| 547 | qemu_rearm_alarm_timer(alarm_timer); |
| 548 | } |
| 549 | /* Interrupt execution to force deadline recalculation. */ |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 550 | qemu_clock_warp(ts->clock); |
| 551 | if (use_icount) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 552 | qemu_notify_event(); |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 553 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 557 | /* modify the current timer so that it will be fired when current_time |
| 558 | >= expire_time. The corresponding callback will be called. */ |
| 559 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
| 560 | { |
| 561 | qemu_mod_timer_ns(ts, expire_time * ts->scale); |
| 562 | } |
| 563 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 564 | int qemu_timer_pending(QEMUTimer *ts) |
| 565 | { |
| 566 | QEMUTimer *t; |
| 567 | for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
| 568 | if (t == ts) |
| 569 | return 1; |
| 570 | } |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
| 575 | { |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 576 | return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static void qemu_run_timers(QEMUClock *clock) |
| 580 | { |
| 581 | QEMUTimer **ptimer_head, *ts; |
| 582 | int64_t current_time; |
| 583 | |
| 584 | if (!clock->enabled) |
| 585 | return; |
| 586 | |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 587 | current_time = qemu_get_clock_ns(clock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 588 | ptimer_head = &active_timers[clock->type]; |
| 589 | for(;;) { |
| 590 | ts = *ptimer_head; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 591 | if (!qemu_timer_expired_ns(ts, current_time)) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 592 | break; |
Stefan Weil | 45c7b37 | 2011-03-24 21:31:24 +0100 | [diff] [blame] | 593 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 594 | /* remove timer from the list before calling the callback */ |
| 595 | *ptimer_head = ts->next; |
| 596 | ts->next = NULL; |
| 597 | |
| 598 | /* run the callback (the timer list can be modified) */ |
| 599 | ts->cb(ts->opaque); |
| 600 | } |
| 601 | } |
| 602 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 603 | int64_t qemu_get_clock_ns(QEMUClock *clock) |
| 604 | { |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 605 | int64_t now, last; |
| 606 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 607 | switch(clock->type) { |
| 608 | case QEMU_CLOCK_REALTIME: |
| 609 | return get_clock(); |
| 610 | default: |
| 611 | case QEMU_CLOCK_VIRTUAL: |
| 612 | if (use_icount) { |
| 613 | return cpu_get_icount(); |
| 614 | } else { |
| 615 | return cpu_get_clock(); |
| 616 | } |
| 617 | case QEMU_CLOCK_HOST: |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 618 | now = get_clock_realtime(); |
| 619 | last = clock->last; |
| 620 | clock->last = now; |
| 621 | if (now < last) { |
| 622 | notifier_list_notify(&clock->reset_notifiers, &now); |
| 623 | } |
| 624 | return now; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Jan Kiszka | 691a0c9 | 2011-06-20 14:06:27 +0200 | [diff] [blame^] | 628 | void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) |
| 629 | { |
| 630 | notifier_list_add(&clock->reset_notifiers, notifier); |
| 631 | } |
| 632 | |
| 633 | void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) |
| 634 | { |
| 635 | notifier_list_remove(&clock->reset_notifiers, notifier); |
| 636 | } |
| 637 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 638 | void init_clocks(void) |
| 639 | { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 640 | rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
| 641 | vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
| 642 | host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
| 643 | |
| 644 | rtc_clock = host_clock; |
| 645 | } |
| 646 | |
| 647 | /* save a timer */ |
| 648 | void qemu_put_timer(QEMUFile *f, QEMUTimer *ts) |
| 649 | { |
| 650 | uint64_t expire_time; |
| 651 | |
| 652 | if (qemu_timer_pending(ts)) { |
| 653 | expire_time = ts->expire_time; |
| 654 | } else { |
| 655 | expire_time = -1; |
| 656 | } |
| 657 | qemu_put_be64(f, expire_time); |
| 658 | } |
| 659 | |
| 660 | void qemu_get_timer(QEMUFile *f, QEMUTimer *ts) |
| 661 | { |
| 662 | uint64_t expire_time; |
| 663 | |
| 664 | expire_time = qemu_get_be64(f); |
| 665 | if (expire_time != -1) { |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 666 | qemu_mod_timer_ns(ts, expire_time); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 667 | } else { |
| 668 | qemu_del_timer(ts); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | static const VMStateDescription vmstate_timers = { |
| 673 | .name = "timer", |
| 674 | .version_id = 2, |
| 675 | .minimum_version_id = 1, |
| 676 | .minimum_version_id_old = 1, |
| 677 | .fields = (VMStateField []) { |
| 678 | VMSTATE_INT64(cpu_ticks_offset, TimersState), |
| 679 | VMSTATE_INT64(dummy, TimersState), |
| 680 | VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2), |
| 681 | VMSTATE_END_OF_LIST() |
| 682 | } |
| 683 | }; |
| 684 | |
| 685 | void configure_icount(const char *option) |
| 686 | { |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 687 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 688 | if (!option) |
| 689 | return; |
| 690 | |
Paolo Bonzini | ab33fcd | 2011-04-13 10:03:44 +0200 | [diff] [blame] | 691 | #ifdef CONFIG_IOTHREAD |
| 692 | vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL); |
| 693 | #endif |
| 694 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 695 | if (strcmp(option, "auto") != 0) { |
| 696 | icount_time_shift = strtol(option, NULL, 0); |
| 697 | use_icount = 1; |
| 698 | return; |
| 699 | } |
| 700 | |
| 701 | use_icount = 2; |
| 702 | |
| 703 | /* 125MIPS seems a reasonable initial guess at the guest speed. |
| 704 | It will be corrected fairly quickly anyway. */ |
| 705 | icount_time_shift = 3; |
| 706 | |
| 707 | /* Have both realtime and virtual time triggers for speed adjustment. |
| 708 | The realtime trigger catches emulated time passing too slowly, |
| 709 | the virtual time trigger catches emulated time passing too fast. |
| 710 | Realtime triggers occur even when idle, so use them less frequently |
| 711 | than VM triggers. */ |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 712 | icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 713 | qemu_mod_timer(icount_rt_timer, |
Paolo Bonzini | 7bd427d | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 714 | qemu_get_clock_ms(rt_clock) + 1000); |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 715 | icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 716 | qemu_mod_timer(icount_vm_timer, |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 717 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void qemu_run_all_timers(void) |
| 721 | { |
Paolo Bonzini | ca5a2a4 | 2010-03-19 11:30:35 +0100 | [diff] [blame] | 722 | alarm_timer->pending = 0; |
| 723 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 724 | /* rearm timer, if not periodic */ |
| 725 | if (alarm_timer->expired) { |
| 726 | alarm_timer->expired = 0; |
| 727 | qemu_rearm_alarm_timer(alarm_timer); |
| 728 | } |
| 729 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 730 | /* vm time timers */ |
| 731 | if (vm_running) { |
| 732 | qemu_run_timers(vm_clock); |
| 733 | } |
| 734 | |
| 735 | qemu_run_timers(rt_clock); |
| 736 | qemu_run_timers(host_clock); |
| 737 | } |
| 738 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 739 | static int64_t qemu_next_alarm_deadline(void); |
| 740 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 741 | #ifdef _WIN32 |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 742 | static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 743 | #else |
| 744 | static void host_alarm_handler(int host_signum) |
| 745 | #endif |
| 746 | { |
| 747 | struct qemu_alarm_timer *t = alarm_timer; |
| 748 | if (!t) |
| 749 | return; |
| 750 | |
| 751 | #if 0 |
| 752 | #define DISP_FREQ 1000 |
| 753 | { |
| 754 | static int64_t delta_min = INT64_MAX; |
| 755 | static int64_t delta_max, delta_cum, last_clock, delta, ti; |
| 756 | static int count; |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 757 | ti = qemu_get_clock_ns(vm_clock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 758 | if (last_clock != 0) { |
| 759 | delta = ti - last_clock; |
| 760 | if (delta < delta_min) |
| 761 | delta_min = delta; |
| 762 | if (delta > delta_max) |
| 763 | delta_max = delta; |
| 764 | delta_cum += delta; |
| 765 | if (++count == DISP_FREQ) { |
| 766 | printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n", |
| 767 | muldiv64(delta_min, 1000000, get_ticks_per_sec()), |
| 768 | muldiv64(delta_max, 1000000, get_ticks_per_sec()), |
| 769 | muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()), |
| 770 | (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ)); |
| 771 | count = 0; |
| 772 | delta_min = INT64_MAX; |
| 773 | delta_max = 0; |
| 774 | delta_cum = 0; |
| 775 | } |
| 776 | } |
| 777 | last_clock = ti; |
| 778 | } |
| 779 | #endif |
| 780 | if (alarm_has_dynticks(t) || |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 781 | qemu_next_alarm_deadline () <= 0) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 782 | t->expired = alarm_has_dynticks(t); |
| 783 | t->pending = 1; |
| 784 | qemu_notify_event(); |
| 785 | } |
| 786 | } |
| 787 | |
Paolo Bonzini | cb842c9 | 2011-04-13 10:03:46 +0200 | [diff] [blame] | 788 | int64_t qemu_next_icount_deadline(void) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 789 | { |
| 790 | /* To avoid problems with overflow limit this to 2^32. */ |
| 791 | int64_t delta = INT32_MAX; |
| 792 | |
Paolo Bonzini | cb842c9 | 2011-04-13 10:03:46 +0200 | [diff] [blame] | 793 | assert(use_icount); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 794 | if (active_timers[QEMU_CLOCK_VIRTUAL]) { |
| 795 | delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 796 | qemu_get_clock_ns(vm_clock); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 797 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 798 | |
| 799 | if (delta < 0) |
| 800 | delta = 0; |
| 801 | |
| 802 | return delta; |
| 803 | } |
| 804 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 805 | static int64_t qemu_next_alarm_deadline(void) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 806 | { |
| 807 | int64_t delta; |
| 808 | int64_t rtdelta; |
| 809 | |
Paolo Bonzini | 6ad0a1e | 2011-02-03 14:49:00 +0100 | [diff] [blame] | 810 | if (!use_icount && active_timers[QEMU_CLOCK_VIRTUAL]) { |
| 811 | delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time - |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 812 | qemu_get_clock_ns(vm_clock); |
Paolo Bonzini | 6ad0a1e | 2011-02-03 14:49:00 +0100 | [diff] [blame] | 813 | } else { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 814 | delta = INT32_MAX; |
Paolo Bonzini | 6ad0a1e | 2011-02-03 14:49:00 +0100 | [diff] [blame] | 815 | } |
| 816 | if (active_timers[QEMU_CLOCK_HOST]) { |
| 817 | int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time - |
| 818 | qemu_get_clock_ns(host_clock); |
| 819 | if (hdelta < delta) |
| 820 | delta = hdelta; |
| 821 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 822 | if (active_timers[QEMU_CLOCK_REALTIME]) { |
Paolo Bonzini | 4a99874 | 2011-03-11 16:33:58 +0100 | [diff] [blame] | 823 | rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time - |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 824 | qemu_get_clock_ns(rt_clock)); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 825 | if (rtdelta < delta) |
| 826 | delta = rtdelta; |
| 827 | } |
| 828 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 829 | return delta; |
| 830 | } |
| 831 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 832 | #if defined(__linux__) |
| 833 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 834 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
| 835 | { |
| 836 | struct sigevent ev; |
| 837 | timer_t host_timer; |
| 838 | struct sigaction act; |
| 839 | |
| 840 | sigfillset(&act.sa_mask); |
| 841 | act.sa_flags = 0; |
| 842 | act.sa_handler = host_alarm_handler; |
| 843 | |
| 844 | sigaction(SIGALRM, &act, NULL); |
| 845 | |
| 846 | /* |
| 847 | * Initialize ev struct to 0 to avoid valgrind complaining |
| 848 | * about uninitialized data in timer_create call |
| 849 | */ |
| 850 | memset(&ev, 0, sizeof(ev)); |
| 851 | ev.sigev_value.sival_int = 0; |
| 852 | ev.sigev_notify = SIGEV_SIGNAL; |
| 853 | ev.sigev_signo = SIGALRM; |
| 854 | |
| 855 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { |
| 856 | perror("timer_create"); |
| 857 | |
| 858 | /* disable dynticks */ |
| 859 | fprintf(stderr, "Dynamic Ticks disabled\n"); |
| 860 | |
| 861 | return -1; |
| 862 | } |
| 863 | |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 864 | t->timer = host_timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
| 870 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 871 | timer_t host_timer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 872 | |
| 873 | timer_delete(host_timer); |
| 874 | } |
| 875 | |
| 876 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
| 877 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 878 | timer_t host_timer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 879 | struct itimerspec timeout; |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 880 | int64_t nearest_delta_ns = INT64_MAX; |
| 881 | int64_t current_ns; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 882 | |
| 883 | assert(alarm_has_dynticks(t)); |
| 884 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
| 885 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 886 | !active_timers[QEMU_CLOCK_HOST]) |
| 887 | return; |
| 888 | |
Paolo Bonzini | 4c3d45e | 2011-02-03 14:49:01 +0100 | [diff] [blame] | 889 | nearest_delta_ns = qemu_next_alarm_deadline(); |
| 890 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
| 891 | nearest_delta_ns = MIN_TIMER_REARM_NS; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 892 | |
| 893 | /* check whether a timer is already running */ |
| 894 | if (timer_gettime(host_timer, &timeout)) { |
| 895 | perror("gettime"); |
| 896 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 897 | exit(1); |
| 898 | } |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 899 | current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; |
| 900 | if (current_ns && current_ns <= nearest_delta_ns) |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 901 | return; |
| 902 | |
| 903 | timeout.it_interval.tv_sec = 0; |
| 904 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
Paolo Bonzini | 9c13246 | 2011-02-03 14:48:59 +0100 | [diff] [blame] | 905 | timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; |
| 906 | timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 907 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
| 908 | perror("settime"); |
| 909 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 910 | exit(1); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | #endif /* defined(__linux__) */ |
| 915 | |
Stefan Weil | f26e5a5 | 2011-02-04 22:01:32 +0100 | [diff] [blame] | 916 | #if !defined(_WIN32) |
| 917 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 918 | static int unix_start_timer(struct qemu_alarm_timer *t) |
| 919 | { |
| 920 | struct sigaction act; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 921 | |
| 922 | /* timer signal */ |
| 923 | sigfillset(&act.sa_mask); |
| 924 | act.sa_flags = 0; |
| 925 | act.sa_handler = host_alarm_handler; |
| 926 | |
| 927 | sigaction(SIGALRM, &act, NULL); |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static void unix_rearm_timer(struct qemu_alarm_timer *t) |
| 932 | { |
| 933 | struct itimerval itv; |
| 934 | int64_t nearest_delta_ns = INT64_MAX; |
| 935 | int err; |
| 936 | |
| 937 | assert(alarm_has_dynticks(t)); |
| 938 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
| 939 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 940 | !active_timers[QEMU_CLOCK_HOST]) |
| 941 | return; |
| 942 | |
| 943 | nearest_delta_ns = qemu_next_alarm_deadline(); |
| 944 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
| 945 | nearest_delta_ns = MIN_TIMER_REARM_NS; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 946 | |
| 947 | itv.it_interval.tv_sec = 0; |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 948 | itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */ |
| 949 | itv.it_value.tv_sec = nearest_delta_ns / 1000000000; |
| 950 | itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 951 | err = setitimer(ITIMER_REAL, &itv, NULL); |
Paolo Bonzini | 8468283 | 2011-06-09 13:10:25 +0200 | [diff] [blame] | 952 | if (err) { |
| 953 | perror("setitimer"); |
| 954 | fprintf(stderr, "Internal timer error: aborting\n"); |
| 955 | exit(1); |
| 956 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static void unix_stop_timer(struct qemu_alarm_timer *t) |
| 960 | { |
| 961 | struct itimerval itv; |
| 962 | |
| 963 | memset(&itv, 0, sizeof(itv)); |
| 964 | setitimer(ITIMER_REAL, &itv, NULL); |
| 965 | } |
| 966 | |
| 967 | #endif /* !defined(_WIN32) */ |
| 968 | |
| 969 | |
| 970 | #ifdef _WIN32 |
| 971 | |
Stefan Weil | 2f9cba0 | 2011-04-05 18:34:21 +0200 | [diff] [blame] | 972 | static MMRESULT mm_timer; |
| 973 | static unsigned mm_period; |
| 974 | |
| 975 | static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg, |
| 976 | DWORD_PTR dwUser, DWORD_PTR dw1, |
| 977 | DWORD_PTR dw2) |
| 978 | { |
| 979 | struct qemu_alarm_timer *t = alarm_timer; |
| 980 | if (!t) { |
| 981 | return; |
| 982 | } |
| 983 | if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) { |
| 984 | t->expired = alarm_has_dynticks(t); |
| 985 | t->pending = 1; |
| 986 | qemu_notify_event(); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | static int mm_start_timer(struct qemu_alarm_timer *t) |
| 991 | { |
| 992 | TIMECAPS tc; |
| 993 | UINT flags; |
| 994 | |
| 995 | memset(&tc, 0, sizeof(tc)); |
| 996 | timeGetDevCaps(&tc, sizeof(tc)); |
| 997 | |
| 998 | mm_period = tc.wPeriodMin; |
| 999 | timeBeginPeriod(mm_period); |
| 1000 | |
| 1001 | flags = TIME_CALLBACK_FUNCTION; |
| 1002 | if (alarm_has_dynticks(t)) { |
| 1003 | flags |= TIME_ONESHOT; |
| 1004 | } else { |
| 1005 | flags |= TIME_PERIODIC; |
| 1006 | } |
| 1007 | |
| 1008 | mm_timer = timeSetEvent(1, /* interval (ms) */ |
| 1009 | mm_period, /* resolution */ |
| 1010 | mm_alarm_handler, /* function */ |
| 1011 | (DWORD_PTR)t, /* parameter */ |
| 1012 | flags); |
| 1013 | |
| 1014 | if (!mm_timer) { |
| 1015 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
| 1016 | GetLastError()); |
| 1017 | timeEndPeriod(mm_period); |
| 1018 | return -1; |
| 1019 | } |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static void mm_stop_timer(struct qemu_alarm_timer *t) |
| 1025 | { |
| 1026 | timeKillEvent(mm_timer); |
| 1027 | timeEndPeriod(mm_period); |
| 1028 | } |
| 1029 | |
| 1030 | static void mm_rearm_timer(struct qemu_alarm_timer *t) |
| 1031 | { |
| 1032 | int nearest_delta_ms; |
| 1033 | |
| 1034 | assert(alarm_has_dynticks(t)); |
| 1035 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
| 1036 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1037 | !active_timers[QEMU_CLOCK_HOST]) { |
| 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | timeKillEvent(mm_timer); |
| 1042 | |
| 1043 | nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000; |
| 1044 | if (nearest_delta_ms < 1) { |
| 1045 | nearest_delta_ms = 1; |
| 1046 | } |
| 1047 | mm_timer = timeSetEvent(nearest_delta_ms, |
| 1048 | mm_period, |
| 1049 | mm_alarm_handler, |
| 1050 | (DWORD_PTR)t, |
| 1051 | TIME_ONESHOT | TIME_CALLBACK_FUNCTION); |
| 1052 | |
| 1053 | if (!mm_timer) { |
| 1054 | fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n", |
| 1055 | GetLastError()); |
| 1056 | |
| 1057 | timeEndPeriod(mm_period); |
| 1058 | exit(1); |
| 1059 | } |
| 1060 | } |
| 1061 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1062 | static int win32_start_timer(struct qemu_alarm_timer *t) |
| 1063 | { |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1064 | HANDLE hTimer; |
| 1065 | BOOLEAN success; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1066 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1067 | /* If you call ChangeTimerQueueTimer on a one-shot timer (its period |
| 1068 | is zero) that has already expired, the timer is not updated. Since |
| 1069 | creating a new timer is relatively expensive, set a bogus one-hour |
| 1070 | interval in the dynticks case. */ |
| 1071 | success = CreateTimerQueueTimer(&hTimer, |
| 1072 | NULL, |
| 1073 | host_alarm_handler, |
| 1074 | t, |
| 1075 | 1, |
| 1076 | alarm_has_dynticks(t) ? 3600000 : 1, |
| 1077 | WT_EXECUTEINTIMERTHREAD); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1078 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1079 | if (!success) { |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1080 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
| 1081 | GetLastError()); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1082 | return -1; |
| 1083 | } |
| 1084 | |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 1085 | t->timer = hTimer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static void win32_stop_timer(struct qemu_alarm_timer *t) |
| 1090 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 1091 | HANDLE hTimer = t->timer; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1092 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1093 | if (hTimer) { |
| 1094 | DeleteTimerQueueTimer(NULL, hTimer, NULL); |
| 1095 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | static void win32_rearm_timer(struct qemu_alarm_timer *t) |
| 1099 | { |
Stefan Weil | cd0544e | 2011-04-10 20:15:09 +0200 | [diff] [blame] | 1100 | HANDLE hTimer = t->timer; |
Paolo Bonzini | cfced5b | 2011-03-12 17:43:49 +0100 | [diff] [blame] | 1101 | int nearest_delta_ms; |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1102 | BOOLEAN success; |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1103 | |
| 1104 | assert(alarm_has_dynticks(t)); |
| 1105 | if (!active_timers[QEMU_CLOCK_REALTIME] && |
| 1106 | !active_timers[QEMU_CLOCK_VIRTUAL] && |
| 1107 | !active_timers[QEMU_CLOCK_HOST]) |
| 1108 | return; |
| 1109 | |
Paolo Bonzini | cfced5b | 2011-03-12 17:43:49 +0100 | [diff] [blame] | 1110 | nearest_delta_ms = (qemu_next_alarm_deadline() + 999999) / 1000000; |
| 1111 | if (nearest_delta_ms < 1) { |
| 1112 | nearest_delta_ms = 1; |
| 1113 | } |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1114 | success = ChangeTimerQueueTimer(NULL, |
| 1115 | hTimer, |
| 1116 | nearest_delta_ms, |
| 1117 | 3600000); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1118 | |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1119 | if (!success) { |
| 1120 | fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n", |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1121 | GetLastError()); |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1122 | exit(-1); |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1123 | } |
Paolo Bonzini | 68c23e5 | 2011-03-12 17:43:50 +0100 | [diff] [blame] | 1124 | |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | #endif /* _WIN32 */ |
| 1128 | |
| 1129 | static void alarm_timer_on_change_state_rearm(void *opaque, int running, int reason) |
| 1130 | { |
| 1131 | if (running) |
| 1132 | qemu_rearm_alarm_timer((struct qemu_alarm_timer *) opaque); |
| 1133 | } |
| 1134 | |
| 1135 | int init_timer_alarm(void) |
| 1136 | { |
| 1137 | struct qemu_alarm_timer *t = NULL; |
| 1138 | int i, err = -1; |
| 1139 | |
| 1140 | for (i = 0; alarm_timers[i].name; i++) { |
| 1141 | t = &alarm_timers[i]; |
| 1142 | |
| 1143 | err = t->start(t); |
| 1144 | if (!err) |
| 1145 | break; |
| 1146 | } |
| 1147 | |
| 1148 | if (err) { |
| 1149 | err = -ENOENT; |
| 1150 | goto fail; |
| 1151 | } |
| 1152 | |
| 1153 | /* first event is at time 0 */ |
| 1154 | t->pending = 1; |
| 1155 | alarm_timer = t; |
| 1156 | qemu_add_vm_change_state_handler(alarm_timer_on_change_state_rearm, t); |
| 1157 | |
| 1158 | return 0; |
| 1159 | |
| 1160 | fail: |
| 1161 | return err; |
| 1162 | } |
| 1163 | |
| 1164 | void quit_timers(void) |
| 1165 | { |
| 1166 | struct qemu_alarm_timer *t = alarm_timer; |
| 1167 | alarm_timer = NULL; |
| 1168 | t->stop(t); |
| 1169 | } |
| 1170 | |
| 1171 | int qemu_calculate_timeout(void) |
| 1172 | { |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 1173 | #ifndef CONFIG_IOTHREAD |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1174 | int timeout; |
| 1175 | |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 1176 | if (!vm_running) |
| 1177 | timeout = 5000; |
| 1178 | else { |
| 1179 | /* XXX: use timeout computed from timers */ |
| 1180 | int64_t add; |
| 1181 | int64_t delta; |
| 1182 | /* Advance virtual time to the next event. */ |
| 1183 | delta = qemu_icount_delta(); |
| 1184 | if (delta > 0) { |
| 1185 | /* If virtual time is ahead of real time then just |
| 1186 | wait for IO. */ |
| 1187 | timeout = (delta + 999999) / 1000000; |
| 1188 | } else { |
| 1189 | /* Wait for either IO to occur or the next |
| 1190 | timer event. */ |
Paolo Bonzini | cb842c9 | 2011-04-13 10:03:46 +0200 | [diff] [blame] | 1191 | add = qemu_next_icount_deadline(); |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 1192 | /* We advance the timer before checking for IO. |
| 1193 | Limit the amount we advance so that early IO |
| 1194 | activity won't get the guest too far ahead. */ |
| 1195 | if (add > 10000000) |
| 1196 | add = 10000000; |
| 1197 | delta += add; |
| 1198 | qemu_icount += qemu_icount_round (add); |
| 1199 | timeout = delta / 1000000; |
| 1200 | if (timeout < 0) |
| 1201 | timeout = 0; |
| 1202 | } |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | return timeout; |
Paolo Bonzini | 1ece93a | 2011-04-13 10:03:45 +0200 | [diff] [blame] | 1206 | #else /* CONFIG_IOTHREAD */ |
| 1207 | return 1000; |
| 1208 | #endif |
Paolo Bonzini | db1a497 | 2010-03-10 11:38:55 +0100 | [diff] [blame] | 1209 | } |
| 1210 | |