blob: 234a3130c2132ad1c13322949549e14fffeb9ded [file] [log] [blame]
Paolo Bonzinid3b12f52011-09-13 10:30:52 +02001/*
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 */
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020024
Stefan Weil0ec024f2011-10-25 22:23:17 +020025#include "qemu-common.h"
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020026#include "qemu-timer.h"
Stefan Weil0ec024f2011-10-25 22:23:17 +020027#include "slirp/slirp.h"
28#include "main-loop.h"
Paolo Bonzinif627aab2012-10-29 23:45:23 +010029#include "qemu-aio.h"
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020030
31#ifndef _WIN32
32
Stefan Weil0ec024f2011-10-25 22:23:17 +020033#include "compatfd.h"
34
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020035/* 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 */
39static 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
70static 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);
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020082 sigaddset(&set, SIGIO);
83 sigaddset(&set, SIGALRM);
84 sigaddset(&set, SIGBUS);
85 pthread_sigmask(SIG_BLOCK, &set, NULL);
86
Lai Jiangshan4aa75342012-01-12 17:05:35 +080087 sigdelset(&set, SIG_IPI);
Paolo Bonzinid3b12f52011-09-13 10:30:52 +020088 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
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200104static int qemu_signal_init(void)
105{
106 return 0;
107}
108#endif
109
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100110static AioContext *qemu_aio_context;
111
Paolo Bonzini4c8d0d22010-05-24 17:27:14 +0200112void qemu_notify_event(void)
113{
114 if (!qemu_aio_context) {
115 return;
116 }
117 aio_notify(qemu_aio_context);
118}
119
Paolo Bonzini172061a2012-10-29 15:28:36 +0100120int qemu_init_main_loop(void)
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200121{
122 int ret;
Paolo Bonzini82cbbdc2012-09-24 15:07:08 +0200123 GSource *src;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200124
Paolo Bonzini172061a2012-10-29 15:28:36 +0100125 init_clocks();
Paolo Bonzinif9ab4652012-11-02 15:43:23 +0100126 if (init_timer_alarm() < 0) {
127 fprintf(stderr, "could not initialize alarm timer\n");
128 exit(1);
129 }
Paolo Bonzini172061a2012-10-29 15:28:36 +0100130
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200131 qemu_mutex_lock_iothread();
132 ret = qemu_signal_init();
133 if (ret) {
134 return ret;
135 }
136
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100137 qemu_aio_context = aio_context_new();
Paolo Bonzini82cbbdc2012-09-24 15:07:08 +0200138 src = aio_get_g_source(qemu_aio_context);
139 g_source_attach(src, NULL);
140 g_source_unref(src);
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200141 return 0;
142}
143
Paolo Bonzini15455532012-03-20 10:49:18 +0100144static fd_set rfds, wfds, xfds;
145static int nfds;
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100146static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200147static int n_poll_fds;
148static int max_priority;
149
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100150#ifndef _WIN32
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200151static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100152 fd_set *xfds, uint32_t *cur_timeout)
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200153{
154 GMainContext *context = g_main_context_default();
155 int i;
Paolo Bonzini4dae83a2012-03-20 10:49:17 +0100156 int timeout = 0;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200157
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
Paolo Bonzini4dae83a2012-03-20 10:49:17 +0100181 if (timeout >= 0 && timeout < *cur_timeout) {
182 *cur_timeout = timeout;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200183 }
184}
185
186static 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
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100214static int os_host_main_loop_wait(uint32_t timeout)
Paolo Bonzini15455532012-03-20 10:49:18 +0100215{
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100216 struct timeval tv, *tvarg = NULL;
Paolo Bonzini15455532012-03-20 10:49:18 +0100217 int ret;
218
219 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout);
220
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100221 if (timeout < UINT32_MAX) {
222 tvarg = &tv;
223 tv.tv_sec = timeout / 1000;
224 tv.tv_usec = (timeout % 1000) * 1000;
225 }
226
Paolo Bonzini15455532012-03-20 10:49:18 +0100227 if (timeout > 0) {
228 qemu_mutex_unlock_iothread();
229 }
230
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100231 ret = select(nfds + 1, &rfds, &wfds, &xfds, tvarg);
Paolo Bonzini15455532012-03-20 10:49:18 +0100232
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
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200241/***********************************************************/
242/* Polling handling */
243
244typedef struct PollingEntry {
245 PollingFunc *func;
246 void *opaque;
247 struct PollingEntry *next;
248} PollingEntry;
249
250static PollingEntry *first_polling_entry;
251
252int 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
263void 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 */
278typedef struct WaitObjects {
279 int num;
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100280 int revents[MAXIMUM_WAIT_OBJECTS + 1];
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200281 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
282 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
283 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
284} WaitObjects;
285
286static WaitObjects wait_objects = {0};
287
288int 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;
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100297 w->revents[w->num] = 0;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200298 w->num++;
299 return 0;
300}
301
302void 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];
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100316 w->revents[i] = w->revents[i + 1];
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200317 }
318 }
319 if (found) {
320 w->num--;
321 }
322}
323
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100324void qemu_fd_register(int fd)
325{
Paolo Bonzini4c8d0d22010-05-24 17:27:14 +0200326 WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier),
327 FD_READ | FD_ACCEPT | FD_CLOSE |
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100328 FD_CONNECT | FD_WRITE | FD_OOB);
329}
330
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100331static int os_host_main_loop_wait(uint32_t timeout)
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200332{
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100333 GMainContext *context = g_main_context_default();
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100334 int ret, i;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200335 PollingEntry *pe;
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100336 WaitObjects *w = &wait_objects;
Stefan Weil42fe1c22012-04-27 17:02:08 +0200337 gint poll_timeout;
Paolo Bonzini15455532012-03-20 10:49:18 +0100338 static struct timeval tv0;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200339
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 }
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100345 if (ret != 0) {
346 return ret;
347 }
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200348
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100349 if (nfds >= 0) {
350 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
351 if (ret != 0) {
Stefan Weil3239ad02012-04-29 19:15:02 +0200352 timeout = 0;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200353 }
354 }
355
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100356 g_main_context_prepare(context, &max_priority);
Stefan Weil42fe1c22012-04-27 17:02:08 +0200357 n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100358 poll_fds, ARRAY_SIZE(poll_fds));
359 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
360
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100361 for (i = 0; i < w->num; i++) {
Stefan Weil58b96302012-04-12 20:42:34 +0200362 poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100363 poll_fds[n_poll_fds + i].events = G_IO_IN;
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100364 }
365
Stefan Weil3239ad02012-04-29 19:15:02 +0200366 if (poll_timeout < 0 || timeout < poll_timeout) {
367 poll_timeout = timeout;
368 }
369
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100370 qemu_mutex_unlock_iothread();
Stefan Weil42fe1c22012-04-27 17:02:08 +0200371 ret = g_poll(poll_fds, n_poll_fds + w->num, poll_timeout);
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100372 qemu_mutex_lock_iothread();
373 if (ret > 0) {
374 for (i = 0; i < w->num; i++) {
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100375 w->revents[i] = poll_fds[n_poll_fds + i].revents;
Paolo Bonzini06ac7d42012-03-20 10:49:20 +0100376 }
377 for (i = 0; i < w->num; i++) {
378 if (w->revents[i] && w->func[i]) {
379 w->func[i](w->opaque[i]);
380 }
381 }
382 }
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100383
Paolo Bonziniea26ce72012-03-20 10:49:21 +0100384 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
385 g_main_context_dispatch(context);
386 }
387
Paolo Bonzinid3385eb2012-03-20 10:49:19 +0100388 /* 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
Paolo Bonzini15455532012-03-20 10:49:18 +0100393 return ret;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200394}
395#endif
396
397int main_loop_wait(int nonblocking)
398{
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100399 int ret;
400 uint32_t timeout = UINT32_MAX;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200401
402 if (nonblocking) {
403 timeout = 0;
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200404 }
405
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200406 /* 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
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100414 slirp_update_timeout(&timeout);
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200415 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
416#endif
417 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Paolo Bonzini15455532012-03-20 10:49:18 +0100418 ret = os_host_main_loop_wait(timeout);
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200419 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
Paolo Bonzinid3b12f52011-09-13 10:30:52 +0200426 return ret;
427}
Paolo Bonzinif627aab2012-10-29 23:45:23 +0100428
429/* Functions to operate on the main QEMU AioContext. */
430
431QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
432{
433 return aio_bh_new(qemu_aio_context, cb, opaque);
434}
435
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200436void qemu_aio_flush(void)
437{
438 aio_flush(qemu_aio_context);
439}
440
441bool qemu_aio_wait(void)
442{
Paolo Bonzini7c0628b2012-09-24 14:37:53 +0200443 return aio_poll(qemu_aio_context, true);
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200444}
445
Paolo Bonzinif42b2202012-06-09 04:01:51 +0200446#ifdef CONFIG_POSIX
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200447void 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);
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200455}
Paolo Bonzini82cbbdc2012-09-24 15:07:08 +0200456#endif
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200457
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200458void qemu_aio_set_event_notifier(EventNotifier *notifier,
459 EventNotifierHandler *io_read,
460 AioFlushEventNotifierHandler *io_flush)
461{
Paolo Bonzini82cbbdc2012-09-24 15:07:08 +0200462 aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flush);
Paolo Bonzinia915f4b2012-09-13 12:28:51 +0200463}