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