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