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