]>
Commit | Line | Data |
---|---|---|
d3b12f5d 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 | */ | |
d3b12f5d | 24 | |
0ec024f6 | 25 | #include "qemu-common.h" |
d3b12f5d | 26 | #include "qemu-timer.h" |
0ec024f6 SW |
27 | #include "slirp/slirp.h" |
28 | #include "main-loop.h" | |
f627aab1 | 29 | #include "qemu-aio.h" |
d3b12f5d PB |
30 | |
31 | #ifndef _WIN32 | |
32 | ||
0ec024f6 SW |
33 | #include "compatfd.h" |
34 | ||
d3b12f5d PB |
35 | /* If we have signalfd, we mask out the signals we want to handle and then |
36 | * use signalfd to listen for them. We rely on whatever the current signal | |
37 | * handler is to dispatch the signals when we receive them. | |
38 | */ | |
39 | static void sigfd_handler(void *opaque) | |
40 | { | |
41 | int fd = (intptr_t)opaque; | |
42 | struct qemu_signalfd_siginfo info; | |
43 | struct sigaction action; | |
44 | ssize_t len; | |
45 | ||
46 | while (1) { | |
47 | do { | |
48 | len = read(fd, &info, sizeof(info)); | |
49 | } while (len == -1 && errno == EINTR); | |
50 | ||
51 | if (len == -1 && errno == EAGAIN) { | |
52 | break; | |
53 | } | |
54 | ||
55 | if (len != sizeof(info)) { | |
56 | printf("read from sigfd returned %zd: %m\n", len); | |
57 | return; | |
58 | } | |
59 | ||
60 | sigaction(info.ssi_signo, NULL, &action); | |
61 | if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) { | |
62 | action.sa_sigaction(info.ssi_signo, | |
63 | (siginfo_t *)&info, NULL); | |
64 | } else if (action.sa_handler) { | |
65 | action.sa_handler(info.ssi_signo); | |
66 | } | |
67 | } | |
68 | } | |
69 | ||
70 | static int qemu_signal_init(void) | |
71 | { | |
72 | int sigfd; | |
73 | sigset_t set; | |
74 | ||
75 | /* | |
76 | * SIG_IPI must be blocked in the main thread and must not be caught | |
77 | * by sigwait() in the signal thread. Otherwise, the cpu thread will | |
78 | * not catch it reliably. | |
79 | */ | |
80 | sigemptyset(&set); | |
81 | sigaddset(&set, SIG_IPI); | |
d3b12f5d PB |
82 | sigaddset(&set, SIGIO); |
83 | sigaddset(&set, SIGALRM); | |
84 | sigaddset(&set, SIGBUS); | |
85 | pthread_sigmask(SIG_BLOCK, &set, NULL); | |
86 | ||
4aa7534d | 87 | sigdelset(&set, SIG_IPI); |
d3b12f5d PB |
88 | sigfd = qemu_signalfd(&set); |
89 | if (sigfd == -1) { | |
90 | fprintf(stderr, "failed to create signalfd\n"); | |
91 | return -errno; | |
92 | } | |
93 | ||
94 | fcntl_setfl(sigfd, O_NONBLOCK); | |
95 | ||
96 | qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL, | |
97 | (void *)(intptr_t)sigfd); | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | #else /* _WIN32 */ | |
103 | ||
4c8d0d27 | 104 | static int qemu_signal_init(void) |
d3b12f5d | 105 | { |
d3b12f5d PB |
106 | return 0; |
107 | } | |
4c8d0d27 PB |
108 | #endif |
109 | ||
110 | static AioContext *qemu_aio_context; | |
d3b12f5d PB |
111 | |
112 | void qemu_notify_event(void) | |
113 | { | |
4c8d0d27 | 114 | if (!qemu_aio_context) { |
ee77dfb2 MR |
115 | return; |
116 | } | |
4c8d0d27 | 117 | aio_notify(qemu_aio_context); |
d3b12f5d PB |
118 | } |
119 | ||
172061a0 | 120 | int qemu_init_main_loop(void) |
d3b12f5d PB |
121 | { |
122 | int ret; | |
82cbbdc6 | 123 | GSource *src; |
d3b12f5d | 124 | |
172061a0 | 125 | init_clocks(); |
f9ab4654 PB |
126 | if (init_timer_alarm() < 0) { |
127 | fprintf(stderr, "could not initialize alarm timer\n"); | |
128 | exit(1); | |
129 | } | |
172061a0 | 130 | |
d3b12f5d PB |
131 | qemu_mutex_lock_iothread(); |
132 | ret = qemu_signal_init(); | |
133 | if (ret) { | |
134 | return ret; | |
135 | } | |
136 | ||
f627aab1 | 137 | qemu_aio_context = aio_context_new(); |
82cbbdc6 PB |
138 | src = aio_get_g_source(qemu_aio_context); |
139 | g_source_attach(src, NULL); | |
140 | g_source_unref(src); | |
d3b12f5d PB |
141 | return 0; |
142 | } | |
143 | ||
15455536 PB |
144 | static fd_set rfds, wfds, xfds; |
145 | static int nfds; | |
06ac7d49 | 146 | static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */ |
d3b12f5d PB |
147 | static int n_poll_fds; |
148 | static int max_priority; | |
149 | ||
ea26ce76 | 150 | #ifndef _WIN32 |
d3b12f5d | 151 | static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds, |
7c7db755 | 152 | fd_set *xfds, uint32_t *cur_timeout) |
d3b12f5d PB |
153 | { |
154 | GMainContext *context = g_main_context_default(); | |
155 | int i; | |
4dae83ae | 156 | int timeout = 0; |
d3b12f5d PB |
157 | |
158 | g_main_context_prepare(context, &max_priority); | |
159 | ||
160 | n_poll_fds = g_main_context_query(context, max_priority, &timeout, | |
161 | poll_fds, ARRAY_SIZE(poll_fds)); | |
162 | g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds)); | |
163 | ||
164 | for (i = 0; i < n_poll_fds; i++) { | |
165 | GPollFD *p = &poll_fds[i]; | |
166 | ||
167 | if ((p->events & G_IO_IN)) { | |
168 | FD_SET(p->fd, rfds); | |
169 | *max_fd = MAX(*max_fd, p->fd); | |
170 | } | |
171 | if ((p->events & G_IO_OUT)) { | |
172 | FD_SET(p->fd, wfds); | |
173 | *max_fd = MAX(*max_fd, p->fd); | |
174 | } | |
175 | if ((p->events & G_IO_ERR)) { | |
176 | FD_SET(p->fd, xfds); | |
177 | *max_fd = MAX(*max_fd, p->fd); | |
178 | } | |
179 | } | |
180 | ||
4dae83ae PB |
181 | if (timeout >= 0 && timeout < *cur_timeout) { |
182 | *cur_timeout = timeout; | |
d3b12f5d PB |
183 | } |
184 | } | |
185 | ||
186 | static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds, | |
187 | bool err) | |
188 | { | |
189 | GMainContext *context = g_main_context_default(); | |
190 | ||
191 | if (!err) { | |
192 | int i; | |
193 | ||
194 | for (i = 0; i < n_poll_fds; i++) { | |
195 | GPollFD *p = &poll_fds[i]; | |
196 | ||
197 | if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) { | |
198 | p->revents |= G_IO_IN; | |
199 | } | |
200 | if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) { | |
201 | p->revents |= G_IO_OUT; | |
202 | } | |
203 | if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) { | |
204 | p->revents |= G_IO_ERR; | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) { | |
210 | g_main_context_dispatch(context); | |
211 | } | |
212 | } | |
213 | ||
7c7db755 | 214 | static int os_host_main_loop_wait(uint32_t timeout) |
15455536 | 215 | { |
7c7db755 | 216 | struct timeval tv, *tvarg = NULL; |
15455536 PB |
217 | int ret; |
218 | ||
219 | glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout); | |
220 | ||
7c7db755 SS |
221 | if (timeout < UINT32_MAX) { |
222 | tvarg = &tv; | |
223 | tv.tv_sec = timeout / 1000; | |
224 | tv.tv_usec = (timeout % 1000) * 1000; | |
225 | } | |
226 | ||
15455536 PB |
227 | if (timeout > 0) { |
228 | qemu_mutex_unlock_iothread(); | |
229 | } | |
230 | ||
7c7db755 | 231 | ret = select(nfds + 1, &rfds, &wfds, &xfds, tvarg); |
15455536 PB |
232 | |
233 | if (timeout > 0) { | |
234 | qemu_mutex_lock_iothread(); | |
235 | } | |
236 | ||
237 | glib_select_poll(&rfds, &wfds, &xfds, (ret < 0)); | |
238 | return ret; | |
239 | } | |
240 | #else | |
d3b12f5d PB |
241 | /***********************************************************/ |
242 | /* Polling handling */ | |
243 | ||
244 | typedef struct PollingEntry { | |
245 | PollingFunc *func; | |
246 | void *opaque; | |
247 | struct PollingEntry *next; | |
248 | } PollingEntry; | |
249 | ||
250 | static PollingEntry *first_polling_entry; | |
251 | ||
252 | int qemu_add_polling_cb(PollingFunc *func, void *opaque) | |
253 | { | |
254 | PollingEntry **ppe, *pe; | |
255 | pe = g_malloc0(sizeof(PollingEntry)); | |
256 | pe->func = func; | |
257 | pe->opaque = opaque; | |
258 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); | |
259 | *ppe = pe; | |
260 | return 0; | |
261 | } | |
262 | ||
263 | void qemu_del_polling_cb(PollingFunc *func, void *opaque) | |
264 | { | |
265 | PollingEntry **ppe, *pe; | |
266 | for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { | |
267 | pe = *ppe; | |
268 | if (pe->func == func && pe->opaque == opaque) { | |
269 | *ppe = pe->next; | |
270 | g_free(pe); | |
271 | break; | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | /***********************************************************/ | |
277 | /* Wait objects support */ | |
278 | typedef struct WaitObjects { | |
279 | int num; | |
06ac7d49 | 280 | int revents[MAXIMUM_WAIT_OBJECTS + 1]; |
d3b12f5d PB |
281 | HANDLE events[MAXIMUM_WAIT_OBJECTS + 1]; |
282 | WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1]; | |
283 | void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; | |
284 | } WaitObjects; | |
285 | ||
286 | static WaitObjects wait_objects = {0}; | |
287 | ||
288 | int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) | |
289 | { | |
290 | WaitObjects *w = &wait_objects; | |
291 | if (w->num >= MAXIMUM_WAIT_OBJECTS) { | |
292 | return -1; | |
293 | } | |
294 | w->events[w->num] = handle; | |
295 | w->func[w->num] = func; | |
296 | w->opaque[w->num] = opaque; | |
06ac7d49 | 297 | w->revents[w->num] = 0; |
d3b12f5d PB |
298 | w->num++; |
299 | return 0; | |
300 | } | |
301 | ||
302 | void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) | |
303 | { | |
304 | int i, found; | |
305 | WaitObjects *w = &wait_objects; | |
306 | ||
307 | found = 0; | |
308 | for (i = 0; i < w->num; i++) { | |
309 | if (w->events[i] == handle) { | |
310 | found = 1; | |
311 | } | |
312 | if (found) { | |
313 | w->events[i] = w->events[i + 1]; | |
314 | w->func[i] = w->func[i + 1]; | |
315 | w->opaque[i] = w->opaque[i + 1]; | |
06ac7d49 | 316 | w->revents[i] = w->revents[i + 1]; |
d3b12f5d PB |
317 | } |
318 | } | |
319 | if (found) { | |
320 | w->num--; | |
321 | } | |
322 | } | |
323 | ||
d3385eb4 PB |
324 | void qemu_fd_register(int fd) |
325 | { | |
4c8d0d27 PB |
326 | WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier), |
327 | FD_READ | FD_ACCEPT | FD_CLOSE | | |
d3385eb4 PB |
328 | FD_CONNECT | FD_WRITE | FD_OOB); |
329 | } | |
330 | ||
7c7db755 | 331 | static int os_host_main_loop_wait(uint32_t timeout) |
d3b12f5d | 332 | { |
ea26ce76 | 333 | GMainContext *context = g_main_context_default(); |
06ac7d49 | 334 | int ret, i; |
d3b12f5d | 335 | PollingEntry *pe; |
d3385eb4 | 336 | WaitObjects *w = &wait_objects; |
42fe1c24 | 337 | gint poll_timeout; |
15455536 | 338 | static struct timeval tv0; |
d3b12f5d PB |
339 | |
340 | /* XXX: need to suppress polling by better using win32 events */ | |
341 | ret = 0; | |
342 | for (pe = first_polling_entry; pe != NULL; pe = pe->next) { | |
343 | ret |= pe->func(pe->opaque); | |
344 | } | |
d3385eb4 PB |
345 | if (ret != 0) { |
346 | return ret; | |
347 | } | |
d3b12f5d | 348 | |
d3385eb4 PB |
349 | if (nfds >= 0) { |
350 | ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0); | |
351 | if (ret != 0) { | |
3239ad04 | 352 | timeout = 0; |
d3385eb4 PB |
353 | } |
354 | } | |
d3b12f5d | 355 | |
ea26ce76 | 356 | g_main_context_prepare(context, &max_priority); |
42fe1c24 | 357 | n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout, |
ea26ce76 PB |
358 | poll_fds, ARRAY_SIZE(poll_fds)); |
359 | g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds)); | |
360 | ||
06ac7d49 | 361 | for (i = 0; i < w->num; i++) { |
58b9630d | 362 | poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i]; |
ea26ce76 | 363 | poll_fds[n_poll_fds + i].events = G_IO_IN; |
06ac7d49 PB |
364 | } |
365 | ||
3239ad04 SW |
366 | if (poll_timeout < 0 || timeout < poll_timeout) { |
367 | poll_timeout = timeout; | |
368 | } | |
369 | ||
d3385eb4 | 370 | qemu_mutex_unlock_iothread(); |
42fe1c24 | 371 | ret = g_poll(poll_fds, n_poll_fds + w->num, poll_timeout); |
d3385eb4 | 372 | qemu_mutex_lock_iothread(); |
06ac7d49 PB |
373 | if (ret > 0) { |
374 | for (i = 0; i < w->num; i++) { | |
ea26ce76 | 375 | w->revents[i] = poll_fds[n_poll_fds + i].revents; |
d3385eb4 | 376 | } |
06ac7d49 PB |
377 | for (i = 0; i < w->num; i++) { |
378 | if (w->revents[i] && w->func[i]) { | |
379 | w->func[i](w->opaque[i]); | |
d3b12f5d | 380 | } |
d3b12f5d PB |
381 | } |
382 | } | |
383 | ||
ea26ce76 PB |
384 | if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) { |
385 | g_main_context_dispatch(context); | |
386 | } | |
387 | ||
d3385eb4 PB |
388 | /* If an edge-triggered socket event occurred, select will return a |
389 | * positive result on the next iteration. We do not need to do anything | |
390 | * here. | |
391 | */ | |
392 | ||
15455536 | 393 | return ret; |
d3b12f5d PB |
394 | } |
395 | #endif | |
396 | ||
397 | int main_loop_wait(int nonblocking) | |
398 | { | |
7c7db755 SS |
399 | int ret; |
400 | uint32_t timeout = UINT32_MAX; | |
d3b12f5d PB |
401 | |
402 | if (nonblocking) { | |
403 | timeout = 0; | |
d3b12f5d PB |
404 | } |
405 | ||
d3b12f5d PB |
406 | /* poll any events */ |
407 | /* XXX: separate device handlers from system ones */ | |
408 | nfds = -1; | |
409 | FD_ZERO(&rfds); | |
410 | FD_ZERO(&wfds); | |
411 | FD_ZERO(&xfds); | |
412 | ||
413 | #ifdef CONFIG_SLIRP | |
7c7db755 | 414 | slirp_update_timeout(&timeout); |
d3b12f5d PB |
415 | slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
416 | #endif | |
417 | qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds); | |
15455536 | 418 | ret = os_host_main_loop_wait(timeout); |
d3b12f5d PB |
419 | qemu_iohandler_poll(&rfds, &wfds, &xfds, ret); |
420 | #ifdef CONFIG_SLIRP | |
421 | slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0)); | |
422 | #endif | |
423 | ||
424 | qemu_run_all_timers(); | |
425 | ||
d3b12f5d PB |
426 | return ret; |
427 | } | |
f627aab1 PB |
428 | |
429 | /* Functions to operate on the main QEMU AioContext. */ | |
430 | ||
431 | QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque) | |
432 | { | |
433 | return aio_bh_new(qemu_aio_context, cb, opaque); | |
434 | } | |
435 | ||
a915f4bc PB |
436 | void qemu_aio_flush(void) |
437 | { | |
438 | aio_flush(qemu_aio_context); | |
439 | } | |
440 | ||
441 | bool qemu_aio_wait(void) | |
442 | { | |
7c0628b2 | 443 | return aio_poll(qemu_aio_context, true); |
a915f4bc PB |
444 | } |
445 | ||
f42b2207 | 446 | #ifdef CONFIG_POSIX |
a915f4bc PB |
447 | void qemu_aio_set_fd_handler(int fd, |
448 | IOHandler *io_read, | |
449 | IOHandler *io_write, | |
450 | AioFlushHandler *io_flush, | |
451 | void *opaque) | |
452 | { | |
453 | aio_set_fd_handler(qemu_aio_context, fd, io_read, io_write, io_flush, | |
454 | opaque); | |
a915f4bc | 455 | } |
82cbbdc6 | 456 | #endif |
a915f4bc | 457 | |
a915f4bc PB |
458 | void qemu_aio_set_event_notifier(EventNotifier *notifier, |
459 | EventNotifierHandler *io_read, | |
460 | AioFlushEventNotifierHandler *io_flush) | |
461 | { | |
82cbbdc6 | 462 | aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flush); |
a915f4bc | 463 | } |