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