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