4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu-common.h"
26 #include "qemu-timer.h"
27 #include "slirp/slirp.h"
28 #include "main-loop.h"
34 static int io_thread_fd = -1;
36 void qemu_notify_event(void)
38 /* Write 8 bytes to be compatible with eventfd. */
39 static const uint64_t val = 1;
42 if (io_thread_fd == -1) {
46 qemu_notify_hax_event();
49 ret = write(io_thread_fd, &val, sizeof(val));
50 } while (ret < 0 && errno == EINTR);
52 /* EAGAIN is fine, a read must be pending. */
53 if (ret < 0 && errno != EAGAIN) {
54 fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
60 static void qemu_event_read(void *opaque)
62 int fd = (intptr_t)opaque;
66 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
68 len = read(fd, buffer, sizeof(buffer));
69 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
72 static int qemu_event_init(void)
77 err = qemu_eventfd(fds);
81 err = fcntl_setfl(fds[0], O_NONBLOCK);
85 err = fcntl_setfl(fds[1], O_NONBLOCK);
89 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
90 (void *)(intptr_t)fds[0]);
92 io_thread_fd = fds[1];
101 /* If we have signalfd, we mask out the signals we want to handle and then
102 * use signalfd to listen for them. We rely on whatever the current signal
103 * handler is to dispatch the signals when we receive them.
105 static void sigfd_handler(void *opaque)
107 int fd = (intptr_t)opaque;
108 struct qemu_signalfd_siginfo info;
109 struct sigaction action;
114 len = read(fd, &info, sizeof(info));
115 } while (len == -1 && errno == EINTR);
117 if (len == -1 && errno == EAGAIN) {
121 if (len != sizeof(info)) {
122 printf("read from sigfd returned %zd: %m\n", len);
126 sigaction(info.ssi_signo, NULL, &action);
127 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
128 action.sa_sigaction(info.ssi_signo,
129 (siginfo_t *)&info, NULL);
130 } else if (action.sa_handler) {
131 action.sa_handler(info.ssi_signo);
136 static int qemu_signal_init(void)
142 * SIG_IPI must be blocked in the main thread and must not be caught
143 * by sigwait() in the signal thread. Otherwise, the cpu thread will
144 * not catch it reliably.
147 sigaddset(&set, SIG_IPI);
148 sigaddset(&set, SIGIO);
149 sigaddset(&set, SIGALRM);
150 sigaddset(&set, SIGBUS);
151 pthread_sigmask(SIG_BLOCK, &set, NULL);
153 sigdelset(&set, SIG_IPI);
154 sigfd = qemu_signalfd(&set);
156 fprintf(stderr, "failed to create signalfd\n");
160 fcntl_setfl(sigfd, O_NONBLOCK);
162 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
163 (void *)(intptr_t)sigfd);
170 static HANDLE qemu_event_handle = NULL;
172 static void dummy_event_handler(void *opaque)
176 static int qemu_event_init(void)
178 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
179 if (!qemu_event_handle) {
180 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
183 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
187 void qemu_notify_event(void)
189 if (!qemu_event_handle) {
193 qemu_notify_hax_event();
195 if (!SetEvent(qemu_event_handle)) {
196 fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
202 static int qemu_signal_init(void)
208 int main_loop_init(void)
212 qemu_mutex_lock_iothread();
213 ret = qemu_signal_init();
218 /* Note eventfd must be drained before signalfd handlers run */
219 ret = qemu_event_init();
227 static fd_set rfds, wfds, xfds;
229 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
230 static int n_poll_fds;
231 static int max_priority;
234 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
235 fd_set *xfds, uint32_t *cur_timeout)
237 GMainContext *context = g_main_context_default();
241 g_main_context_prepare(context, &max_priority);
243 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
244 poll_fds, ARRAY_SIZE(poll_fds));
245 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
247 for (i = 0; i < n_poll_fds; i++) {
248 GPollFD *p = &poll_fds[i];
250 if ((p->events & G_IO_IN)) {
252 *max_fd = MAX(*max_fd, p->fd);
254 if ((p->events & G_IO_OUT)) {
256 *max_fd = MAX(*max_fd, p->fd);
258 if ((p->events & G_IO_ERR)) {
260 *max_fd = MAX(*max_fd, p->fd);
264 if (timeout >= 0 && timeout < *cur_timeout) {
265 *cur_timeout = timeout;
269 static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
272 GMainContext *context = g_main_context_default();
277 for (i = 0; i < n_poll_fds; i++) {
278 GPollFD *p = &poll_fds[i];
280 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
281 p->revents |= G_IO_IN;
283 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
284 p->revents |= G_IO_OUT;
286 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
287 p->revents |= G_IO_ERR;
292 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
293 g_main_context_dispatch(context);
297 static int os_host_main_loop_wait(uint32_t timeout)
299 struct timeval tv, *tvarg = NULL;
302 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout);
304 if (timeout < UINT32_MAX) {
306 tv.tv_sec = timeout / 1000;
307 tv.tv_usec = (timeout % 1000) * 1000;
311 qemu_mutex_unlock_iothread();
314 ret = select(nfds + 1, &rfds, &wfds, &xfds, tvarg);
317 qemu_mutex_lock_iothread();
320 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
324 /***********************************************************/
325 /* Polling handling */
327 typedef struct PollingEntry {
330 struct PollingEntry *next;
333 static PollingEntry *first_polling_entry;
335 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
337 PollingEntry **ppe, *pe;
338 pe = g_malloc0(sizeof(PollingEntry));
341 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
346 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
348 PollingEntry **ppe, *pe;
349 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
351 if (pe->func == func && pe->opaque == opaque) {
359 /***********************************************************/
360 /* Wait objects support */
361 typedef struct WaitObjects {
363 int revents[MAXIMUM_WAIT_OBJECTS + 1];
364 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
365 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
366 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
369 static WaitObjects wait_objects = {0};
371 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
373 WaitObjects *w = &wait_objects;
374 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
377 w->events[w->num] = handle;
378 w->func[w->num] = func;
379 w->opaque[w->num] = opaque;
380 w->revents[w->num] = 0;
385 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
388 WaitObjects *w = &wait_objects;
391 for (i = 0; i < w->num; i++) {
392 if (w->events[i] == handle) {
396 w->events[i] = w->events[i + 1];
397 w->func[i] = w->func[i + 1];
398 w->opaque[i] = w->opaque[i + 1];
399 w->revents[i] = w->revents[i + 1];
407 void qemu_fd_register(int fd)
409 WSAEventSelect(fd, qemu_event_handle, FD_READ | FD_ACCEPT | FD_CLOSE |
410 FD_CONNECT | FD_WRITE | FD_OOB);
413 static int os_host_main_loop_wait(uint32_t timeout)
415 GMainContext *context = g_main_context_default();
418 WaitObjects *w = &wait_objects;
420 static struct timeval tv0;
422 /* XXX: need to suppress polling by better using win32 events */
424 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
425 ret |= pe->func(pe->opaque);
432 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
438 g_main_context_prepare(context, &max_priority);
439 n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
440 poll_fds, ARRAY_SIZE(poll_fds));
441 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
443 for (i = 0; i < w->num; i++) {
444 poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
445 poll_fds[n_poll_fds + i].events = G_IO_IN;
448 if (poll_timeout < 0 || timeout < poll_timeout) {
449 poll_timeout = timeout;
452 qemu_mutex_unlock_iothread();
453 ret = g_poll(poll_fds, n_poll_fds + w->num, poll_timeout);
454 qemu_mutex_lock_iothread();
456 for (i = 0; i < w->num; i++) {
457 w->revents[i] = poll_fds[n_poll_fds + i].revents;
459 for (i = 0; i < w->num; i++) {
460 if (w->revents[i] && w->func[i]) {
461 w->func[i](w->opaque[i]);
466 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
467 g_main_context_dispatch(context);
470 /* If an edge-triggered socket event occurred, select will return a
471 * positive result on the next iteration. We do not need to do anything
479 int main_loop_wait(int nonblocking)
482 uint32_t timeout = UINT32_MAX;
487 qemu_bh_update_timeout(&timeout);
490 /* poll any events */
491 /* XXX: separate device handlers from system ones */
498 slirp_update_timeout(&timeout);
499 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
501 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
502 ret = os_host_main_loop_wait(timeout);
503 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
505 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
508 qemu_run_all_timers();
510 /* Check bottom-halves last in case any of the earlier events triggered