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