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