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