]> git.proxmox.com Git - qemu.git/blob - main-loop.c
vl: unify calls to init_timer_alarm
[qemu.git] / main-loop.c
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
25 #include "qemu-common.h"
26 #include "qemu-timer.h"
27 #include "slirp/slirp.h"
28 #include "main-loop.h"
29 #include "qemu-aio.h"
30
31 #ifndef _WIN32
32
33 #include "compatfd.h"
34
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);
82 sigaddset(&set, SIGIO);
83 sigaddset(&set, SIGALRM);
84 sigaddset(&set, SIGBUS);
85 pthread_sigmask(SIG_BLOCK, &set, NULL);
86
87 sigdelset(&set, SIG_IPI);
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
104 static int qemu_signal_init(void)
105 {
106 return 0;
107 }
108 #endif
109
110 static AioContext *qemu_aio_context;
111
112 void qemu_notify_event(void)
113 {
114 if (!qemu_aio_context) {
115 return;
116 }
117 aio_notify(qemu_aio_context);
118 }
119
120 int qemu_init_main_loop(void)
121 {
122 int ret;
123 GSource *src;
124
125 init_clocks();
126 if (init_timer_alarm() < 0) {
127 fprintf(stderr, "could not initialize alarm timer\n");
128 exit(1);
129 }
130
131 qemu_mutex_lock_iothread();
132 ret = qemu_signal_init();
133 if (ret) {
134 return ret;
135 }
136
137 qemu_aio_context = aio_context_new();
138 src = aio_get_g_source(qemu_aio_context);
139 g_source_attach(src, NULL);
140 g_source_unref(src);
141 return 0;
142 }
143
144 static fd_set rfds, wfds, xfds;
145 static int nfds;
146 static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
147 static int n_poll_fds;
148 static int max_priority;
149
150 #ifndef _WIN32
151 static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
152 fd_set *xfds, uint32_t *cur_timeout)
153 {
154 GMainContext *context = g_main_context_default();
155 int i;
156 int timeout = 0;
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
181 if (timeout >= 0 && timeout < *cur_timeout) {
182 *cur_timeout = timeout;
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
214 static int os_host_main_loop_wait(uint32_t timeout)
215 {
216 struct timeval tv, *tvarg = NULL;
217 int ret;
218
219 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout);
220
221 if (timeout < UINT32_MAX) {
222 tvarg = &tv;
223 tv.tv_sec = timeout / 1000;
224 tv.tv_usec = (timeout % 1000) * 1000;
225 }
226
227 if (timeout > 0) {
228 qemu_mutex_unlock_iothread();
229 }
230
231 ret = select(nfds + 1, &rfds, &wfds, &xfds, tvarg);
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
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;
280 int revents[MAXIMUM_WAIT_OBJECTS + 1];
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;
297 w->revents[w->num] = 0;
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];
316 w->revents[i] = w->revents[i + 1];
317 }
318 }
319 if (found) {
320 w->num--;
321 }
322 }
323
324 void qemu_fd_register(int fd)
325 {
326 WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier),
327 FD_READ | FD_ACCEPT | FD_CLOSE |
328 FD_CONNECT | FD_WRITE | FD_OOB);
329 }
330
331 static int os_host_main_loop_wait(uint32_t timeout)
332 {
333 GMainContext *context = g_main_context_default();
334 int ret, i;
335 PollingEntry *pe;
336 WaitObjects *w = &wait_objects;
337 gint poll_timeout;
338 static struct timeval tv0;
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 }
345 if (ret != 0) {
346 return ret;
347 }
348
349 if (nfds >= 0) {
350 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
351 if (ret != 0) {
352 timeout = 0;
353 }
354 }
355
356 g_main_context_prepare(context, &max_priority);
357 n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
358 poll_fds, ARRAY_SIZE(poll_fds));
359 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
360
361 for (i = 0; i < w->num; i++) {
362 poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
363 poll_fds[n_poll_fds + i].events = G_IO_IN;
364 }
365
366 if (poll_timeout < 0 || timeout < poll_timeout) {
367 poll_timeout = timeout;
368 }
369
370 qemu_mutex_unlock_iothread();
371 ret = g_poll(poll_fds, n_poll_fds + w->num, poll_timeout);
372 qemu_mutex_lock_iothread();
373 if (ret > 0) {
374 for (i = 0; i < w->num; i++) {
375 w->revents[i] = poll_fds[n_poll_fds + i].revents;
376 }
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 }
383
384 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
385 g_main_context_dispatch(context);
386 }
387
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
393 return ret;
394 }
395 #endif
396
397 int main_loop_wait(int nonblocking)
398 {
399 int ret;
400 uint32_t timeout = UINT32_MAX;
401
402 if (nonblocking) {
403 timeout = 0;
404 }
405
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
414 slirp_update_timeout(&timeout);
415 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
416 #endif
417 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
418 ret = os_host_main_loop_wait(timeout);
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
426 return ret;
427 }
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
436 void qemu_aio_flush(void)
437 {
438 aio_flush(qemu_aio_context);
439 }
440
441 bool qemu_aio_wait(void)
442 {
443 return aio_poll(qemu_aio_context, true);
444 }
445
446 #ifdef CONFIG_POSIX
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);
455 }
456 #endif
457
458 void qemu_aio_set_event_notifier(EventNotifier *notifier,
459 EventNotifierHandler *io_read,
460 AioFlushEventNotifierHandler *io_flush)
461 {
462 aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flush);
463 }