]> git.proxmox.com Git - qemu.git/blame - main-loop.c
target-sparc: Fix use of g_new0 / g_free
[qemu.git] / main-loop.c
CommitLineData
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 */
24#include "config-host.h"
25#include <unistd.h>
26#include <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <stdbool.h>
31
32#ifdef _WIN32
33#include <windows.h>
34#include <winsock2.h>
35#include <ws2tcpip.h>
36#else
37#include <sys/socket.h>
38#include <netinet/in.h>
39#include <net/if.h>
40#include <arpa/inet.h>
41#include <sys/select.h>
42#include <sys/stat.h>
43#include "compatfd.h"
44#endif
45
46#include <glib.h>
47
48#include "main-loop.h"
49#include "qemu-timer.h"
50#include "slirp/libslirp.h"
51
52#ifndef _WIN32
53
54static int io_thread_fd = -1;
55
56void qemu_notify_event(void)
57{
58 /* Write 8 bytes to be compatible with eventfd. */
59 static const uint64_t val = 1;
60 ssize_t ret;
61
62 if (io_thread_fd == -1) {
63 return;
64 }
65 do {
66 ret = write(io_thread_fd, &val, sizeof(val));
67 } while (ret < 0 && errno == EINTR);
68
69 /* EAGAIN is fine, a read must be pending. */
70 if (ret < 0 && errno != EAGAIN) {
71 fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
72 strerror(errno));
73 exit(1);
74 }
75}
76
77static void qemu_event_read(void *opaque)
78{
79 int fd = (intptr_t)opaque;
80 ssize_t len;
81 char buffer[512];
82
83 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
84 do {
85 len = read(fd, buffer, sizeof(buffer));
86 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
87}
88
89static int qemu_event_init(void)
90{
91 int err;
92 int fds[2];
93
94 err = qemu_eventfd(fds);
95 if (err == -1) {
96 return -errno;
97 }
98 err = fcntl_setfl(fds[0], O_NONBLOCK);
99 if (err < 0) {
100 goto fail;
101 }
102 err = fcntl_setfl(fds[1], O_NONBLOCK);
103 if (err < 0) {
104 goto fail;
105 }
106 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
107 (void *)(intptr_t)fds[0]);
108
109 io_thread_fd = fds[1];
110 return 0;
111
112fail:
113 close(fds[0]);
114 close(fds[1]);
115 return err;
116}
117
118/* If we have signalfd, we mask out the signals we want to handle and then
119 * use signalfd to listen for them. We rely on whatever the current signal
120 * handler is to dispatch the signals when we receive them.
121 */
122static void sigfd_handler(void *opaque)
123{
124 int fd = (intptr_t)opaque;
125 struct qemu_signalfd_siginfo info;
126 struct sigaction action;
127 ssize_t len;
128
129 while (1) {
130 do {
131 len = read(fd, &info, sizeof(info));
132 } while (len == -1 && errno == EINTR);
133
134 if (len == -1 && errno == EAGAIN) {
135 break;
136 }
137
138 if (len != sizeof(info)) {
139 printf("read from sigfd returned %zd: %m\n", len);
140 return;
141 }
142
143 sigaction(info.ssi_signo, NULL, &action);
144 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
145 action.sa_sigaction(info.ssi_signo,
146 (siginfo_t *)&info, NULL);
147 } else if (action.sa_handler) {
148 action.sa_handler(info.ssi_signo);
149 }
150 }
151}
152
153static int qemu_signal_init(void)
154{
155 int sigfd;
156 sigset_t set;
157
158 /*
159 * SIG_IPI must be blocked in the main thread and must not be caught
160 * by sigwait() in the signal thread. Otherwise, the cpu thread will
161 * not catch it reliably.
162 */
163 sigemptyset(&set);
164 sigaddset(&set, SIG_IPI);
165 pthread_sigmask(SIG_BLOCK, &set, NULL);
166
167 sigemptyset(&set);
168 sigaddset(&set, SIGIO);
169 sigaddset(&set, SIGALRM);
170 sigaddset(&set, SIGBUS);
171 pthread_sigmask(SIG_BLOCK, &set, NULL);
172
173 sigfd = qemu_signalfd(&set);
174 if (sigfd == -1) {
175 fprintf(stderr, "failed to create signalfd\n");
176 return -errno;
177 }
178
179 fcntl_setfl(sigfd, O_NONBLOCK);
180
181 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
182 (void *)(intptr_t)sigfd);
183
184 return 0;
185}
186
187#else /* _WIN32 */
188
189HANDLE qemu_event_handle;
190
191static void dummy_event_handler(void *opaque)
192{
193}
194
195static int qemu_event_init(void)
196{
197 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
198 if (!qemu_event_handle) {
199 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
200 return -1;
201 }
202 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
203 return 0;
204}
205
206void qemu_notify_event(void)
207{
208 if (!SetEvent(qemu_event_handle)) {
209 fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
210 GetLastError());
211 exit(1);
212 }
213}
214
215static int qemu_signal_init(void)
216{
217 return 0;
218}
219#endif
220
221int qemu_init_main_loop(void)
222{
223 int ret;
224
225 qemu_mutex_lock_iothread();
226 ret = qemu_signal_init();
227 if (ret) {
228 return ret;
229 }
230
231 /* Note eventfd must be drained before signalfd handlers run */
232 ret = qemu_event_init();
233 if (ret) {
234 return ret;
235 }
236
237 return 0;
238}
239
240
241static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
242static int n_poll_fds;
243static int max_priority;
244
245static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
246 fd_set *xfds, struct timeval *tv)
247{
248 GMainContext *context = g_main_context_default();
249 int i;
250 int timeout = 0, cur_timeout;
251
252 g_main_context_prepare(context, &max_priority);
253
254 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
255 poll_fds, ARRAY_SIZE(poll_fds));
256 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
257
258 for (i = 0; i < n_poll_fds; i++) {
259 GPollFD *p = &poll_fds[i];
260
261 if ((p->events & G_IO_IN)) {
262 FD_SET(p->fd, rfds);
263 *max_fd = MAX(*max_fd, p->fd);
264 }
265 if ((p->events & G_IO_OUT)) {
266 FD_SET(p->fd, wfds);
267 *max_fd = MAX(*max_fd, p->fd);
268 }
269 if ((p->events & G_IO_ERR)) {
270 FD_SET(p->fd, xfds);
271 *max_fd = MAX(*max_fd, p->fd);
272 }
273 }
274
275 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
276 if (timeout >= 0 && timeout < cur_timeout) {
277 tv->tv_sec = timeout / 1000;
278 tv->tv_usec = (timeout % 1000) * 1000;
279 }
280}
281
282static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
283 bool err)
284{
285 GMainContext *context = g_main_context_default();
286
287 if (!err) {
288 int i;
289
290 for (i = 0; i < n_poll_fds; i++) {
291 GPollFD *p = &poll_fds[i];
292
293 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
294 p->revents |= G_IO_IN;
295 }
296 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
297 p->revents |= G_IO_OUT;
298 }
299 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
300 p->revents |= G_IO_ERR;
301 }
302 }
303 }
304
305 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
306 g_main_context_dispatch(context);
307 }
308}
309
310#ifdef _WIN32
311/***********************************************************/
312/* Polling handling */
313
314typedef struct PollingEntry {
315 PollingFunc *func;
316 void *opaque;
317 struct PollingEntry *next;
318} PollingEntry;
319
320static PollingEntry *first_polling_entry;
321
322int qemu_add_polling_cb(PollingFunc *func, void *opaque)
323{
324 PollingEntry **ppe, *pe;
325 pe = g_malloc0(sizeof(PollingEntry));
326 pe->func = func;
327 pe->opaque = opaque;
328 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
329 *ppe = pe;
330 return 0;
331}
332
333void qemu_del_polling_cb(PollingFunc *func, void *opaque)
334{
335 PollingEntry **ppe, *pe;
336 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
337 pe = *ppe;
338 if (pe->func == func && pe->opaque == opaque) {
339 *ppe = pe->next;
340 g_free(pe);
341 break;
342 }
343 }
344}
345
346/***********************************************************/
347/* Wait objects support */
348typedef struct WaitObjects {
349 int num;
350 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
351 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
352 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
353} WaitObjects;
354
355static WaitObjects wait_objects = {0};
356
357int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
358{
359 WaitObjects *w = &wait_objects;
360 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
361 return -1;
362 }
363 w->events[w->num] = handle;
364 w->func[w->num] = func;
365 w->opaque[w->num] = opaque;
366 w->num++;
367 return 0;
368}
369
370void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
371{
372 int i, found;
373 WaitObjects *w = &wait_objects;
374
375 found = 0;
376 for (i = 0; i < w->num; i++) {
377 if (w->events[i] == handle) {
378 found = 1;
379 }
380 if (found) {
381 w->events[i] = w->events[i + 1];
382 w->func[i] = w->func[i + 1];
383 w->opaque[i] = w->opaque[i + 1];
384 }
385 }
386 if (found) {
387 w->num--;
388 }
389}
390
391static void os_host_main_loop_wait(int *timeout)
392{
393 int ret, ret2, i;
394 PollingEntry *pe;
395
396 /* XXX: need to suppress polling by better using win32 events */
397 ret = 0;
398 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
399 ret |= pe->func(pe->opaque);
400 }
401 if (ret == 0) {
402 int err;
403 WaitObjects *w = &wait_objects;
404
405 qemu_mutex_unlock_iothread();
406 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
407 qemu_mutex_lock_iothread();
408 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
409 if (w->func[ret - WAIT_OBJECT_0]) {
410 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
411 }
412
413 /* Check for additional signaled events */
414 for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
415 /* Check if event is signaled */
416 ret2 = WaitForSingleObject(w->events[i], 0);
417 if (ret2 == WAIT_OBJECT_0) {
418 if (w->func[i]) {
419 w->func[i](w->opaque[i]);
420 }
421 } else if (ret2 != WAIT_TIMEOUT) {
422 err = GetLastError();
423 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
424 }
425 }
426 } else if (ret != WAIT_TIMEOUT) {
427 err = GetLastError();
428 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
429 }
430 }
431
432 *timeout = 0;
433}
434#else
435static inline void os_host_main_loop_wait(int *timeout)
436{
437}
438#endif
439
440int main_loop_wait(int nonblocking)
441{
442 fd_set rfds, wfds, xfds;
443 int ret, nfds;
444 struct timeval tv;
445 int timeout;
446
447 if (nonblocking) {
448 timeout = 0;
449 } else {
450 timeout = qemu_calculate_timeout();
451 qemu_bh_update_timeout(&timeout);
452 }
453
454 os_host_main_loop_wait(&timeout);
455
456 tv.tv_sec = timeout / 1000;
457 tv.tv_usec = (timeout % 1000) * 1000;
458
459 /* poll any events */
460 /* XXX: separate device handlers from system ones */
461 nfds = -1;
462 FD_ZERO(&rfds);
463 FD_ZERO(&wfds);
464 FD_ZERO(&xfds);
465
466#ifdef CONFIG_SLIRP
467 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
468#endif
469 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
470 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
471
472 if (timeout > 0) {
473 qemu_mutex_unlock_iothread();
474 }
475
476 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
477
478 if (timeout > 0) {
479 qemu_mutex_lock_iothread();
480 }
481
482 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
483 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
484#ifdef CONFIG_SLIRP
485 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
486#endif
487
488 qemu_run_all_timers();
489
490 /* Check bottom-halves last in case any of the earlier events triggered
491 them. */
492 qemu_bh_poll();
493
494 return ret;
495}