]>
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 | ||
bff9f8bf SW |
32 | #include "qemu-timer.h" |
33 | ||
44459349 JL |
34 | #ifdef __FreeBSD__ |
35 | #include <sys/param.h> | |
36 | #endif | |
db1a4972 | 37 | |
db1a4972 | 38 | #ifdef _WIN32 |
db1a4972 PB |
39 | #include <mmsystem.h> |
40 | #endif | |
41 | ||
db1a4972 PB |
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; | |
ab33fcda | 52 | |
688eb389 | 53 | QEMUTimer *active_timers; |
691a0c9c JK |
54 | |
55 | NotifierList reset_notifiers; | |
56 | int64_t last; | |
db1a4972 PB |
57 | }; |
58 | ||
59 | struct QEMUTimer { | |
60 | QEMUClock *clock; | |
4a998740 PB |
61 | int64_t expire_time; /* in nanoseconds */ |
62 | int scale; | |
db1a4972 PB |
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); | |
f3fc6e2e | 72 | void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns); |
cd0544ee SW |
73 | #if defined(__linux__) |
74 | int fd; | |
75 | timer_t timer; | |
76 | #elif defined(_WIN32) | |
77 | HANDLE timer; | |
78 | #endif | |
db1a4972 PB |
79 | char expired; |
80 | char pending; | |
81 | }; | |
82 | ||
83 | static struct qemu_alarm_timer *alarm_timer; | |
84 | ||
45c7b37f SW |
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 | ||
db1a4972 PB |
90 | static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
91 | { | |
92 | return !!t->rearm; | |
93 | } | |
94 | ||
f3fc6e2e PB |
95 | static int64_t qemu_next_alarm_deadline(void) |
96 | { | |
4ffd16fc | 97 | int64_t delta = INT64_MAX; |
f3fc6e2e PB |
98 | int64_t rtdelta; |
99 | ||
4ffd16fc | 100 | if (!use_icount && vm_clock->enabled && vm_clock->active_timers) { |
f3fc6e2e PB |
101 | delta = vm_clock->active_timers->expire_time - |
102 | qemu_get_clock_ns(vm_clock); | |
f3fc6e2e | 103 | } |
4ffd16fc | 104 | if (host_clock->enabled && host_clock->active_timers) { |
f3fc6e2e PB |
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 | } | |
4ffd16fc | 111 | if (rt_clock->enabled && rt_clock->active_timers) { |
f3fc6e2e PB |
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 | ||
db1a4972 PB |
122 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
123 | { | |
f3fc6e2e PB |
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) { | |
db1a4972 | 129 | return; |
f3fc6e2e PB |
130 | } |
131 | nearest_delta_ns = qemu_next_alarm_deadline(); | |
132 | t->rearm(t, nearest_delta_ns); | |
db1a4972 PB |
133 | } |
134 | ||
9c13246a PB |
135 | /* TODO: MIN_TIMER_REARM_NS should be optimized */ |
136 | #define MIN_TIMER_REARM_NS 250000 | |
db1a4972 PB |
137 | |
138 | #ifdef _WIN32 | |
139 | ||
2f9cba0c SW |
140 | static int mm_start_timer(struct qemu_alarm_timer *t); |
141 | static void mm_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 142 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
2f9cba0c | 143 | |
db1a4972 PB |
144 | static int win32_start_timer(struct qemu_alarm_timer *t); |
145 | static void win32_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 146 | static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
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); | |
f3fc6e2e | 152 | static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
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); | |
f3fc6e2e | 158 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 | 159 | |
db1a4972 PB |
160 | #endif /* __linux__ */ |
161 | ||
162 | #endif /* _WIN32 */ | |
163 | ||
db1a4972 PB |
164 | static struct qemu_alarm_timer alarm_timers[] = { |
165 | #ifndef _WIN32 | |
166 | #ifdef __linux__ | |
167 | {"dynticks", dynticks_start_timer, | |
cd0544ee | 168 | dynticks_stop_timer, dynticks_rearm_timer}, |
db1a4972 | 169 | #endif |
84682834 | 170 | {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer}, |
db1a4972 | 171 | #else |
cca5de73 | 172 | {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer}, |
cd0544ee | 173 | {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer}, |
db1a4972 PB |
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 | ||
7267c094 | 201 | arg = g_strdup(opt); |
db1a4972 PB |
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 | ||
7267c094 | 230 | g_free(arg); |
db1a4972 PB |
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 | ||
db1a4972 PB |
242 | QEMUClock *rt_clock; |
243 | QEMUClock *vm_clock; | |
244 | QEMUClock *host_clock; | |
245 | ||
db1a4972 PB |
246 | static QEMUClock *qemu_new_clock(int type) |
247 | { | |
248 | QEMUClock *clock; | |
691a0c9c | 249 | |
7267c094 | 250 | clock = g_malloc0(sizeof(QEMUClock)); |
db1a4972 PB |
251 | clock->type = type; |
252 | clock->enabled = 1; | |
2ff68d07 | 253 | clock->last = INT64_MIN; |
691a0c9c | 254 | notifier_list_init(&clock->reset_notifiers); |
db1a4972 PB |
255 | return clock; |
256 | } | |
257 | ||
258 | void qemu_clock_enable(QEMUClock *clock, int enabled) | |
259 | { | |
fbdc14eb | 260 | bool old = clock->enabled; |
db1a4972 | 261 | clock->enabled = enabled; |
fbdc14eb PB |
262 | if (enabled && !old) { |
263 | qemu_rearm_alarm_timer(alarm_timer); | |
264 | } | |
db1a4972 PB |
265 | } |
266 | ||
dc2dfcf0 PB |
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 | ||
4a998740 PB |
292 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, |
293 | QEMUTimerCB *cb, void *opaque) | |
db1a4972 PB |
294 | { |
295 | QEMUTimer *ts; | |
296 | ||
7267c094 | 297 | ts = g_malloc0(sizeof(QEMUTimer)); |
db1a4972 PB |
298 | ts->clock = clock; |
299 | ts->cb = cb; | |
300 | ts->opaque = opaque; | |
4a998740 | 301 | ts->scale = scale; |
db1a4972 PB |
302 | return ts; |
303 | } | |
304 | ||
305 | void qemu_free_timer(QEMUTimer *ts) | |
306 | { | |
7267c094 | 307 | g_free(ts); |
db1a4972 PB |
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. */ | |
688eb389 | 317 | pt = &ts->clock->active_timers; |
db1a4972 PB |
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. */ | |
2ff68d07 | 332 | void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) |
db1a4972 PB |
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. */ | |
688eb389 | 341 | pt = &ts->clock->active_timers; |
db1a4972 PB |
342 | for(;;) { |
343 | t = *pt; | |
45c7b37f | 344 | if (!qemu_timer_expired_ns(t, expire_time)) { |
db1a4972 | 345 | break; |
45c7b37f | 346 | } |
db1a4972 PB |
347 | pt = &t->next; |
348 | } | |
349 | ts->expire_time = expire_time; | |
350 | ts->next = *pt; | |
351 | *pt = ts; | |
352 | ||
353 | /* Rearm if necessary */ | |
688eb389 | 354 | if (pt == &ts->clock->active_timers) { |
db1a4972 PB |
355 | if (!alarm_timer->pending) { |
356 | qemu_rearm_alarm_timer(alarm_timer); | |
357 | } | |
358 | /* Interrupt execution to force deadline recalculation. */ | |
ab33fcda PB |
359 | qemu_clock_warp(ts->clock); |
360 | if (use_icount) { | |
db1a4972 | 361 | qemu_notify_event(); |
ab33fcda | 362 | } |
db1a4972 PB |
363 | } |
364 | } | |
365 | ||
4a998740 PB |
366 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
367 | { | |
368 | qemu_mod_timer_ns(ts, expire_time * ts->scale); | |
369 | } | |
370 | ||
db1a4972 PB |
371 | int qemu_timer_pending(QEMUTimer *ts) |
372 | { | |
373 | QEMUTimer *t; | |
688eb389 | 374 | for (t = ts->clock->active_timers; t != NULL; t = t->next) { |
db1a4972 PB |
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 | { | |
45c7b37f | 383 | return qemu_timer_expired_ns(timer_head, current_time * timer_head->scale); |
db1a4972 PB |
384 | } |
385 | ||
8156be56 | 386 | void qemu_run_timers(QEMUClock *clock) |
db1a4972 PB |
387 | { |
388 | QEMUTimer **ptimer_head, *ts; | |
389 | int64_t current_time; | |
390 | ||
391 | if (!clock->enabled) | |
392 | return; | |
393 | ||
4a998740 | 394 | current_time = qemu_get_clock_ns(clock); |
688eb389 | 395 | ptimer_head = &clock->active_timers; |
db1a4972 PB |
396 | for(;;) { |
397 | ts = *ptimer_head; | |
45c7b37f | 398 | if (!qemu_timer_expired_ns(ts, current_time)) { |
db1a4972 | 399 | break; |
45c7b37f | 400 | } |
db1a4972 PB |
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 | ||
db1a4972 PB |
410 | int64_t qemu_get_clock_ns(QEMUClock *clock) |
411 | { | |
691a0c9c JK |
412 | int64_t now, last; |
413 | ||
db1a4972 PB |
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: | |
691a0c9c JK |
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; | |
db1a4972 PB |
432 | } |
433 | } | |
434 | ||
691a0c9c JK |
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 | { | |
31552529 | 442 | notifier_remove(notifier); |
691a0c9c JK |
443 | } |
444 | ||
db1a4972 PB |
445 | void init_clocks(void) |
446 | { | |
db1a4972 PB |
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); | |
db1a4972 PB |
450 | } |
451 | ||
2ff68d07 | 452 | uint64_t qemu_timer_expire_time_ns(QEMUTimer *ts) |
db1a4972 | 453 | { |
2ff68d07 | 454 | return qemu_timer_pending(ts) ? ts->expire_time : -1; |
db1a4972 PB |
455 | } |
456 | ||
db1a4972 PB |
457 | void qemu_run_all_timers(void) |
458 | { | |
ca5a2a4b PB |
459 | alarm_timer->pending = 0; |
460 | ||
158fd3ce PP |
461 | /* vm time timers */ |
462 | qemu_run_timers(vm_clock); | |
463 | qemu_run_timers(rt_clock); | |
464 | qemu_run_timers(host_clock); | |
465 | ||
db1a4972 PB |
466 | /* rearm timer, if not periodic */ |
467 | if (alarm_timer->expired) { | |
468 | alarm_timer->expired = 0; | |
469 | qemu_rearm_alarm_timer(alarm_timer); | |
470 | } | |
db1a4972 PB |
471 | } |
472 | ||
473 | #ifdef _WIN32 | |
68c23e55 | 474 | static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
db1a4972 PB |
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 | ||
db1a4972 | 483 | if (alarm_has_dynticks(t) || |
4c3d45eb | 484 | qemu_next_alarm_deadline () <= 0) { |
db1a4972 PB |
485 | t->expired = alarm_has_dynticks(t); |
486 | t->pending = 1; | |
487 | qemu_notify_event(); | |
488 | } | |
489 | } | |
490 | ||
4c3d45eb PB |
491 | #if defined(__linux__) |
492 | ||
d25f89c9 JK |
493 | #include "compatfd.h" |
494 | ||
db1a4972 PB |
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; | |
d25f89c9 JK |
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 */ | |
db1a4972 PB |
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 | ||
cd0544ee | 531 | t->timer = host_timer; |
db1a4972 PB |
532 | |
533 | return 0; | |
534 | } | |
535 | ||
536 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) | |
537 | { | |
cd0544ee | 538 | timer_t host_timer = t->timer; |
db1a4972 PB |
539 | |
540 | timer_delete(host_timer); | |
541 | } | |
542 | ||
f3fc6e2e PB |
543 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, |
544 | int64_t nearest_delta_ns) | |
db1a4972 | 545 | { |
cd0544ee | 546 | timer_t host_timer = t->timer; |
db1a4972 | 547 | struct itimerspec timeout; |
9c13246a | 548 | int64_t current_ns; |
db1a4972 | 549 | |
4c3d45eb PB |
550 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
551 | nearest_delta_ns = MIN_TIMER_REARM_NS; | |
db1a4972 PB |
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 | } | |
9c13246a PB |
559 | current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; |
560 | if (current_ns && current_ns <= nearest_delta_ns) | |
db1a4972 PB |
561 | return; |
562 | ||
563 | timeout.it_interval.tv_sec = 0; | |
564 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ | |
9c13246a PB |
565 | timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; |
566 | timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; | |
db1a4972 PB |
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 | ||
f26e5a54 SW |
576 | #if !defined(_WIN32) |
577 | ||
db1a4972 PB |
578 | static int unix_start_timer(struct qemu_alarm_timer *t) |
579 | { | |
580 | struct sigaction act; | |
db1a4972 PB |
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); | |
84682834 PB |
588 | return 0; |
589 | } | |
db1a4972 | 590 | |
f3fc6e2e PB |
591 | static void unix_rearm_timer(struct qemu_alarm_timer *t, |
592 | int64_t nearest_delta_ns) | |
84682834 PB |
593 | { |
594 | struct itimerval itv; | |
84682834 | 595 | int err; |
db1a4972 | 596 | |
84682834 PB |
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 | } | |
db1a4972 PB |
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 | ||
2f9cba0c SW |
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 | ||
f3fc6e2e | 683 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta) |
2f9cba0c | 684 | { |
5bfb723f | 685 | int64_t nearest_delta_ms = delta / 1000000; |
2f9cba0c SW |
686 | if (nearest_delta_ms < 1) { |
687 | nearest_delta_ms = 1; | |
688 | } | |
5bfb723f SS |
689 | /* UINT_MAX can be 32 bit */ |
690 | if (nearest_delta_ms > UINT_MAX) { | |
691 | nearest_delta_ms = UINT_MAX; | |
692 | } | |
f3fc6e2e PB |
693 | |
694 | timeKillEvent(mm_timer); | |
5bfb723f | 695 | mm_timer = timeSetEvent((unsigned int) nearest_delta_ms, |
2f9cba0c SW |
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 | ||
db1a4972 PB |
710 | static int win32_start_timer(struct qemu_alarm_timer *t) |
711 | { | |
68c23e55 PB |
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) { | |
db1a4972 PB |
728 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
729 | GetLastError()); | |
db1a4972 PB |
730 | return -1; |
731 | } | |
732 | ||
cd0544ee | 733 | t->timer = hTimer; |
db1a4972 PB |
734 | return 0; |
735 | } | |
736 | ||
737 | static void win32_stop_timer(struct qemu_alarm_timer *t) | |
738 | { | |
cd0544ee | 739 | HANDLE hTimer = t->timer; |
db1a4972 | 740 | |
68c23e55 PB |
741 | if (hTimer) { |
742 | DeleteTimerQueueTimer(NULL, hTimer, NULL); | |
743 | } | |
db1a4972 PB |
744 | } |
745 | ||
f3fc6e2e PB |
746 | static void win32_rearm_timer(struct qemu_alarm_timer *t, |
747 | int64_t nearest_delta_ns) | |
db1a4972 | 748 | { |
cd0544ee | 749 | HANDLE hTimer = t->timer; |
5bfb723f | 750 | int64_t nearest_delta_ms; |
68c23e55 | 751 | BOOLEAN success; |
db1a4972 | 752 | |
5bfb723f | 753 | nearest_delta_ms = nearest_delta_ns / 1000000; |
cfced5b2 PB |
754 | if (nearest_delta_ms < 1) { |
755 | nearest_delta_ms = 1; | |
756 | } | |
5bfb723f SS |
757 | /* ULONG_MAX can be 32 bit */ |
758 | if (nearest_delta_ms > ULONG_MAX) { | |
759 | nearest_delta_ms = ULONG_MAX; | |
760 | } | |
68c23e55 PB |
761 | success = ChangeTimerQueueTimer(NULL, |
762 | hTimer, | |
5bfb723f | 763 | (unsigned long) nearest_delta_ms, |
68c23e55 | 764 | 3600000); |
db1a4972 | 765 | |
68c23e55 PB |
766 | if (!success) { |
767 | fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n", | |
768 | GetLastError()); | |
769 | exit(-1); | |
db1a4972 | 770 | } |
68c23e55 | 771 | |
db1a4972 PB |
772 | } |
773 | ||
774 | #endif /* _WIN32 */ | |
775 | ||
4260a739 PB |
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 | ||
db1a4972 PB |
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 */ | |
4260a739 | 802 | atexit(quit_timers); |
db1a4972 PB |
803 | t->pending = 1; |
804 | alarm_timer = t; | |
db1a4972 PB |
805 | |
806 | return 0; | |
807 | ||
808 | fail: | |
809 | return err; | |
810 | } | |
811 |