]>
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 | ||
9c17d615 | 25 | #include "sysemu/sysemu.h" |
83c9089e | 26 | #include "monitor/monitor.h" |
28ecbaee | 27 | #include "ui/console.h" |
db1a4972 PB |
28 | |
29 | #include "hw/hw.h" | |
30 | ||
1de7afc9 | 31 | #include "qemu/timer.h" |
30ea8339 AL |
32 | #ifdef CONFIG_POSIX |
33 | #include <pthread.h> | |
34 | #endif | |
bff9f8bf | 35 | |
db1a4972 | 36 | #ifdef _WIN32 |
db1a4972 PB |
37 | #include <mmsystem.h> |
38 | #endif | |
39 | ||
4e0c6529 AB |
40 | #ifdef CONFIG_PPOLL |
41 | #include <poll.h> | |
42 | #endif | |
43 | ||
cd758dd0 AB |
44 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
45 | #include <sys/prctl.h> | |
46 | #endif | |
47 | ||
db1a4972 PB |
48 | /***********************************************************/ |
49 | /* timers */ | |
50 | ||
db1a4972 | 51 | struct QEMUClock { |
ff83c66e AB |
52 | QEMUTimerList *main_loop_timerlist; |
53 | QLIST_HEAD(, QEMUTimerList) timerlists; | |
691a0c9c JK |
54 | |
55 | NotifierList reset_notifiers; | |
56 | int64_t last; | |
9a14b298 | 57 | |
ff83c66e | 58 | QEMUClockType type; |
9a14b298 | 59 | bool enabled; |
db1a4972 PB |
60 | }; |
61 | ||
754d6a54 | 62 | QEMUTimerListGroup main_loop_tlg; |
ff83c66e AB |
63 | QEMUClock *qemu_clocks[QEMU_CLOCK_MAX]; |
64 | ||
65 | /* A QEMUTimerList is a list of timers attached to a clock. More | |
66 | * than one QEMUTimerList can be attached to each clock, for instance | |
67 | * used by different AioContexts / threads. Each clock also has | |
68 | * a list of the QEMUTimerLists associated with it, in order that | |
69 | * reenabling the clock can call all the notifiers. | |
70 | */ | |
71 | ||
72 | struct QEMUTimerList { | |
9a14b298 | 73 | QEMUClock *clock; |
ff83c66e AB |
74 | QEMUTimer *active_timers; |
75 | QLIST_ENTRY(QEMUTimerList) list; | |
d5541d86 AB |
76 | QEMUTimerListNotifyCB *notify_cb; |
77 | void *notify_opaque; | |
db1a4972 PB |
78 | }; |
79 | ||
80 | struct qemu_alarm_timer { | |
81 | char const *name; | |
82 | int (*start)(struct qemu_alarm_timer *t); | |
83 | void (*stop)(struct qemu_alarm_timer *t); | |
f3fc6e2e | 84 | void (*rearm)(struct qemu_alarm_timer *t, int64_t nearest_delta_ns); |
cd0544ee | 85 | #if defined(__linux__) |
cd0544ee | 86 | timer_t timer; |
9a14b298 | 87 | int fd; |
cd0544ee SW |
88 | #elif defined(_WIN32) |
89 | HANDLE timer; | |
90 | #endif | |
5e1ec7b2 SW |
91 | bool expired; |
92 | bool pending; | |
db1a4972 PB |
93 | }; |
94 | ||
95 | static struct qemu_alarm_timer *alarm_timer; | |
96 | ||
e93379b0 | 97 | static bool timer_expired_ns(QEMUTimer *timer_head, int64_t current_time) |
45c7b37f SW |
98 | { |
99 | return timer_head && (timer_head->expire_time <= current_time); | |
100 | } | |
101 | ||
f3fc6e2e PB |
102 | static int64_t qemu_next_alarm_deadline(void) |
103 | { | |
4ffd16fc | 104 | int64_t delta = INT64_MAX; |
f3fc6e2e | 105 | int64_t rtdelta; |
ff83c66e | 106 | int64_t hdelta; |
f3fc6e2e | 107 | |
ff83c66e AB |
108 | if (!use_icount && vm_clock->enabled && |
109 | vm_clock->main_loop_timerlist->active_timers) { | |
110 | delta = vm_clock->main_loop_timerlist->active_timers->expire_time - | |
111 | qemu_get_clock_ns(vm_clock); | |
f3fc6e2e | 112 | } |
ff83c66e AB |
113 | if (host_clock->enabled && |
114 | host_clock->main_loop_timerlist->active_timers) { | |
115 | hdelta = host_clock->main_loop_timerlist->active_timers->expire_time - | |
116 | qemu_get_clock_ns(host_clock); | |
f3fc6e2e PB |
117 | if (hdelta < delta) { |
118 | delta = hdelta; | |
119 | } | |
120 | } | |
ff83c66e AB |
121 | if (rt_clock->enabled && |
122 | rt_clock->main_loop_timerlist->active_timers) { | |
123 | rtdelta = (rt_clock->main_loop_timerlist->active_timers->expire_time - | |
124 | qemu_get_clock_ns(rt_clock)); | |
f3fc6e2e PB |
125 | if (rtdelta < delta) { |
126 | delta = rtdelta; | |
127 | } | |
128 | } | |
129 | ||
130 | return delta; | |
131 | } | |
132 | ||
db1a4972 PB |
133 | static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
134 | { | |
8227421e SS |
135 | int64_t nearest_delta_ns = qemu_next_alarm_deadline(); |
136 | if (nearest_delta_ns < INT64_MAX) { | |
137 | t->rearm(t, nearest_delta_ns); | |
f3fc6e2e | 138 | } |
db1a4972 PB |
139 | } |
140 | ||
9c13246a PB |
141 | /* TODO: MIN_TIMER_REARM_NS should be optimized */ |
142 | #define MIN_TIMER_REARM_NS 250000 | |
db1a4972 PB |
143 | |
144 | #ifdef _WIN32 | |
145 | ||
2f9cba0c SW |
146 | static int mm_start_timer(struct qemu_alarm_timer *t); |
147 | static void mm_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 148 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
2f9cba0c | 149 | |
db1a4972 PB |
150 | static int win32_start_timer(struct qemu_alarm_timer *t); |
151 | static void win32_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 152 | static void win32_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
153 | |
154 | #else | |
155 | ||
156 | static int unix_start_timer(struct qemu_alarm_timer *t); | |
157 | static void unix_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 158 | static void unix_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 PB |
159 | |
160 | #ifdef __linux__ | |
161 | ||
162 | static int dynticks_start_timer(struct qemu_alarm_timer *t); | |
163 | static void dynticks_stop_timer(struct qemu_alarm_timer *t); | |
f3fc6e2e | 164 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, int64_t delta); |
db1a4972 | 165 | |
db1a4972 PB |
166 | #endif /* __linux__ */ |
167 | ||
168 | #endif /* _WIN32 */ | |
169 | ||
db1a4972 PB |
170 | static struct qemu_alarm_timer alarm_timers[] = { |
171 | #ifndef _WIN32 | |
172 | #ifdef __linux__ | |
173 | {"dynticks", dynticks_start_timer, | |
cd0544ee | 174 | dynticks_stop_timer, dynticks_rearm_timer}, |
db1a4972 | 175 | #endif |
84682834 | 176 | {"unix", unix_start_timer, unix_stop_timer, unix_rearm_timer}, |
db1a4972 | 177 | #else |
cca5de73 | 178 | {"mmtimer", mm_start_timer, mm_stop_timer, mm_rearm_timer}, |
cd0544ee | 179 | {"dynticks", win32_start_timer, win32_stop_timer, win32_rearm_timer}, |
db1a4972 PB |
180 | #endif |
181 | {NULL, } | |
182 | }; | |
183 | ||
184 | static void show_available_alarms(void) | |
185 | { | |
186 | int i; | |
187 | ||
188 | printf("Available alarm timers, in order of precedence:\n"); | |
189 | for (i = 0; alarm_timers[i].name; i++) | |
190 | printf("%s\n", alarm_timers[i].name); | |
191 | } | |
192 | ||
193 | void configure_alarms(char const *opt) | |
194 | { | |
195 | int i; | |
196 | int cur = 0; | |
197 | int count = ARRAY_SIZE(alarm_timers) - 1; | |
198 | char *arg; | |
199 | char *name; | |
200 | struct qemu_alarm_timer tmp; | |
201 | ||
c8057f95 | 202 | if (is_help_option(opt)) { |
db1a4972 PB |
203 | show_available_alarms(); |
204 | exit(0); | |
205 | } | |
206 | ||
7267c094 | 207 | arg = g_strdup(opt); |
db1a4972 PB |
208 | |
209 | /* Reorder the array */ | |
210 | name = strtok(arg, ","); | |
211 | while (name) { | |
212 | for (i = 0; i < count && alarm_timers[i].name; i++) { | |
213 | if (!strcmp(alarm_timers[i].name, name)) | |
214 | break; | |
215 | } | |
216 | ||
217 | if (i == count) { | |
218 | fprintf(stderr, "Unknown clock %s\n", name); | |
219 | goto next; | |
220 | } | |
221 | ||
222 | if (i < cur) | |
223 | /* Ignore */ | |
224 | goto next; | |
225 | ||
226 | /* Swap */ | |
227 | tmp = alarm_timers[i]; | |
228 | alarm_timers[i] = alarm_timers[cur]; | |
229 | alarm_timers[cur] = tmp; | |
230 | ||
231 | cur++; | |
232 | next: | |
233 | name = strtok(NULL, ","); | |
234 | } | |
235 | ||
7267c094 | 236 | g_free(arg); |
db1a4972 PB |
237 | |
238 | if (cur) { | |
239 | /* Disable remaining timers */ | |
240 | for (i = cur; i < count; i++) | |
241 | alarm_timers[i].name = NULL; | |
242 | } else { | |
243 | show_available_alarms(); | |
244 | exit(1); | |
245 | } | |
246 | } | |
247 | ||
d5541d86 AB |
248 | static QEMUTimerList *timerlist_new_from_clock(QEMUClock *clock, |
249 | QEMUTimerListNotifyCB *cb, | |
250 | void *opaque) | |
ff83c66e AB |
251 | { |
252 | QEMUTimerList *timer_list; | |
253 | ||
254 | /* Assert if we do not have a clock. If you see this | |
255 | * assertion in means that the clocks have not been | |
256 | * initialised before a timerlist is needed. This | |
257 | * normally happens if an AioContext is used before | |
258 | * init_clocks() is called within main(). | |
259 | */ | |
260 | assert(clock); | |
261 | ||
262 | timer_list = g_malloc0(sizeof(QEMUTimerList)); | |
263 | timer_list->clock = clock; | |
d5541d86 AB |
264 | timer_list->notify_cb = cb; |
265 | timer_list->notify_opaque = opaque; | |
ff83c66e AB |
266 | QLIST_INSERT_HEAD(&clock->timerlists, timer_list, list); |
267 | return timer_list; | |
268 | } | |
269 | ||
d5541d86 AB |
270 | QEMUTimerList *timerlist_new(QEMUClockType type, |
271 | QEMUTimerListNotifyCB *cb, void *opaque) | |
ff83c66e | 272 | { |
d5541d86 | 273 | return timerlist_new_from_clock(qemu_clock_ptr(type), cb, opaque); |
ff83c66e | 274 | } |
db1a4972 | 275 | |
ff83c66e AB |
276 | void timerlist_free(QEMUTimerList *timer_list) |
277 | { | |
278 | assert(!timerlist_has_timers(timer_list)); | |
279 | if (timer_list->clock) { | |
280 | QLIST_REMOVE(timer_list, list); | |
281 | if (timer_list->clock->main_loop_timerlist == timer_list) { | |
282 | timer_list->clock->main_loop_timerlist = NULL; | |
283 | } | |
284 | } | |
285 | g_free(timer_list); | |
286 | } | |
287 | ||
288 | static QEMUClock *qemu_clock_new(QEMUClockType type) | |
db1a4972 PB |
289 | { |
290 | QEMUClock *clock; | |
691a0c9c | 291 | |
7267c094 | 292 | clock = g_malloc0(sizeof(QEMUClock)); |
db1a4972 | 293 | clock->type = type; |
5e1ec7b2 | 294 | clock->enabled = true; |
2ff68d07 | 295 | clock->last = INT64_MIN; |
ff83c66e | 296 | QLIST_INIT(&clock->timerlists); |
691a0c9c | 297 | notifier_list_init(&clock->reset_notifiers); |
d5541d86 | 298 | clock->main_loop_timerlist = timerlist_new_from_clock(clock, NULL, NULL); |
db1a4972 PB |
299 | return clock; |
300 | } | |
301 | ||
ff83c66e AB |
302 | bool qemu_clock_use_for_deadline(QEMUClock *clock) |
303 | { | |
304 | return !(use_icount && (clock->type == QEMU_CLOCK_VIRTUAL)); | |
305 | } | |
306 | ||
b1bbfe72 AB |
307 | void qemu_clock_notify(QEMUClock *clock) |
308 | { | |
309 | QEMUTimerList *timer_list; | |
310 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { | |
311 | timerlist_notify(timer_list); | |
312 | } | |
313 | } | |
314 | ||
5e1ec7b2 | 315 | void qemu_clock_enable(QEMUClock *clock, bool enabled) |
db1a4972 | 316 | { |
fbdc14eb | 317 | bool old = clock->enabled; |
db1a4972 | 318 | clock->enabled = enabled; |
fbdc14eb | 319 | if (enabled && !old) { |
b1bbfe72 | 320 | qemu_clock_notify(clock); |
fbdc14eb PB |
321 | qemu_rearm_alarm_timer(alarm_timer); |
322 | } | |
db1a4972 PB |
323 | } |
324 | ||
ff83c66e | 325 | bool timerlist_has_timers(QEMUTimerList *timer_list) |
dc2dfcf0 | 326 | { |
ff83c66e | 327 | return !!timer_list->active_timers; |
dc2dfcf0 PB |
328 | } |
329 | ||
ff83c66e | 330 | bool qemu_clock_has_timers(QEMUClock *clock) |
dc2dfcf0 | 331 | { |
ff83c66e | 332 | return timerlist_has_timers(clock->main_loop_timerlist); |
dc2dfcf0 PB |
333 | } |
334 | ||
ff83c66e AB |
335 | bool timerlist_expired(QEMUTimerList *timer_list) |
336 | { | |
337 | return (timer_list->active_timers && | |
338 | timer_list->active_timers->expire_time < | |
339 | qemu_get_clock_ns(timer_list->clock)); | |
340 | } | |
341 | ||
342 | bool qemu_clock_expired(QEMUClock *clock) | |
343 | { | |
344 | return timerlist_expired(clock->main_loop_timerlist); | |
345 | } | |
346 | ||
347 | int64_t timerlist_deadline(QEMUTimerList *timer_list) | |
dc2dfcf0 PB |
348 | { |
349 | /* To avoid problems with overflow limit this to 2^32. */ | |
350 | int64_t delta = INT32_MAX; | |
351 | ||
ff83c66e AB |
352 | if (timer_list->clock->enabled && timer_list->active_timers) { |
353 | delta = timer_list->active_timers->expire_time - | |
354 | qemu_get_clock_ns(timer_list->clock); | |
dc2dfcf0 PB |
355 | } |
356 | if (delta < 0) { | |
357 | delta = 0; | |
358 | } | |
359 | return delta; | |
360 | } | |
361 | ||
ff83c66e AB |
362 | int64_t qemu_clock_deadline(QEMUClock *clock) |
363 | { | |
364 | return timerlist_deadline(clock->main_loop_timerlist); | |
365 | } | |
366 | ||
02a03a9f AB |
367 | /* |
368 | * As above, but return -1 for no deadline, and do not cap to 2^32 | |
369 | * as we know the result is always positive. | |
370 | */ | |
371 | ||
ff83c66e | 372 | int64_t timerlist_deadline_ns(QEMUTimerList *timer_list) |
02a03a9f AB |
373 | { |
374 | int64_t delta; | |
375 | ||
ff83c66e | 376 | if (!timer_list->clock->enabled || !timer_list->active_timers) { |
02a03a9f AB |
377 | return -1; |
378 | } | |
379 | ||
ff83c66e AB |
380 | delta = timer_list->active_timers->expire_time - |
381 | qemu_get_clock_ns(timer_list->clock); | |
02a03a9f AB |
382 | |
383 | if (delta <= 0) { | |
384 | return 0; | |
385 | } | |
386 | ||
387 | return delta; | |
388 | } | |
389 | ||
ff83c66e AB |
390 | int64_t qemu_clock_deadline_ns(QEMUClock *clock) |
391 | { | |
392 | return timerlist_deadline_ns(clock->main_loop_timerlist); | |
393 | } | |
394 | ||
ac70aafc AB |
395 | /* Calculate the soonest deadline across all timerlists attached |
396 | * to the clock. This is used for the icount timeout so we | |
397 | * ignore whether or not the clock should be used in deadline | |
398 | * calculations. | |
399 | */ | |
400 | int64_t qemu_clock_deadline_ns_all(QEMUClock *clock) | |
401 | { | |
402 | int64_t deadline = -1; | |
403 | QEMUTimerList *timer_list; | |
404 | QLIST_FOREACH(timer_list, &clock->timerlists, list) { | |
405 | deadline = qemu_soonest_timeout(deadline, | |
406 | timerlist_deadline_ns(timer_list)); | |
407 | } | |
408 | return deadline; | |
409 | } | |
410 | ||
ff83c66e AB |
411 | QEMUClock *timerlist_get_clock(QEMUTimerList *timer_list) |
412 | { | |
413 | return timer_list->clock; | |
414 | } | |
415 | ||
416 | QEMUTimerList *qemu_clock_get_main_loop_timerlist(QEMUClock *clock) | |
417 | { | |
418 | return clock->main_loop_timerlist; | |
419 | } | |
420 | ||
d5541d86 AB |
421 | void timerlist_notify(QEMUTimerList *timer_list) |
422 | { | |
423 | if (timer_list->notify_cb) { | |
424 | timer_list->notify_cb(timer_list->notify_opaque); | |
425 | } else { | |
426 | qemu_notify_event(); | |
427 | } | |
428 | } | |
429 | ||
02a03a9f AB |
430 | /* Transition function to convert a nanosecond timeout to ms |
431 | * This is used where a system does not support ppoll | |
432 | */ | |
433 | int qemu_timeout_ns_to_ms(int64_t ns) | |
434 | { | |
435 | int64_t ms; | |
436 | if (ns < 0) { | |
437 | return -1; | |
438 | } | |
439 | ||
440 | if (!ns) { | |
441 | return 0; | |
442 | } | |
443 | ||
444 | /* Always round up, because it's better to wait too long than to wait too | |
445 | * little and effectively busy-wait | |
446 | */ | |
447 | ms = (ns + SCALE_MS - 1) / SCALE_MS; | |
448 | ||
449 | /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */ | |
450 | if (ms > (int64_t) INT32_MAX) { | |
451 | ms = INT32_MAX; | |
452 | } | |
453 | ||
454 | return (int) ms; | |
455 | } | |
456 | ||
457 | ||
4e0c6529 AB |
458 | /* qemu implementation of g_poll which uses a nanosecond timeout but is |
459 | * otherwise identical to g_poll | |
460 | */ | |
461 | int qemu_poll_ns(GPollFD *fds, guint nfds, int64_t timeout) | |
462 | { | |
463 | #ifdef CONFIG_PPOLL | |
464 | if (timeout < 0) { | |
465 | return ppoll((struct pollfd *)fds, nfds, NULL, NULL); | |
466 | } else { | |
467 | struct timespec ts; | |
468 | ts.tv_sec = timeout / 1000000000LL; | |
469 | ts.tv_nsec = timeout % 1000000000LL; | |
470 | return ppoll((struct pollfd *)fds, nfds, &ts, NULL); | |
471 | } | |
472 | #else | |
473 | return g_poll(fds, nfds, qemu_timeout_ns_to_ms(timeout)); | |
474 | #endif | |
475 | } | |
476 | ||
477 | ||
ff83c66e AB |
478 | void timer_init(QEMUTimer *ts, |
479 | QEMUTimerList *timer_list, int scale, | |
480 | QEMUTimerCB *cb, void *opaque) | |
db1a4972 | 481 | { |
ff83c66e | 482 | ts->timer_list = timer_list; |
db1a4972 PB |
483 | ts->cb = cb; |
484 | ts->opaque = opaque; | |
4a998740 | 485 | ts->scale = scale; |
ff83c66e AB |
486 | } |
487 | ||
488 | QEMUTimer *qemu_new_timer(QEMUClock *clock, int scale, | |
489 | QEMUTimerCB *cb, void *opaque) | |
490 | { | |
491 | return timer_new_tl(clock->main_loop_timerlist, | |
492 | scale, cb, opaque); | |
db1a4972 PB |
493 | } |
494 | ||
495 | void qemu_free_timer(QEMUTimer *ts) | |
496 | { | |
7267c094 | 497 | g_free(ts); |
db1a4972 PB |
498 | } |
499 | ||
500 | /* stop a timer, but do not dealloc it */ | |
501 | void qemu_del_timer(QEMUTimer *ts) | |
502 | { | |
503 | QEMUTimer **pt, *t; | |
504 | ||
505 | /* NOTE: this code must be signal safe because | |
e93379b0 | 506 | timer_expired() can be called from a signal. */ |
ff83c66e | 507 | pt = &ts->timer_list->active_timers; |
db1a4972 PB |
508 | for(;;) { |
509 | t = *pt; | |
510 | if (!t) | |
511 | break; | |
512 | if (t == ts) { | |
513 | *pt = t->next; | |
514 | break; | |
515 | } | |
516 | pt = &t->next; | |
517 | } | |
518 | } | |
519 | ||
520 | /* modify the current timer so that it will be fired when current_time | |
521 | >= expire_time. The corresponding callback will be called. */ | |
2ff68d07 | 522 | void qemu_mod_timer_ns(QEMUTimer *ts, int64_t expire_time) |
db1a4972 PB |
523 | { |
524 | QEMUTimer **pt, *t; | |
525 | ||
526 | qemu_del_timer(ts); | |
527 | ||
528 | /* add the timer in the sorted list */ | |
529 | /* NOTE: this code must be signal safe because | |
e93379b0 | 530 | timer_expired() can be called from a signal. */ |
ff83c66e | 531 | pt = &ts->timer_list->active_timers; |
db1a4972 PB |
532 | for(;;) { |
533 | t = *pt; | |
e93379b0 | 534 | if (!timer_expired_ns(t, expire_time)) { |
db1a4972 | 535 | break; |
45c7b37f | 536 | } |
db1a4972 PB |
537 | pt = &t->next; |
538 | } | |
539 | ts->expire_time = expire_time; | |
540 | ts->next = *pt; | |
541 | *pt = ts; | |
542 | ||
543 | /* Rearm if necessary */ | |
ff83c66e | 544 | if (pt == &ts->timer_list->active_timers) { |
db1a4972 PB |
545 | if (!alarm_timer->pending) { |
546 | qemu_rearm_alarm_timer(alarm_timer); | |
547 | } | |
548 | /* Interrupt execution to force deadline recalculation. */ | |
ff83c66e | 549 | qemu_clock_warp(ts->timer_list->clock); |
b1bbfe72 | 550 | timerlist_notify(ts->timer_list); |
db1a4972 PB |
551 | } |
552 | } | |
553 | ||
4a998740 PB |
554 | void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time) |
555 | { | |
556 | qemu_mod_timer_ns(ts, expire_time * ts->scale); | |
557 | } | |
558 | ||
e93379b0 | 559 | bool timer_pending(QEMUTimer *ts) |
db1a4972 PB |
560 | { |
561 | QEMUTimer *t; | |
ff83c66e | 562 | for (t = ts->timer_list->active_timers; t != NULL; t = t->next) { |
5e1ec7b2 SW |
563 | if (t == ts) { |
564 | return true; | |
565 | } | |
db1a4972 | 566 | } |
5e1ec7b2 | 567 | return false; |
db1a4972 PB |
568 | } |
569 | ||
e93379b0 | 570 | bool timer_expired(QEMUTimer *timer_head, int64_t current_time) |
db1a4972 | 571 | { |
e93379b0 | 572 | return timer_expired_ns(timer_head, current_time * timer_head->scale); |
db1a4972 PB |
573 | } |
574 | ||
ff83c66e | 575 | bool timerlist_run_timers(QEMUTimerList *timer_list) |
db1a4972 | 576 | { |
144b97c2 | 577 | QEMUTimer *ts; |
db1a4972 | 578 | int64_t current_time; |
f9a976b7 | 579 | bool progress = false; |
db1a4972 | 580 | |
ff83c66e | 581 | if (!timer_list->clock->enabled) { |
f9a976b7 | 582 | return progress; |
ff83c66e | 583 | } |
db1a4972 | 584 | |
ff83c66e | 585 | current_time = qemu_get_clock_ns(timer_list->clock); |
db1a4972 | 586 | for(;;) { |
ff83c66e | 587 | ts = timer_list->active_timers; |
e93379b0 | 588 | if (!timer_expired_ns(ts, current_time)) { |
db1a4972 | 589 | break; |
45c7b37f | 590 | } |
db1a4972 | 591 | /* remove timer from the list before calling the callback */ |
ff83c66e | 592 | timer_list->active_timers = ts->next; |
db1a4972 PB |
593 | ts->next = NULL; |
594 | ||
595 | /* run the callback (the timer list can be modified) */ | |
596 | ts->cb(ts->opaque); | |
f9a976b7 | 597 | progress = true; |
db1a4972 | 598 | } |
f9a976b7 | 599 | return progress; |
db1a4972 PB |
600 | } |
601 | ||
ff83c66e AB |
602 | bool qemu_run_timers(QEMUClock *clock) |
603 | { | |
604 | return timerlist_run_timers(clock->main_loop_timerlist); | |
605 | } | |
606 | ||
d5541d86 AB |
607 | void timerlistgroup_init(QEMUTimerListGroup *tlg, |
608 | QEMUTimerListNotifyCB *cb, void *opaque) | |
754d6a54 AB |
609 | { |
610 | QEMUClockType type; | |
611 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
d5541d86 | 612 | tlg->tl[type] = timerlist_new(type, cb, opaque); |
754d6a54 AB |
613 | } |
614 | } | |
615 | ||
616 | void timerlistgroup_deinit(QEMUTimerListGroup *tlg) | |
617 | { | |
618 | QEMUClockType type; | |
619 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
620 | timerlist_free(tlg->tl[type]); | |
621 | } | |
622 | } | |
623 | ||
624 | bool timerlistgroup_run_timers(QEMUTimerListGroup *tlg) | |
625 | { | |
626 | QEMUClockType type; | |
627 | bool progress = false; | |
628 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
629 | progress |= timerlist_run_timers(tlg->tl[type]); | |
630 | } | |
631 | return progress; | |
632 | } | |
633 | ||
634 | int64_t timerlistgroup_deadline_ns(QEMUTimerListGroup *tlg) | |
635 | { | |
636 | int64_t deadline = -1; | |
637 | QEMUClockType type; | |
638 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
639 | if (qemu_clock_use_for_deadline(tlg->tl[type]->clock)) { | |
640 | deadline = qemu_soonest_timeout(deadline, | |
641 | timerlist_deadline_ns( | |
642 | tlg->tl[type])); | |
643 | } | |
644 | } | |
645 | return deadline; | |
646 | } | |
647 | ||
db1a4972 PB |
648 | int64_t qemu_get_clock_ns(QEMUClock *clock) |
649 | { | |
691a0c9c JK |
650 | int64_t now, last; |
651 | ||
db1a4972 PB |
652 | switch(clock->type) { |
653 | case QEMU_CLOCK_REALTIME: | |
654 | return get_clock(); | |
655 | default: | |
656 | case QEMU_CLOCK_VIRTUAL: | |
657 | if (use_icount) { | |
658 | return cpu_get_icount(); | |
659 | } else { | |
660 | return cpu_get_clock(); | |
661 | } | |
662 | case QEMU_CLOCK_HOST: | |
691a0c9c JK |
663 | now = get_clock_realtime(); |
664 | last = clock->last; | |
665 | clock->last = now; | |
666 | if (now < last) { | |
667 | notifier_list_notify(&clock->reset_notifiers, &now); | |
668 | } | |
669 | return now; | |
db1a4972 PB |
670 | } |
671 | } | |
672 | ||
691a0c9c JK |
673 | void qemu_register_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) |
674 | { | |
675 | notifier_list_add(&clock->reset_notifiers, notifier); | |
676 | } | |
677 | ||
678 | void qemu_unregister_clock_reset_notifier(QEMUClock *clock, Notifier *notifier) | |
679 | { | |
31552529 | 680 | notifier_remove(notifier); |
691a0c9c JK |
681 | } |
682 | ||
db1a4972 PB |
683 | void init_clocks(void) |
684 | { | |
ff83c66e AB |
685 | QEMUClockType type; |
686 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
687 | if (!qemu_clocks[type]) { | |
688 | qemu_clocks[type] = qemu_clock_new(type); | |
754d6a54 | 689 | main_loop_tlg.tl[type] = qemu_clocks[type]->main_loop_timerlist; |
ff83c66e | 690 | } |
744ca8e3 | 691 | } |
ff83c66e | 692 | |
cd758dd0 AB |
693 | #ifdef CONFIG_PRCTL_PR_SET_TIMERSLACK |
694 | prctl(PR_SET_TIMERSLACK, 1, 0, 0, 0); | |
695 | #endif | |
db1a4972 PB |
696 | } |
697 | ||
e93379b0 | 698 | uint64_t timer_expire_time_ns(QEMUTimer *ts) |
db1a4972 | 699 | { |
e93379b0 | 700 | return timer_pending(ts) ? ts->expire_time : -1; |
db1a4972 PB |
701 | } |
702 | ||
f9a976b7 | 703 | bool qemu_run_all_timers(void) |
db1a4972 | 704 | { |
f9a976b7 | 705 | bool progress = false; |
5e1ec7b2 | 706 | alarm_timer->pending = false; |
ca5a2a4b | 707 | |
158fd3ce | 708 | /* vm time timers */ |
ff83c66e AB |
709 | QEMUClockType type; |
710 | for (type = 0; type < QEMU_CLOCK_MAX; type++) { | |
711 | progress |= qemu_run_timers(qemu_clock_ptr(type)); | |
712 | } | |
158fd3ce | 713 | |
db1a4972 PB |
714 | /* rearm timer, if not periodic */ |
715 | if (alarm_timer->expired) { | |
5e1ec7b2 | 716 | alarm_timer->expired = false; |
db1a4972 PB |
717 | qemu_rearm_alarm_timer(alarm_timer); |
718 | } | |
f9a976b7 AB |
719 | |
720 | return progress; | |
db1a4972 PB |
721 | } |
722 | ||
723 | #ifdef _WIN32 | |
68c23e55 | 724 | static void CALLBACK host_alarm_handler(PVOID lpParam, BOOLEAN unused) |
db1a4972 PB |
725 | #else |
726 | static void host_alarm_handler(int host_signum) | |
727 | #endif | |
728 | { | |
729 | struct qemu_alarm_timer *t = alarm_timer; | |
730 | if (!t) | |
731 | return; | |
732 | ||
8205199d SW |
733 | t->expired = true; |
734 | t->pending = true; | |
735 | qemu_notify_event(); | |
db1a4972 PB |
736 | } |
737 | ||
4c3d45eb PB |
738 | #if defined(__linux__) |
739 | ||
1de7afc9 | 740 | #include "qemu/compatfd.h" |
d25f89c9 | 741 | |
db1a4972 PB |
742 | static int dynticks_start_timer(struct qemu_alarm_timer *t) |
743 | { | |
744 | struct sigevent ev; | |
745 | timer_t host_timer; | |
746 | struct sigaction act; | |
747 | ||
748 | sigfillset(&act.sa_mask); | |
749 | act.sa_flags = 0; | |
750 | act.sa_handler = host_alarm_handler; | |
751 | ||
752 | sigaction(SIGALRM, &act, NULL); | |
753 | ||
754 | /* | |
755 | * Initialize ev struct to 0 to avoid valgrind complaining | |
756 | * about uninitialized data in timer_create call | |
757 | */ | |
758 | memset(&ev, 0, sizeof(ev)); | |
759 | ev.sigev_value.sival_int = 0; | |
760 | ev.sigev_notify = SIGEV_SIGNAL; | |
1e9737da | 761 | #ifdef CONFIG_SIGEV_THREAD_ID |
d25f89c9 JK |
762 | if (qemu_signalfd_available()) { |
763 | ev.sigev_notify = SIGEV_THREAD_ID; | |
764 | ev._sigev_un._tid = qemu_get_thread_id(); | |
765 | } | |
1e9737da | 766 | #endif /* CONFIG_SIGEV_THREAD_ID */ |
db1a4972 PB |
767 | ev.sigev_signo = SIGALRM; |
768 | ||
769 | if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) { | |
770 | perror("timer_create"); | |
db1a4972 PB |
771 | return -1; |
772 | } | |
773 | ||
cd0544ee | 774 | t->timer = host_timer; |
db1a4972 PB |
775 | |
776 | return 0; | |
777 | } | |
778 | ||
779 | static void dynticks_stop_timer(struct qemu_alarm_timer *t) | |
780 | { | |
cd0544ee | 781 | timer_t host_timer = t->timer; |
db1a4972 PB |
782 | |
783 | timer_delete(host_timer); | |
784 | } | |
785 | ||
f3fc6e2e PB |
786 | static void dynticks_rearm_timer(struct qemu_alarm_timer *t, |
787 | int64_t nearest_delta_ns) | |
db1a4972 | 788 | { |
cd0544ee | 789 | timer_t host_timer = t->timer; |
db1a4972 | 790 | struct itimerspec timeout; |
9c13246a | 791 | int64_t current_ns; |
db1a4972 | 792 | |
4c3d45eb PB |
793 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
794 | nearest_delta_ns = MIN_TIMER_REARM_NS; | |
db1a4972 PB |
795 | |
796 | /* check whether a timer is already running */ | |
797 | if (timer_gettime(host_timer, &timeout)) { | |
798 | perror("gettime"); | |
799 | fprintf(stderr, "Internal timer error: aborting\n"); | |
800 | exit(1); | |
801 | } | |
9c13246a PB |
802 | current_ns = timeout.it_value.tv_sec * 1000000000LL + timeout.it_value.tv_nsec; |
803 | if (current_ns && current_ns <= nearest_delta_ns) | |
db1a4972 PB |
804 | return; |
805 | ||
806 | timeout.it_interval.tv_sec = 0; | |
807 | timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ | |
9c13246a PB |
808 | timeout.it_value.tv_sec = nearest_delta_ns / 1000000000; |
809 | timeout.it_value.tv_nsec = nearest_delta_ns % 1000000000; | |
db1a4972 PB |
810 | if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
811 | perror("settime"); | |
812 | fprintf(stderr, "Internal timer error: aborting\n"); | |
813 | exit(1); | |
814 | } | |
815 | } | |
816 | ||
817 | #endif /* defined(__linux__) */ | |
818 | ||
f26e5a54 SW |
819 | #if !defined(_WIN32) |
820 | ||
db1a4972 PB |
821 | static int unix_start_timer(struct qemu_alarm_timer *t) |
822 | { | |
823 | struct sigaction act; | |
db1a4972 PB |
824 | |
825 | /* timer signal */ | |
826 | sigfillset(&act.sa_mask); | |
827 | act.sa_flags = 0; | |
828 | act.sa_handler = host_alarm_handler; | |
829 | ||
830 | sigaction(SIGALRM, &act, NULL); | |
84682834 PB |
831 | return 0; |
832 | } | |
db1a4972 | 833 | |
f3fc6e2e PB |
834 | static void unix_rearm_timer(struct qemu_alarm_timer *t, |
835 | int64_t nearest_delta_ns) | |
84682834 PB |
836 | { |
837 | struct itimerval itv; | |
84682834 | 838 | int err; |
db1a4972 | 839 | |
84682834 PB |
840 | if (nearest_delta_ns < MIN_TIMER_REARM_NS) |
841 | nearest_delta_ns = MIN_TIMER_REARM_NS; | |
842 | ||
843 | itv.it_interval.tv_sec = 0; | |
844 | itv.it_interval.tv_usec = 0; /* 0 for one-shot timer */ | |
845 | itv.it_value.tv_sec = nearest_delta_ns / 1000000000; | |
846 | itv.it_value.tv_usec = (nearest_delta_ns % 1000000000) / 1000; | |
847 | err = setitimer(ITIMER_REAL, &itv, NULL); | |
848 | if (err) { | |
849 | perror("setitimer"); | |
850 | fprintf(stderr, "Internal timer error: aborting\n"); | |
851 | exit(1); | |
852 | } | |
db1a4972 PB |
853 | } |
854 | ||
855 | static void unix_stop_timer(struct qemu_alarm_timer *t) | |
856 | { | |
857 | struct itimerval itv; | |
858 | ||
859 | memset(&itv, 0, sizeof(itv)); | |
860 | setitimer(ITIMER_REAL, &itv, NULL); | |
861 | } | |
862 | ||
863 | #endif /* !defined(_WIN32) */ | |
864 | ||
865 | ||
866 | #ifdef _WIN32 | |
867 | ||
2f9cba0c | 868 | static MMRESULT mm_timer; |
40f08e87 | 869 | static TIMECAPS mm_tc; |
2f9cba0c SW |
870 | |
871 | static void CALLBACK mm_alarm_handler(UINT uTimerID, UINT uMsg, | |
872 | DWORD_PTR dwUser, DWORD_PTR dw1, | |
873 | DWORD_PTR dw2) | |
874 | { | |
875 | struct qemu_alarm_timer *t = alarm_timer; | |
876 | if (!t) { | |
877 | return; | |
878 | } | |
8205199d SW |
879 | t->expired = true; |
880 | t->pending = true; | |
881 | qemu_notify_event(); | |
2f9cba0c SW |
882 | } |
883 | ||
884 | static int mm_start_timer(struct qemu_alarm_timer *t) | |
885 | { | |
40f08e87 | 886 | timeGetDevCaps(&mm_tc, sizeof(mm_tc)); |
2f9cba0c SW |
887 | return 0; |
888 | } | |
889 | ||
890 | static void mm_stop_timer(struct qemu_alarm_timer *t) | |
891 | { | |
0727b867 PB |
892 | if (mm_timer) { |
893 | timeKillEvent(mm_timer); | |
894 | } | |
2f9cba0c SW |
895 | } |
896 | ||
f3fc6e2e | 897 | static void mm_rearm_timer(struct qemu_alarm_timer *t, int64_t delta) |
2f9cba0c | 898 | { |
5bfb723f | 899 | int64_t nearest_delta_ms = delta / 1000000; |
40f08e87 SW |
900 | if (nearest_delta_ms < mm_tc.wPeriodMin) { |
901 | nearest_delta_ms = mm_tc.wPeriodMin; | |
902 | } else if (nearest_delta_ms > mm_tc.wPeriodMax) { | |
903 | nearest_delta_ms = mm_tc.wPeriodMax; | |
5bfb723f | 904 | } |
f3fc6e2e | 905 | |
0727b867 PB |
906 | if (mm_timer) { |
907 | timeKillEvent(mm_timer); | |
908 | } | |
40f08e87 SW |
909 | mm_timer = timeSetEvent((UINT)nearest_delta_ms, |
910 | mm_tc.wPeriodMin, | |
2f9cba0c SW |
911 | mm_alarm_handler, |
912 | (DWORD_PTR)t, | |
913 | TIME_ONESHOT | TIME_CALLBACK_FUNCTION); | |
914 | ||
915 | if (!mm_timer) { | |
52ef651f | 916 | fprintf(stderr, "Failed to re-arm win32 alarm timer\n"); |
40f08e87 | 917 | timeEndPeriod(mm_tc.wPeriodMin); |
2f9cba0c SW |
918 | exit(1); |
919 | } | |
920 | } | |
921 | ||
db1a4972 PB |
922 | static int win32_start_timer(struct qemu_alarm_timer *t) |
923 | { | |
68c23e55 PB |
924 | HANDLE hTimer; |
925 | BOOLEAN success; | |
926 | ||
927 | /* If you call ChangeTimerQueueTimer on a one-shot timer (its period | |
928 | is zero) that has already expired, the timer is not updated. Since | |
929 | creating a new timer is relatively expensive, set a bogus one-hour | |
930 | interval in the dynticks case. */ | |
931 | success = CreateTimerQueueTimer(&hTimer, | |
932 | NULL, | |
933 | host_alarm_handler, | |
934 | t, | |
935 | 1, | |
8205199d | 936 | 3600000, |
68c23e55 PB |
937 | WT_EXECUTEINTIMERTHREAD); |
938 | ||
939 | if (!success) { | |
db1a4972 PB |
940 | fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n", |
941 | GetLastError()); | |
db1a4972 PB |
942 | return -1; |
943 | } | |
944 | ||
cd0544ee | 945 | t->timer = hTimer; |
db1a4972 PB |
946 | return 0; |
947 | } | |
948 | ||
949 | static void win32_stop_timer(struct qemu_alarm_timer *t) | |
950 | { | |
cd0544ee | 951 | HANDLE hTimer = t->timer; |
db1a4972 | 952 | |
68c23e55 PB |
953 | if (hTimer) { |
954 | DeleteTimerQueueTimer(NULL, hTimer, NULL); | |
955 | } | |
db1a4972 PB |
956 | } |
957 | ||
f3fc6e2e PB |
958 | static void win32_rearm_timer(struct qemu_alarm_timer *t, |
959 | int64_t nearest_delta_ns) | |
db1a4972 | 960 | { |
cd0544ee | 961 | HANDLE hTimer = t->timer; |
5bfb723f | 962 | int64_t nearest_delta_ms; |
68c23e55 | 963 | BOOLEAN success; |
db1a4972 | 964 | |
5bfb723f | 965 | nearest_delta_ms = nearest_delta_ns / 1000000; |
cfced5b2 PB |
966 | if (nearest_delta_ms < 1) { |
967 | nearest_delta_ms = 1; | |
968 | } | |
5bfb723f SS |
969 | /* ULONG_MAX can be 32 bit */ |
970 | if (nearest_delta_ms > ULONG_MAX) { | |
971 | nearest_delta_ms = ULONG_MAX; | |
972 | } | |
68c23e55 PB |
973 | success = ChangeTimerQueueTimer(NULL, |
974 | hTimer, | |
5bfb723f | 975 | (unsigned long) nearest_delta_ms, |
68c23e55 | 976 | 3600000); |
db1a4972 | 977 | |
68c23e55 PB |
978 | if (!success) { |
979 | fprintf(stderr, "Failed to rearm win32 alarm timer: %ld\n", | |
980 | GetLastError()); | |
981 | exit(-1); | |
db1a4972 | 982 | } |
68c23e55 | 983 | |
db1a4972 PB |
984 | } |
985 | ||
986 | #endif /* _WIN32 */ | |
987 | ||
4260a739 PB |
988 | static void quit_timers(void) |
989 | { | |
990 | struct qemu_alarm_timer *t = alarm_timer; | |
991 | alarm_timer = NULL; | |
992 | t->stop(t); | |
993 | } | |
994 | ||
253ecf83 | 995 | #ifdef CONFIG_POSIX |
c8122c35 PB |
996 | static void reinit_timers(void) |
997 | { | |
998 | struct qemu_alarm_timer *t = alarm_timer; | |
999 | t->stop(t); | |
1000 | if (t->start(t)) { | |
1001 | fprintf(stderr, "Internal timer error: aborting\n"); | |
1002 | exit(1); | |
1003 | } | |
1004 | qemu_rearm_alarm_timer(t); | |
1005 | } | |
253ecf83 | 1006 | #endif /* CONFIG_POSIX */ |
c8122c35 | 1007 | |
db1a4972 PB |
1008 | int init_timer_alarm(void) |
1009 | { | |
1010 | struct qemu_alarm_timer *t = NULL; | |
1011 | int i, err = -1; | |
1012 | ||
744ca8e3 PB |
1013 | if (alarm_timer) { |
1014 | return 0; | |
1015 | } | |
1016 | ||
db1a4972 PB |
1017 | for (i = 0; alarm_timers[i].name; i++) { |
1018 | t = &alarm_timers[i]; | |
1019 | ||
1020 | err = t->start(t); | |
1021 | if (!err) | |
1022 | break; | |
1023 | } | |
1024 | ||
1025 | if (err) { | |
1026 | err = -ENOENT; | |
1027 | goto fail; | |
1028 | } | |
1029 | ||
4260a739 | 1030 | atexit(quit_timers); |
c8122c35 PB |
1031 | #ifdef CONFIG_POSIX |
1032 | pthread_atfork(NULL, NULL, reinit_timers); | |
1033 | #endif | |
db1a4972 | 1034 | alarm_timer = t; |
db1a4972 PB |
1035 | return 0; |
1036 | ||
1037 | fail: | |
1038 | return err; | |
1039 | } | |
1040 |