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