root / qemu-timer.c @ c4879f2a
History | View | Annotate | Download (19.6 kB)
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 "qemu-timer.h" |
33 |
|
34 |
#ifdef __FreeBSD__
|
35 |
#include <sys/param.h> |
36 |
#endif
|
37 |
|
38 |
#ifdef _WIN32
|
39 |
#include <mmsystem.h> |
40 |
#endif
|
41 |
|
42 |
/***********************************************************/
|
43 |
/* timers */
|
44 |
|
45 |
#define QEMU_CLOCK_REALTIME 0 |
46 |
#define QEMU_CLOCK_VIRTUAL 1 |
47 |
#define QEMU_CLOCK_HOST 2 |
48 |
|
49 |
struct QEMUClock {
|
50 |
int type;
|
51 |
int enabled;
|
52 |
|
53 |
QEMUTimer *active_timers; |
54 |
|
55 |
NotifierList reset_notifiers; |
56 |
int64_t last; |
57 |
}; |
58 |
|
59 |
struct QEMUTimer {
|
60 |
QEMUClock *clock; |
61 |
int64_t expire_time; /* in nanoseconds */
|
62 |
int scale;
|
63 |
QEMUTimerCB *cb; |
64 |
void *opaque;
|
65 |
struct QEMUTimer *next;
|
66 |
}; |
67 |
|
68 |
struct qemu_alarm_timer {
|
69 |
char const *name; |
70 |
int (*start)(struct qemu_alarm_timer *t); |
71 |
void (*stop)(struct qemu_alarm_timer *t); |
72 |
void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns); |
73 |
#if defined(__linux__)
|
74 |
int fd;
|
75 |
timer_t timer; |
76 |
#elif defined(_WIN32)
|
77 |
HANDLE timer; |
78 |
#endif
|
79 |
char expired;
|
80 |
char pending;
|
81 |
}; |
82 |
|
83 |
static struct qemu_alarm_timer *alarm_timer; |
84 |
|
85 |
static bool qemu_timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
86 |
{ |
87 |
return timer_head && (timer_head->expire_time <= current_time);
|
88 |
} |
89 |
|
90 |
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
91 |
{ |
92 |
return !!t->rearm;
|
93 |
} |
94 |
|
95 |
static int64_t qemu_next_alarm_deadline(void) |
96 |
{ |
97 |
int64_t delta = INT64_MAX; |
98 |
int64_t rtdelta; |
99 |
|
100 |
if (!use_icount && vm_clock->enabled && vm_clock->active_timers) {
|
101 |
delta = vm_clock->active_timers->expire_time - |
102 |
qemu_get_clock_ns(vm_clock); |
103 |
} |
104 |
if (host_clock->enabled && host_clock->active_timers) {
|
105 |
int64_t hdelta = host_clock->active_timers->expire_time - |
106 |
qemu_get_clock_ns(host_clock); |
107 |
if (hdelta < delta) {
|
108 |
delta = hdelta; |
109 |
} |
110 |
} |
111 |
if (rt_clock->enabled && rt_clock->active_timers) {
|
112 |
rtdelta = (rt_clock->active_timers->expire_time - |
113 |
qemu_get_clock_ns(rt_clock)); |
114 |
if (rtdelta < delta) {
|
115 |
delta = rtdelta; |
116 |
} |
117 |
} |
118 |
|
119 |
return delta;
|
120 |
} |
121 |
|
122 |
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
123 |
{ |
124 |
int64_t nearest_delta_ns; |
125 |
assert(alarm_has_dynticks(t)); |
126 |
if (!rt_clock->active_timers &&
|
127 |
!vm_clock->active_timers && |
128 |
!host_clock->active_timers) { |
129 |
return;
|
130 |
} |
131 |
nearest_delta_ns = qemu_next_alarm_deadline(); |
132 |
t->rearm(t, nearest_delta_ns); |
133 |
} |
134 |
|
135 |
/* TODO: MIN_TIMER_REARM_NS should be optimized */
|
136 |
#define MIN_TIMER_REARM_NS 250000 |
137 |
|
138 |
#ifdef _WIN32
|
139 |
|
140 |
static int mm_start_timer(struct qemu_alarm_timer *t); |
141 |
static void mm_stop_timer(struct qemu_alarm_timer *t); |
142 |
static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
143 |
|
144 |
static int win32_start_timer(struct qemu_alarm_timer *t); |
145 |
static void win32_stop_timer(struct qemu_alarm_timer *t); |
146 |
static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
147 |
|
148 |
#else
|
149 |
|
150 |
static int unix_start_timer(struct qemu_alarm_timer *t); |
151 |
static void unix_stop_timer(struct qemu_alarm_timer *t); |
152 |
static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
153 |
|
154 |
#ifdef __linux__
|
155 |
|
156 |
static int dynticks_start_timer(struct qemu_alarm_timer *t); |
157 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
158 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
159 |
|
160 |
#endif /* __linux__ */ |
161 |
|
162 |
#endif /* _WIN32 */ |
163 |
|
164 |
static struct qemu_alarm_timer alarm_timers[] = { |
165 |
#ifndef _WIN32
|
166 |
#ifdef __linux__
|
167 |
{"dynticks", dynticks_start_timer,
|
168 |
dynticks_stop_timer, dynticks_rearm_timer}, |
169 |
#endif
|
170 |
{"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer},
|
171 |
#else
|
172 |
{"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer},
|
173 |
{"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer},
|
174 |
#endif
|
175 |
{NULL, }
|
176 |
}; |
177 |
|
178 |
static void show_available_alarms(void) |
179 |
{ |
180 |
int i;
|
181 |
|
182 |
printf("Available alarm timers, in order of precedence:\n");
|
183 |
for (i = 0; alarm_timers[i].name; i++) |
184 |
printf("%s\n", alarm_timers[i].name);
|
185 |
} |
186 |
|
187 |
void configure_alarms(char const *opt) |
188 |
{ |
189 |
int i;
|
190 |
int cur = 0; |
191 |
int count = ARRAY_SIZE(alarm_timers) - 1; |
192 |
char *arg;
|
193 |
char *name;
|
194 |
struct qemu_alarm_timer tmp;
|
195 |
|
196 |
if (!strcmp(opt, "?")) { |
197 |
show_available_alarms(); |
198 |
exit(0);
|
199 |
} |
200 |
|
201 |
arg = g_strdup(opt); |
202 |
|
203 |
/* Reorder the array */
|
204 |
name = strtok(arg, ",");
|
205 |
while (name) {
|
206 |
for (i = 0; i < count && alarm_timers[i].name; i++) { |
207 |
if (!strcmp(alarm_timers[i].name, name))
|
208 |
break;
|
209 |
} |
210 |
|
211 |
if (i == count) {
|
212 |
fprintf(stderr, "Unknown clock %s\n", name);
|
213 |
goto next;
|
214 |
} |
215 |
|
216 |
if (i < cur)
|
217 |
/* Ignore */
|
218 |
goto next;
|
219 |
|
220 |
/* Swap */
|
221 |
tmp = alarm_timers[i]; |
222 |
alarm_timers[i] = alarm_timers[cur]; |
223 |
alarm_timers[cur] = tmp; |
224 |
|
225 |
cur++; |
226 |
next:
|
227 |
name = strtok(NULL, ","); |
228 |
} |
229 |
|
230 |
g_free(arg); |
231 |
|
232 |
if (cur) {
|
233 |
/* Disable remaining timers */
|
234 |
for (i = cur; i < count; i++)
|
235 |
alarm_timers[i].name = NULL;
|
236 |
} else {
|
237 |
show_available_alarms(); |
238 |
exit(1);
|
239 |
} |
240 |
} |
241 |
|
242 |
QEMUClock *rt_clock; |
243 |
QEMUClock *vm_clock; |
244 |
QEMUClock *host_clock; |
245 |
|
246 |
static QEMUClock *qemu_new_clock(int type) |
247 |
{ |
248 |
QEMUClock *clock; |
249 |
|
250 |
clock = g_malloc0(sizeof(QEMUClock));
|
251 |
clock->type = type; |
252 |
clock->enabled = 1;
|
253 |
clock->last = INT64_MIN; |
254 |
notifier_list_init(&clock->reset_notifiers); |
255 |
return clock;
|
256 |
} |
257 |
|
258 |
void qemu_clock_enable(QEMUClock *clock, int enabled) |
259 |
{ |
260 |
bool old = clock->enabled;
|
261 |
clock->enabled = enabled; |
262 |
if (enabled && !old) {
|
263 |
qemu_rearm_alarm_timer(alarm_timer); |
264 |
} |
265 |
} |
266 |
|
267 |
int64_t qemu_clock_has_timers(QEMUClock *clock) |
268 |
{ |
269 |
return !!clock->active_timers;
|
270 |
} |
271 |
|
272 |
int64_t qemu_clock_expired(QEMUClock *clock) |
273 |
{ |
274 |
return (clock->active_timers &&
|
275 |
clock->active_timers->expire_time < qemu_get_clock_ns(clock)); |
276 |
} |
277 |
|
278 |
int64_t qemu_clock_deadline(QEMUClock *clock) |
279 |
{ |
280 |
/* To avoid problems with overflow limit this to 2^32. */
|
281 |
int64_t delta = INT32_MAX; |
282 |
|
283 |
if (clock->active_timers) {
|
284 |
delta = clock->active_timers->expire_time - qemu_get_clock_ns(clock); |
285 |
} |
286 |
if (delta < 0) { |
287 |
delta = 0;
|
288 |
} |
289 |
return delta;
|
290 |
} |
291 |
|
292 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale,
|
293 |
QEMUTimerCB *cb, void *opaque)
|
294 |
{ |
295 |
QEMUTimer *ts; |
296 |
|
297 |
ts = g_malloc0(sizeof(QEMUTimer));
|
298 |
ts->clock = clock; |
299 |
ts->cb = cb; |
300 |
ts->opaque = opaque; |
301 |
ts->scale = scale; |
302 |
return ts;
|
303 |
} |
304 |
|
305 |
void qemu_free_timer(QEMUTimer *ts)
|
306 |
{ |
307 |
g_free(ts); |
308 |
} |
309 |
|
310 |
/* stop a timer, but do not dealloc it */
|
311 |
void qemu_del_timer(QEMUTimer *ts)
|
312 |
{ |
313 |
QEMUTimer **pt, *t; |
314 |
|
315 |
/* NOTE: this code must be signal safe because
|
316 |
qemu_timer_expired() can be called from a signal. */
|
317 |
pt = &ts->clock->active_timers; |
318 |
for(;;) {
|
319 |
t = *pt; |
320 |
if (!t)
|
321 |
break;
|
322 |
if (t == ts) {
|
323 |
*pt = t->next; |
324 |
break;
|
325 |
} |
326 |
pt = &t->next; |
327 |
} |
328 |
} |
329 |
|
330 |
/* modify the current timer so that it will be fired when current_time
|
331 |
>= expire_time. The corresponding callback will be called. */
|
332 |
void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time)
|
333 |
{ |
334 |
QEMUTimer **pt, *t; |
335 |
|
336 |
qemu_del_timer(ts); |
337 |
|
338 |
/* add the timer in the sorted list */
|
339 |
/* NOTE: this code must be signal safe because
|
340 |
qemu_timer_expired() can be called from a signal. */
|
341 |
pt = &ts->clock->active_timers; |
342 |
for(;;) {
|
343 |
t = *pt; |
344 |
if (!qemu_timer_expired_ns(t, expire_time)) {
|
345 |
break;
|
346 |
} |
347 |
pt = &t->next; |
348 |
} |
349 |
ts->expire_time = expire_time; |
350 |
ts->next = *pt; |
351 |
*pt = ts; |
352 |
|
353 |
/* Rearm if necessary */
|
354 |
if (pt == &ts->clock->active_timers) {
|
355 |
if (!alarm_timer->pending) {
|
356 |
qemu_rearm_alarm_timer(alarm_timer); |
357 |
} |
358 |
/* Interrupt execution to force deadline recalculation. */
|
359 |
qemu_clock_warp(ts->clock); |
360 |
if (use_icount) {
|
361 |
qemu_notify_event(); |
362 |
} |
363 |
} |
364 |
} |
365 |
|
366 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
367 |
{ |
368 |
qemu_mod_timer_ns(ts, expire_time * ts->scale); |
369 |
} |
370 |
|
371 |
int qemu_timer_pending(QEMUTimer *ts)
|
372 |
{ |
373 |
QEMUTimer *t; |
374 |
for (t = ts->clock->active_timers; t != NULL; t = t->next) { |
375 |
if (t == ts)
|
376 |
return 1; |
377 |
} |
378 |
return 0; |
379 |
} |
380 |
|
381 |
int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
|
382 |
{ |
383 |
return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale);
|
384 |
} |
385 |
|
386 |
void qemu_run_timers(QEMUClock *clock)
|
387 |
{ |
388 |
QEMUTimer **ptimer_head, *ts; |
389 |
int64_t current_time; |
390 |
|
391 |
if (!clock->enabled)
|
392 |
return;
|
393 |
|
394 |
current_time = qemu_get_clock_ns(clock); |
395 |
ptimer_head = &clock->active_timers; |
396 |
for(;;) {
|
397 |
ts = *ptimer_head; |
398 |
if (!qemu_timer_expired_ns(ts, current_time)) {
|
399 |
break;
|
400 |
} |
401 |
/* remove timer from the list before calling the callback */
|
402 |
*ptimer_head = ts->next; |
403 |
ts->next = NULL;
|
404 |
|
405 |
/* run the callback (the timer list can be modified) */
|
406 |
ts->cb(ts->opaque); |
407 |
} |
408 |
} |
409 |
|
410 |
int64_t qemu_get_clock_ns(QEMUClock *clock) |
411 |
{ |
412 |
int64_t now, last; |
413 |
|
414 |
switch(clock->type) {
|
415 |
case QEMU_CLOCK_REALTIME:
|
416 |
return get_clock();
|
417 |
default:
|
418 |
case QEMU_CLOCK_VIRTUAL:
|
419 |
if (use_icount) {
|
420 |
return cpu_get_icount();
|
421 |
} else {
|
422 |
return cpu_get_clock();
|
423 |
} |
424 |
case QEMU_CLOCK_HOST:
|
425 |
now = get_clock_realtime(); |
426 |
last = clock->last; |
427 |
clock->last = now; |
428 |
if (now < last) {
|
429 |
notifier_list_notify(&clock->reset_notifiers, &now); |
430 |
} |
431 |
return now;
|
432 |
} |
433 |
} |
434 |
|
435 |
void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
|
436 |
{ |
437 |
notifier_list_add(&clock->reset_notifiers, notifier); |
438 |
} |
439 |
|
440 |
void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier)
|
441 |
{ |
442 |
notifier_remove(notifier); |
443 |
} |
444 |
|
445 |
void init_clocks(void) |
446 |
{ |
447 |
rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME); |
448 |
vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL); |
449 |
host_clock = qemu_new_clock(QEMU_CLOCK_HOST); |
450 |
} |
451 |
|
452 |
uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts) |
453 |
{ |
454 |
return qemu_timer_pending(ts) ? ts->expire_time : -1; |
455 |
} |
456 |
|
457 |
void qemu_run_all_timers(void) |
458 |
{ |
459 |
alarm_timer->pending = 0;
|
460 |
|
461 |
/* vm time timers */
|
462 |
qemu_run_timers(vm_clock); |
463 |
qemu_run_timers(rt_clock); |
464 |
qemu_run_timers(host_clock); |
465 |
|
466 |
/* rearm timer, if not periodic */
|
467 |
if (alarm_timer->expired) {
|
468 |
alarm_timer->expired = 0;
|
469 |
qemu_rearm_alarm_timer(alarm_timer); |
470 |
} |
471 |
} |
472 |
|
473 |
#ifdef _WIN32
|
474 |
static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
475 |
#else
|
476 |
static void host_alarm_handler(int host_signum) |
477 |
#endif
|
478 |
{ |
479 |
struct qemu_alarm_timer *t = alarm_timer;
|
480 |
if (!t)
|
481 |
return;
|
482 |
|
483 |
if (alarm_has_dynticks(t) ||
|
484 |
qemu_next_alarm_deadline () <= 0) {
|
485 |
t->expired = alarm_has_dynticks(t); |
486 |
t->pending = 1;
|
487 |
qemu_notify_event(); |
488 |
} |
489 |
} |
490 |
|
491 |
#if defined(__linux__)
|
492 |
|
493 |
#include "compatfd.h" |
494 |
|
495 |
static int dynticks_start_timer(struct qemu_alarm_timer *t) |
496 |
{ |
497 |
struct sigevent ev;
|
498 |
timer_t host_timer; |
499 |
struct sigaction act;
|
500 |
|
501 |
sigfillset(&act.sa_mask); |
502 |
act.sa_flags = 0;
|
503 |
act.sa_handler = host_alarm_handler; |
504 |
|
505 |
sigaction(SIGALRM, &act, NULL);
|
506 |
|
507 |
/*
|
508 |
* Initialize ev struct to 0 to avoid valgrind complaining
|
509 |
* about uninitialized data in timer_create call
|
510 |
*/
|
511 |
memset(&ev, 0, sizeof(ev)); |
512 |
ev.sigev_value.sival_int = 0;
|
513 |
ev.sigev_notify = SIGEV_SIGNAL; |
514 |
#ifdef SIGEV_THREAD_ID
|
515 |
if (qemu_signalfd_available()) {
|
516 |
ev.sigev_notify = SIGEV_THREAD_ID; |
517 |
ev._sigev_un._tid = qemu_get_thread_id(); |
518 |
} |
519 |
#endif /* SIGEV_THREAD_ID */ |
520 |
ev.sigev_signo = SIGALRM; |
521 |
|
522 |
if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
523 |
perror("timer_create");
|
524 |
|
525 |
/* disable dynticks */
|
526 |
fprintf(stderr, "Dynamic Ticks disabled\n");
|
527 |
|
528 |
return -1; |
529 |
} |
530 |
|
531 |
t->timer = host_timer; |
532 |
|
533 |
return 0; |
534 |
} |
535 |
|
536 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
537 |
{ |
538 |
timer_t host_timer = t->timer; |
539 |
|
540 |
timer_delete(host_timer); |
541 |
} |
542 |
|
543 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t, |
544 |
int64_t nearest_delta_ns) |
545 |
{ |
546 |
timer_t host_timer = t->timer; |
547 |
struct itimerspec timeout;
|
548 |
int64_t current_ns; |
549 |
|
550 |
if (nearest_delta_ns < MIN_TIMER_REARM_NS)
|
551 |
nearest_delta_ns = MIN_TIMER_REARM_NS; |
552 |
|
553 |
/* check whether a timer is already running */
|
554 |
if (timer_gettime(host_timer, &timeout)) {
|
555 |
perror("gettime");
|
556 |
fprintf(stderr, "Internal timer error: aborting\n");
|
557 |
exit(1);
|
558 |
} |
559 |
current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec;
|
560 |
if (current_ns && current_ns <= nearest_delta_ns)
|
561 |
return;
|
562 |
|
563 |
timeout.it_interval.tv_sec = 0;
|
564 |
timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
565 |
timeout.it_value.tv_sec = nearest_delta_ns / 1000000000;
|
566 |
timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000;
|
567 |
if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
568 |
perror("settime");
|
569 |
fprintf(stderr, "Internal timer error: aborting\n");
|
570 |
exit(1);
|
571 |
} |
572 |
} |
573 |
|
574 |
#endif /* defined(__linux__) */ |
575 |
|
576 |
#if !defined(_WIN32)
|
577 |
|
578 |
static int unix_start_timer(struct qemu_alarm_timer *t) |
579 |
{ |
580 |
struct sigaction act;
|
581 |
|
582 |
/* timer signal */
|
583 |
sigfillset(&act.sa_mask); |
584 |
act.sa_flags = 0;
|
585 |
act.sa_handler = host_alarm_handler; |
586 |
|
587 |
sigaction(SIGALRM, &act, NULL);
|
588 |
return 0; |
589 |
} |
590 |
|
591 |
static void unix_rearm_timer(struct qemu_alarm_timer *t, |
592 |
int64_t nearest_delta_ns) |
593 |
{ |
594 |
struct itimerval itv;
|
595 |
int err;
|
596 |
|
597 |
if (nearest_delta_ns < MIN_TIMER_REARM_NS)
|
598 |
nearest_delta_ns = MIN_TIMER_REARM_NS; |
599 |
|
600 |
itv.it_interval.tv_sec = 0;
|
601 |
itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */ |
602 |
itv.it_value.tv_sec = nearest_delta_ns / 1000000000;
|
603 |
itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000; |
604 |
err = setitimer(ITIMER_REAL, &itv, NULL);
|
605 |
if (err) {
|
606 |
perror("setitimer");
|
607 |
fprintf(stderr, "Internal timer error: aborting\n");
|
608 |
exit(1);
|
609 |
} |
610 |
} |
611 |
|
612 |
static void unix_stop_timer(struct qemu_alarm_timer *t) |
613 |
{ |
614 |
struct itimerval itv;
|
615 |
|
616 |
memset(&itv, 0, sizeof(itv)); |
617 |
setitimer(ITIMER_REAL, &itv, NULL);
|
618 |
} |
619 |
|
620 |
#endif /* !defined(_WIN32) */ |
621 |
|
622 |
|
623 |
#ifdef _WIN32
|
624 |
|
625 |
static MMRESULT mm_timer;
|
626 |
static unsigned mm_period; |
627 |
|
628 |
static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg, |
629 |
DWORD_PTR dwUser, DWORD_PTR dw1, |
630 |
DWORD_PTR dw2) |
631 |
{ |
632 |
struct qemu_alarm_timer *t = alarm_timer;
|
633 |
if (!t) {
|
634 |
return;
|
635 |
} |
636 |
if (alarm_has_dynticks(t) || qemu_next_alarm_deadline() <= 0) { |
637 |
t->expired = alarm_has_dynticks(t); |
638 |
t->pending = 1;
|
639 |
qemu_notify_event(); |
640 |
} |
641 |
} |
642 |
|
643 |
static int mm_start_timer(struct qemu_alarm_timer *t) |
644 |
{ |
645 |
TIMECAPS tc; |
646 |
UINT flags; |
647 |
|
648 |
memset(&tc, 0, sizeof(tc)); |
649 |
timeGetDevCaps(&tc, sizeof(tc));
|
650 |
|
651 |
mm_period = tc.wPeriodMin; |
652 |
timeBeginPeriod(mm_period); |
653 |
|
654 |
flags = TIME_CALLBACK_FUNCTION; |
655 |
if (alarm_has_dynticks(t)) {
|
656 |
flags |= TIME_ONESHOT; |
657 |
} else {
|
658 |
flags |= TIME_PERIODIC; |
659 |
} |
660 |
|
661 |
mm_timer = timeSetEvent(1, /* interval (ms) */ |
662 |
mm_period, /* resolution */
|
663 |
mm_alarm_handler, /* function */
|
664 |
(DWORD_PTR)t, /* parameter */
|
665 |
flags); |
666 |
|
667 |
if (!mm_timer) {
|
668 |
fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
|
669 |
GetLastError()); |
670 |
timeEndPeriod(mm_period); |
671 |
return -1; |
672 |
} |
673 |
|
674 |
return 0; |
675 |
} |
676 |
|
677 |
static void mm_stop_timer(struct qemu_alarm_timer *t) |
678 |
{ |
679 |
timeKillEvent(mm_timer); |
680 |
timeEndPeriod(mm_period); |
681 |
} |
682 |
|
683 |
static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta) |
684 |
{ |
685 |
int64_t nearest_delta_ms = delta / 1000000;
|
686 |
if (nearest_delta_ms < 1) { |
687 |
nearest_delta_ms = 1;
|
688 |
} |
689 |
/* UINT_MAX can be 32 bit */
|
690 |
if (nearest_delta_ms > UINT_MAX) {
|
691 |
nearest_delta_ms = UINT_MAX; |
692 |
} |
693 |
|
694 |
timeKillEvent(mm_timer); |
695 |
mm_timer = timeSetEvent((unsigned int) nearest_delta_ms, |
696 |
mm_period, |
697 |
mm_alarm_handler, |
698 |
(DWORD_PTR)t, |
699 |
TIME_ONESHOT | TIME_CALLBACK_FUNCTION); |
700 |
|
701 |
if (!mm_timer) {
|
702 |
fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
|
703 |
GetLastError()); |
704 |
|
705 |
timeEndPeriod(mm_period); |
706 |
exit(1);
|
707 |
} |
708 |
} |
709 |
|
710 |
static int win32_start_timer(struct qemu_alarm_timer *t) |
711 |
{ |
712 |
HANDLE hTimer; |
713 |
BOOLEAN success; |
714 |
|
715 |
/* If you call ChangeTimerQueueTimer on a one-shot timer (its period
|
716 |
is zero) that has already expired, the timer is not updated. Since
|
717 |
creating a new timer is relatively expensive, set a bogus one-hour
|
718 |
interval in the dynticks case. */
|
719 |
success = CreateTimerQueueTimer(&hTimer, |
720 |
NULL,
|
721 |
host_alarm_handler, |
722 |
t, |
723 |
1,
|
724 |
alarm_has_dynticks(t) ? 3600000 : 1, |
725 |
WT_EXECUTEINTIMERTHREAD); |
726 |
|
727 |
if (!success) {
|
728 |
fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
|
729 |
GetLastError()); |
730 |
return -1; |
731 |
} |
732 |
|
733 |
t->timer = hTimer; |
734 |
return 0; |
735 |
} |
736 |
|
737 |
static void win32_stop_timer(struct qemu_alarm_timer *t) |
738 |
{ |
739 |
HANDLE hTimer = t->timer; |
740 |
|
741 |
if (hTimer) {
|
742 |
DeleteTimerQueueTimer(NULL, hTimer, NULL); |
743 |
} |
744 |
} |
745 |
|
746 |
static void win32_rearm_timer(struct qemu_alarm_timer *t, |
747 |
int64_t nearest_delta_ns) |
748 |
{ |
749 |
HANDLE hTimer = t->timer; |
750 |
int64_t nearest_delta_ms; |
751 |
BOOLEAN success; |
752 |
|
753 |
nearest_delta_ms = nearest_delta_ns / 1000000;
|
754 |
if (nearest_delta_ms < 1) { |
755 |
nearest_delta_ms = 1;
|
756 |
} |
757 |
/* ULONG_MAX can be 32 bit */
|
758 |
if (nearest_delta_ms > ULONG_MAX) {
|
759 |
nearest_delta_ms = ULONG_MAX; |
760 |
} |
761 |
success = ChangeTimerQueueTimer(NULL,
|
762 |
hTimer, |
763 |
(unsigned long) nearest_delta_ms, |
764 |
3600000);
|
765 |
|
766 |
if (!success) {
|
767 |
fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n",
|
768 |
GetLastError()); |
769 |
exit(-1);
|
770 |
} |
771 |
|
772 |
} |
773 |
|
774 |
#endif /* _WIN32 */ |
775 |
|
776 |
static void quit_timers(void) |
777 |
{ |
778 |
struct qemu_alarm_timer *t = alarm_timer;
|
779 |
alarm_timer = NULL;
|
780 |
t->stop(t); |
781 |
} |
782 |
|
783 |
int init_timer_alarm(void) |
784 |
{ |
785 |
struct qemu_alarm_timer *t = NULL; |
786 |
int i, err = -1; |
787 |
|
788 |
for (i = 0; alarm_timers[i].name; i++) { |
789 |
t = &alarm_timers[i]; |
790 |
|
791 |
err = t->start(t); |
792 |
if (!err)
|
793 |
break;
|
794 |
} |
795 |
|
796 |
if (err) {
|
797 |
err = -ENOENT; |
798 |
goto fail;
|
799 |
} |
800 |
|
801 |
/* first event is at time 0 */
|
802 |
atexit(quit_timers); |
803 |
t->pending = 1;
|
804 |
alarm_timer = t; |
805 |
|
806 |
return 0; |
807 |
|
808 |
fail:
|
809 |
return err;
|
810 |
} |
811 |
|