]>
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; | |
688eb389 | 137 | QEMUTimer *active_timers; |
691a0c9c JK |
138 | |
139 | NotifierList reset_notifiers; | |
140 | int64_t last; | |
db1a4972 PB |
141 | }; |
142 | ||
143 | struct QEMUTimer { | |
144 | QEMUClock *clock; | |
4a998740 PB |
145 | int64_t expire_time; /* in nanoseconds */ |
146 | int scale; | |
db1a4972 PB |
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); | |
f3fc6e2e | 156 | void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns); |
cd0544ee SW |
157 | #if defined(__linux__) |
158 | int fd; | |
159 | timer_t timer; | |
160 | #elif defined(_WIN32) | |
161 | HANDLE timer; | |
162 | #endif | |
db1a4972 PB |
163 | char expired; |
164 | char pending; | |
165 | }; | |
166 | ||
167 | static struct qemu_alarm_timer *alarm_timer; | |
168 | ||
45c7b37f SW |
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 | ||
db1a4972 PB |
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 | ||
f3fc6e2e PB |
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 | ||
db1a4972 PB |
213 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
214 | { | |
f3fc6e2e PB |
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) { | |
db1a4972 | 220 | return; |
f3fc6e2e PB |
221 | } |
222 | nearest_delta_ns = qemu_next_alarm_deadline(); | |
223 | t->rearm(t, nearest_delta_ns); | |
db1a4972 PB |
224 | } |
225 | ||
9c13246a PB |
226 | /* TODO: MIN_TIMER_REARM_NS should be optimized */ |
227 | #define MIN_TIMER_REARM_NS 250000 | |
db1a4972 PB |
228 | |
229 | #ifdef _WIN32 | |
230 | ||
2f9cba0c SW |
231 | static int mm_start_timer(struct qemu_alarm_timer *t); |
232 | static void mm_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 233 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
2f9cba0c | 234 | |
db1a4972 PB |
235 | static int win32_start_timer(struct qemu_alarm_timer *t); |
236 | static void win32_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 237 | static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
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); | |
f3fc6e2e | 243 | static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
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); | |
f3fc6e2e | 249 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 | 250 | |
db1a4972 PB |
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. */ | |
1354869c | 268 | if (!runstate_is_running()) |
db1a4972 PB |
269 | return; |
270 | ||
271 | cur_time = cpu_get_clock(); | |
74475455 | 272 | cur_icount = qemu_get_clock_ns(vm_clock); |
db1a4972 PB |
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, | |
7bd427d8 | 294 | qemu_get_clock_ms(rt_clock) + 1000); |
db1a4972 PB |
295 | icount_adjust(); |
296 | } | |
297 | ||
298 | static void icount_adjust_vm(void * opaque) | |
299 | { | |
300 | qemu_mod_timer(icount_vm_timer, | |
74475455 | 301 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
db1a4972 PB |
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, | |
cd0544ee | 314 | dynticks_stop_timer, dynticks_rearm_timer}, |
db1a4972 | 315 | #endif |
84682834 | 316 | {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer}, |
db1a4972 | 317 | #else |
2f9cba0c SW |
318 | {"mmtimer", mm_start_timer, mm_stop_timer, NULL}, |
319 | {"mmtimer2", mm_start_timer, mm_stop_timer, mm_rearm_timer}, | |
cd0544ee SW |
320 | {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer}, |
321 | {"win32", win32_start_timer, win32_stop_timer, NULL}, | |
db1a4972 PB |
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 | ||
7267c094 | 349 | arg = g_strdup(opt); |
db1a4972 PB |
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 | ||
7267c094 | 378 | g_free(arg); |
db1a4972 PB |
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 | ||
db1a4972 PB |
390 | QEMUClock *rt_clock; |
391 | QEMUClock *vm_clock; | |
392 | QEMUClock *host_clock; | |
393 | ||
db1a4972 PB |
394 | static QEMUClock *qemu_new_clock(int type) |
395 | { | |
396 | QEMUClock *clock; | |
691a0c9c | 397 | |
7267c094 | 398 | clock = g_malloc0(sizeof(QEMUClock)); |
db1a4972 PB |
399 | clock->type = type; |
400 | clock->enabled = 1; | |
691a0c9c JK |
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 | } | |
db1a4972 PB |
406 | return clock; |
407 | } | |
408 | ||
409 | void qemu_clock_enable(QEMUClock *clock, int enabled) | |
410 | { | |
411 | clock->enabled = enabled; | |
412 | } | |
413 | ||
ab33fcda PB |
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 | ||
1354869c | 422 | if (runstate_is_running()) { |
ab33fcda PB |
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 | } | |
688eb389 | 437 | if (qemu_timer_expired(vm_clock->active_timers, |
ab33fcda PB |
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); | |
688eb389 | 468 | if (!all_cpu_threads_idle() || !clock->active_timers) { |
ab33fcda PB |
469 | qemu_del_timer(clock->warp_timer); |
470 | return; | |
471 | } | |
472 | ||
473 | vm_clock_warp_start = qemu_get_clock_ns(rt_clock); | |
cb842c90 | 474 | deadline = qemu_next_icount_deadline(); |
ab33fcda PB |
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 | ||
4a998740 PB |
498 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, |
499 | QEMUTimerCB *cb, void *opaque) | |
db1a4972 PB |
500 | { |
501 | QEMUTimer *ts; | |
502 | ||
7267c094 | 503 | ts = g_malloc0(sizeof(QEMUTimer)); |
db1a4972 PB |
504 | ts->clock = clock; |
505 | ts->cb = cb; | |
506 | ts->opaque = opaque; | |
4a998740 | 507 | ts->scale = scale; |
db1a4972 PB |
508 | return ts; |
509 | } | |
510 | ||
511 | void qemu_free_timer(QEMUTimer *ts) | |
512 | { | |
7267c094 | 513 | g_free(ts); |
db1a4972 PB |
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. */ | |
688eb389 | 523 | pt = &ts->clock->active_timers; |
db1a4972 PB |
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. */ | |
4a998740 | 538 | static void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) |
db1a4972 PB |
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. */ | |
688eb389 | 547 | pt = &ts->clock->active_timers; |
db1a4972 PB |
548 | for(;;) { |
549 | t = *pt; | |
45c7b37f | 550 | if (!qemu_timer_expired_ns(t, expire_time)) { |
db1a4972 | 551 | break; |
45c7b37f | 552 | } |
db1a4972 PB |
553 | pt = &t->next; |
554 | } | |
555 | ts->expire_time = expire_time; | |
556 | ts->next = *pt; | |
557 | *pt = ts; | |
558 | ||
559 | /* Rearm if necessary */ | |
688eb389 | 560 | if (pt == &ts->clock->active_timers) { |
db1a4972 PB |
561 | if (!alarm_timer->pending) { |
562 | qemu_rearm_alarm_timer(alarm_timer); | |
563 | } | |
564 | /* Interrupt execution to force deadline recalculation. */ | |
ab33fcda PB |
565 | qemu_clock_warp(ts->clock); |
566 | if (use_icount) { | |
db1a4972 | 567 | qemu_notify_event(); |
ab33fcda | 568 | } |
db1a4972 PB |
569 | } |
570 | } | |
571 | ||
4a998740 PB |
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 | ||
db1a4972 PB |
579 | int qemu_timer_pending(QEMUTimer *ts) |
580 | { | |
581 | QEMUTimer *t; | |
688eb389 | 582 | for (t = ts->clock->active_timers; t != NULL; t = t->next) { |
db1a4972 PB |
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 | { | |
45c7b37f | 591 | return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale); |
db1a4972 PB |
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 | ||
4a998740 | 602 | current_time = qemu_get_clock_ns(clock); |
688eb389 | 603 | ptimer_head = &clock->active_timers; |
db1a4972 PB |
604 | for(;;) { |
605 | ts = *ptimer_head; | |
45c7b37f | 606 | if (!qemu_timer_expired_ns(ts, current_time)) { |
db1a4972 | 607 | break; |
45c7b37f | 608 | } |
db1a4972 PB |
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 | ||
db1a4972 PB |
618 | int64_t qemu_get_clock_ns(QEMUClock *clock) |
619 | { | |
691a0c9c JK |
620 | int64_t now, last; |
621 | ||
db1a4972 PB |
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: | |
691a0c9c JK |
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; | |
db1a4972 PB |
640 | } |
641 | } | |
642 | ||
691a0c9c JK |
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 | ||
db1a4972 PB |
653 | void init_clocks(void) |
654 | { | |
db1a4972 PB |
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) { | |
4a998740 | 681 | qemu_mod_timer_ns(ts, expire_time); |
db1a4972 PB |
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 | { | |
0be71e32 | 702 | vmstate_register(NULL, 0, &vmstate_timers, &timers_state); |
db1a4972 PB |
703 | if (!option) |
704 | return; | |
705 | ||
ab33fcda | 706 | vm_clock->warp_timer = qemu_new_timer_ns(rt_clock, icount_warp_rt, NULL); |
ab33fcda | 707 | |
db1a4972 PB |
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. */ | |
7bd427d8 | 725 | icount_rt_timer = qemu_new_timer_ms(rt_clock, icount_adjust_rt, NULL); |
db1a4972 | 726 | qemu_mod_timer(icount_rt_timer, |
7bd427d8 | 727 | qemu_get_clock_ms(rt_clock) + 1000); |
74475455 | 728 | icount_vm_timer = qemu_new_timer_ns(vm_clock, icount_adjust_vm, NULL); |
db1a4972 | 729 | qemu_mod_timer(icount_vm_timer, |
74475455 | 730 | qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 10); |
db1a4972 PB |
731 | } |
732 | ||
733 | void qemu_run_all_timers(void) | |
734 | { | |
ca5a2a4b PB |
735 | alarm_timer->pending = 0; |
736 | ||
db1a4972 PB |
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 | ||
db1a4972 | 743 | /* vm time timers */ |
1354869c | 744 | if (runstate_is_running()) { |
db1a4972 PB |
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 | |
68c23e55 | 753 | static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
db1a4972 PB |
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; | |
74475455 | 768 | ti = qemu_get_clock_ns(vm_clock); |
db1a4972 PB |
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) || | |
4c3d45eb | 792 | qemu_next_alarm_deadline () <= 0) { |
db1a4972 PB |
793 | t->expired = alarm_has_dynticks(t); |
794 | t->pending = 1; | |
795 | qemu_notify_event(); | |
796 | } | |
797 | } | |
798 | ||
cb842c90 | 799 | int64_t qemu_next_icount_deadline(void) |
db1a4972 PB |
800 | { |
801 | /* To avoid problems with overflow limit this to 2^32. */ | |
802 | int64_t delta = INT32_MAX; | |
803 | ||
cb842c90 | 804 | assert(use_icount); |
688eb389 PB |
805 | if (vm_clock->active_timers) { |
806 | delta = vm_clock->active_timers->expire_time - | |
9c13246a | 807 | qemu_get_clock_ns(vm_clock); |
db1a4972 | 808 | } |
db1a4972 PB |
809 | |
810 | if (delta < 0) | |
811 | delta = 0; | |
812 | ||
813 | return delta; | |
814 | } | |
815 | ||
4c3d45eb PB |
816 | #if defined(__linux__) |
817 | ||
d25f89c9 JK |
818 | #include "compatfd.h" |
819 | ||
db1a4972 PB |
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; | |
d25f89c9 JK |
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 */ | |
db1a4972 PB |
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 | ||
cd0544ee | 856 | t->timer = host_timer; |
db1a4972 PB |
857 | |
858 | return 0; | |
859 | } | |
860 | ||
861 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) | |
862 | { | |
cd0544ee | 863 | timer_t host_timer = t->timer; |
db1a4972 PB |
864 | |
865 | timer_delete(host_timer); | |
866 | } | |
867 | ||
f3fc6e2e PB |
868 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, |
869 | int64_t nearest_delta_ns) | |
db1a4972 | 870 | { |
cd0544ee | 871 | timer_t host_timer = t->timer; |
db1a4972 | 872 | struct itimerspec timeout; |
9c13246a | 873 | int64_t current_ns; |
db1a4972 | 874 | |
4c3d45eb PB |
875 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
876 | nearest_delta_ns = MIN_TIMER_REARM_NS; | |
db1a4972 PB |
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 | } | |
9c13246a PB |
884 | current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; |
885 | if (current_ns && current_ns <= nearest_delta_ns) | |
db1a4972 PB |
886 | return; |
887 | ||
888 | timeout.it_interval.tv_sec = 0; | |
889 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ | |
9c13246a PB |
890 | timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; |
891 | timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; | |
db1a4972 PB |
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 | ||
f26e5a54 SW |
901 | #if !defined(_WIN32) |
902 | ||
db1a4972 PB |
903 | static int unix_start_timer(struct qemu_alarm_timer *t) |
904 | { | |
905 | struct sigaction act; | |
db1a4972 PB |
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); | |
84682834 PB |
913 | return 0; |
914 | } | |
db1a4972 | 915 | |
f3fc6e2e PB |
916 | static void unix_rearm_timer(struct qemu_alarm_timer *t, |
917 | int64_t nearest_delta_ns) | |
84682834 PB |
918 | { |
919 | struct itimerval itv; | |
84682834 | 920 | int err; |
db1a4972 | 921 | |
84682834 PB |
922 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
923 | nearest_delta_ns = MIN_TIMER_REARM_NS; | |
924 | ||
925 | itv.it_interval.tv_sec = 0; | |
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; | |
929 | err = setitimer(ITIMER_REAL, &itv, NULL); | |
930 | if (err) { | |
931 | perror("setitimer"); | |
932 | fprintf(stderr, "Internal timer error: aborting\n"); | |
933 | exit(1); | |
934 | } | |
db1a4972 PB |
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 | ||
2f9cba0c SW |
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 | ||
f3fc6e2e | 1008 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta) |
2f9cba0c | 1009 | { |
f3fc6e2e | 1010 | int nearest_delta_ms = (delta + 999999) / 1000000; |
2f9cba0c SW |
1011 | if (nearest_delta_ms < 1) { |
1012 | nearest_delta_ms = 1; | |
1013 | } | |
f3fc6e2e PB |
1014 | |
1015 | timeKillEvent(mm_timer); | |
2f9cba0c SW |
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 | ||
db1a4972 PB |
1031 | static int win32_start_timer(struct qemu_alarm_timer *t) |
1032 | { | |
68c23e55 PB |
1033 | HANDLE hTimer; |
1034 | BOOLEAN success; | |
1035 | ||
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); | |
1047 | ||
1048 | if (!success) { | |
db1a4972 PB |
1049 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
1050 | GetLastError()); | |
db1a4972 PB |
1051 | return -1; |
1052 | } | |
1053 | ||
cd0544ee | 1054 | t->timer = hTimer; |
db1a4972 PB |
1055 | return 0; |
1056 | } | |
1057 | ||
1058 | static void win32_stop_timer(struct qemu_alarm_timer *t) | |
1059 | { | |
cd0544ee | 1060 | HANDLE hTimer = t->timer; |
db1a4972 | 1061 | |
68c23e55 PB |
1062 | if (hTimer) { |
1063 | DeleteTimerQueueTimer(NULL, hTimer, NULL); | |
1064 | } | |
db1a4972 PB |
1065 | } |
1066 | ||
f3fc6e2e PB |
1067 | static void win32_rearm_timer(struct qemu_alarm_timer *t, |
1068 | int64_t nearest_delta_ns) | |
db1a4972 | 1069 | { |
cd0544ee | 1070 | HANDLE hTimer = t->timer; |
cfced5b2 | 1071 | int nearest_delta_ms; |
68c23e55 | 1072 | BOOLEAN success; |
db1a4972 | 1073 | |
f3fc6e2e | 1074 | nearest_delta_ms = (nearest_delta_ns + 999999) / 1000000; |
cfced5b2 PB |
1075 | if (nearest_delta_ms < 1) { |
1076 | nearest_delta_ms = 1; | |
1077 | } | |
68c23e55 PB |
1078 | success = ChangeTimerQueueTimer(NULL, |
1079 | hTimer, | |
1080 | nearest_delta_ms, | |
1081 | 3600000); | |
db1a4972 | 1082 | |
68c23e55 PB |
1083 | if (!success) { |
1084 | fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n", | |
1085 | GetLastError()); | |
1086 | exit(-1); | |
db1a4972 | 1087 | } |
68c23e55 | 1088 | |
db1a4972 PB |
1089 | } |
1090 | ||
1091 | #endif /* _WIN32 */ | |
1092 | ||
1dfb4dd9 LC |
1093 | static void alarm_timer_on_change_state_rearm(void *opaque, int running, |
1094 | RunState state) | |
db1a4972 PB |
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 | { | |
1ece93a9 | 1138 | return 1000; |
db1a4972 PB |
1139 | } |
1140 |