master
c 658 lines 17.1 KB
Raw
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/osdep.h"
26 #include "qapi/error.h"
27 #include "qemu/cutils.h"
28 #include "qemu/timer.h"
29 #include "system/cpu-timers.h"
30 #include "exec/icount.h"
31 #include "system/replay.h"
32 #include "qemu/main-loop.h"
33 #include "qemu/aio.h"
34 #include "block/thread-pool.h"
35 #include "qemu/error-report.h"
36 #include "qemu/queue.h"
37 #include "qom/object.h"
38
39 #ifndef _WIN32
40 #include <sys/wait.h>
41 #endif
42
43 #ifndef _WIN32
44
45 /* If we have signalfd, we mask out the signals we want to handle and then
46 * use signalfd to listen for them. We rely on whatever the current signal
47 * handler is to dispatch the signals when we receive them.
48 */
49 /*
50 * Disable CFI checks.
51 * We are going to call a signal handler directly. Such handler may or may not
52 * have been defined in our binary, so there's no guarantee that the pointer
53 * used to set the handler is a cfi-valid pointer. Since the handlers are
54 * stored in kernel memory, changing the handler to an attacker-defined
55 * function requires being able to call a sigaction() syscall,
56 * which is not as easy as overwriting a pointer in memory.
57 */
58 QEMU_DISABLE_CFI
59 static void sigfd_handler(void *opaque)
60 {
61 int fd = (intptr_t)opaque;
62 struct qemu_signalfd_siginfo info;
63 struct sigaction action;
64 ssize_t len;
65
66 while (1) {
67 len = RETRY_ON_EINTR(read(fd, &info, sizeof(info)));
68
69 if (len == -1 && errno == EAGAIN) {
70 break;
71 }
72
73 if (len != sizeof(info)) {
74 error_report("read from sigfd returned %zd: %s", len,
75 g_strerror(errno));
76 return;
77 }
78
79 sigaction(info.ssi_signo, NULL, &action);
80 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
81 sigaction_invoke(&action, &info);
82 } else if (action.sa_handler) {
83 action.sa_handler(info.ssi_signo);
84 }
85 }
86 }
87
88 static int qemu_signal_init(Error **errp)
89 {
90 int sigfd;
91 sigset_t set;
92
93 /*
94 * SIG_IPI must be blocked in the main thread and must not be caught
95 * by sigwait() in the signal thread. Otherwise, the cpu thread will
96 * not catch it reliably.
97 */
98 sigemptyset(&set);
99 sigaddset(&set, SIG_IPI);
100 sigaddset(&set, SIGIO);
101 sigaddset(&set, SIGALRM);
102 sigaddset(&set, SIGBUS);
103 /* SIGINT cannot be handled via signalfd, so that ^C can be used
104 * to interrupt QEMU when it is being run under gdb. SIGHUP and
105 * SIGTERM are also handled asynchronously, even though it is not
106 * strictly necessary, because they use the same handler as SIGINT.
107 */
108 pthread_sigmask(SIG_BLOCK, &set, NULL);
109
110 sigdelset(&set, SIG_IPI);
111 sigfd = qemu_signalfd(&set);
112 if (sigfd == -1) {
113 error_setg_errno(errp, errno, "failed to create signalfd");
114 return -errno;
115 }
116
117 if (!qemu_set_blocking(sigfd, false, errp)) {
118 close(sigfd);
119 return -EINVAL;
120 }
121
122 qemu_set_fd_handler(sigfd, sigfd_handler, NULL, (void *)(intptr_t)sigfd);
123
124 return 0;
125 }
126
127 #else /* _WIN32 */
128
129 static int qemu_signal_init(Error **errp)
130 {
131 return 0;
132 }
133 #endif
134
135 static AioContext *qemu_aio_context;
136 static QEMUBH *qemu_notify_bh;
137
138 static void notify_event_cb(void *opaque)
139 {
140 /* No need to do anything; this bottom half is only used to
141 * kick the kernel out of ppoll/poll/WaitForMultipleObjects.
142 */
143 }
144
145 AioContext *qemu_get_aio_context(void)
146 {
147 return qemu_aio_context;
148 }
149
150 void qemu_notify_event(void)
151 {
152 if (!qemu_aio_context) {
153 return;
154 }
155 qemu_bh_schedule(qemu_notify_bh);
156 }
157
158 static GArray *gpollfds;
159
160 int qemu_init_main_loop(Error **errp)
161 {
162 int ret;
163 GSource *src;
164
165 qemu_init_clocks(qemu_timer_notify_cb);
166
167 ret = qemu_signal_init(errp);
168 if (ret) {
169 return ret;
170 }
171
172 qemu_aio_context = aio_context_new(errp);
173 if (!qemu_aio_context) {
174 return -EMFILE;
175 }
176 qemu_set_current_aio_context(qemu_aio_context);
177 qemu_notify_bh = qemu_bh_new(notify_event_cb, NULL);
178 gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
179 src = aio_get_g_source(qemu_aio_context);
180 g_source_set_name(src, "aio-context");
181 g_source_attach(src, NULL);
182 g_source_unref(src);
183 src = iohandler_get_g_source();
184 g_source_set_name(src, "io-handler");
185 g_source_attach(src, NULL);
186 g_source_unref(src);
187 return 0;
188 }
189
190 static void main_loop_update_params(EventLoopBase *base, Error **errp)
191 {
192 ERRP_GUARD();
193
194 if (!qemu_aio_context) {
195 error_setg(errp, "qemu aio context not ready");
196 return;
197 }
198
199 aio_context_set_aio_params(qemu_aio_context, base->aio_max_batch);
200
201 aio_context_set_thread_pool_params(qemu_aio_context, base->thread_pool_min,
202 base->thread_pool_max, errp);
203 }
204
205 MainLoop *mloop;
206
207 static void main_loop_init(EventLoopBase *base, Error **errp)
208 {
209 MainLoop *m = MAIN_LOOP(base);
210
211 if (mloop) {
212 error_setg(errp, "only one main-loop instance allowed");
213 return;
214 }
215
216 main_loop_update_params(base, errp);
217
218 mloop = m;
219 }
220
221 static bool main_loop_prepare_delete(EventLoopBase *base, Error **errp)
222 {
223 error_setg(errp, "Deleting main loop '%s' is not supported",
224 object_get_canonical_path_component(OBJECT(base)));
225 return false;
226 }
227
228 static void main_loop_class_init(ObjectClass *oc, const void *class_data)
229 {
230 EventLoopBaseClass *bc = EVENT_LOOP_BASE_CLASS(oc);
231
232 bc->init = main_loop_init;
233 bc->update_params = main_loop_update_params;
234 bc->prepare_delete = main_loop_prepare_delete;
235 }
236
237 static const TypeInfo main_loop_info = {
238 .name = TYPE_MAIN_LOOP,
239 .parent = TYPE_EVENT_LOOP_BASE,
240 .class_init = main_loop_class_init,
241 .instance_size = sizeof(MainLoop),
242 };
243
244 static void main_loop_register_types(void)
245 {
246 type_register_static(&main_loop_info);
247 }
248
249 type_init(main_loop_register_types)
250
251 static int max_priority;
252
253 #ifndef _WIN32
254 static int glib_pollfds_idx;
255 static int glib_n_poll_fds;
256
257 static void glib_pollfds_fill(int64_t *cur_timeout)
258 {
259 GMainContext *context = g_main_context_default();
260 int timeout = 0;
261 int64_t timeout_ns;
262 int n;
263
264 g_main_context_prepare(context, &max_priority);
265
266 glib_pollfds_idx = gpollfds->len;
267 n = glib_n_poll_fds;
268 do {
269 GPollFD *pfds;
270 glib_n_poll_fds = n;
271 g_array_set_size(gpollfds, glib_pollfds_idx + glib_n_poll_fds);
272 pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
273 n = g_main_context_query(context, max_priority, &timeout, pfds,
274 glib_n_poll_fds);
275 } while (n != glib_n_poll_fds);
276
277 if (timeout < 0) {
278 timeout_ns = -1;
279 } else {
280 timeout_ns = (int64_t)timeout * (int64_t)SCALE_MS;
281 }
282
283 *cur_timeout = qemu_soonest_timeout(timeout_ns, *cur_timeout);
284 }
285
286 static void glib_pollfds_poll(void)
287 {
288 GMainContext *context = g_main_context_default();
289 GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
290
291 if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) {
292 g_main_context_dispatch(context);
293 }
294 }
295
296 #define MAX_MAIN_LOOP_SPIN (1000)
297
298 static int os_host_main_loop_wait(int64_t timeout)
299 {
300 GMainContext *context = g_main_context_default();
301 int ret;
302
303 g_main_context_acquire(context);
304
305 glib_pollfds_fill(&timeout);
306
307 bql_unlock();
308 replay_mutex_unlock();
309
310 ret = qemu_poll_ns((GPollFD *)gpollfds->data, gpollfds->len, timeout);
311
312 replay_mutex_lock();
313 bql_lock();
314
315 glib_pollfds_poll();
316
317 g_main_context_release(context);
318
319 return ret;
320 }
321 #else
322 /***********************************************************/
323 /* Polling handling */
324
325 typedef struct PollingEntry {
326 PollingFunc *func;
327 void *opaque;
328 struct PollingEntry *next;
329 } PollingEntry;
330
331 static PollingEntry *first_polling_entry;
332
333 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
334 {
335 PollingEntry **ppe, *pe;
336 pe = g_new0(PollingEntry, 1);
337 pe->func = func;
338 pe->opaque = opaque;
339 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
340 *ppe = pe;
341 return 0;
342 }
343
344 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
345 {
346 PollingEntry **ppe, *pe;
347 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
348 pe = *ppe;
349 if (pe->func == func && pe->opaque == opaque) {
350 *ppe = pe->next;
351 g_free(pe);
352 break;
353 }
354 }
355 }
356
357 /***********************************************************/
358 /* Wait objects support */
359 typedef struct WaitObjects {
360 int num;
361 int revents[MAXIMUM_WAIT_OBJECTS];
362 HANDLE events[MAXIMUM_WAIT_OBJECTS];
363 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
364 void *opaque[MAXIMUM_WAIT_OBJECTS];
365 } WaitObjects;
366
367 static WaitObjects wait_objects = {0};
368
369 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
370 {
371 int i;
372 WaitObjects *w = &wait_objects;
373
374 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
375 return -1;
376 }
377
378 for (i = 0; i < w->num; i++) {
379 /* check if the same handle is added twice */
380 if (w->events[i] == handle) {
381 return -1;
382 }
383 }
384
385 w->events[w->num] = handle;
386 w->func[w->num] = func;
387 w->opaque[w->num] = opaque;
388 w->revents[w->num] = 0;
389 w->num++;
390 return 0;
391 }
392
393 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
394 {
395 int i, found;
396 WaitObjects *w = &wait_objects;
397
398 found = 0;
399 for (i = 0; i < w->num; i++) {
400 if (w->events[i] == handle) {
401 found = 1;
402 }
403 if (found && i < (MAXIMUM_WAIT_OBJECTS - 1)) {
404 w->events[i] = w->events[i + 1];
405 w->func[i] = w->func[i + 1];
406 w->opaque[i] = w->opaque[i + 1];
407 w->revents[i] = w->revents[i + 1];
408 }
409 }
410 if (found) {
411 w->num--;
412 }
413 }
414
415 static int pollfds_fill(GArray *pollfds, fd_set *rfds, fd_set *wfds,
416 fd_set *xfds)
417 {
418 int nfds = -1;
419 int i;
420
421 for (i = 0; i < pollfds->len; i++) {
422 GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
423 int fd = pfd->fd;
424 int events = pfd->events;
425 if (events & G_IO_IN) {
426 FD_SET(fd, rfds);
427 nfds = MAX(nfds, fd);
428 }
429 if (events & G_IO_OUT) {
430 FD_SET(fd, wfds);
431 nfds = MAX(nfds, fd);
432 }
433 if (events & G_IO_PRI) {
434 FD_SET(fd, xfds);
435 nfds = MAX(nfds, fd);
436 }
437 }
438 return nfds;
439 }
440
441 static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,
442 fd_set *wfds, fd_set *xfds)
443 {
444 int i;
445
446 for (i = 0; i < pollfds->len; i++) {
447 GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
448 int fd = pfd->fd;
449 int revents = 0;
450
451 if (FD_ISSET(fd, rfds)) {
452 revents |= G_IO_IN;
453 }
454 if (FD_ISSET(fd, wfds)) {
455 revents |= G_IO_OUT;
456 }
457 if (FD_ISSET(fd, xfds)) {
458 revents |= G_IO_PRI;
459 }
460 pfd->revents = revents & pfd->events;
461 }
462 }
463
464 static int os_host_main_loop_wait(int64_t timeout)
465 {
466 GMainContext *context = g_main_context_default();
467 GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
468 int select_ret = 0;
469 int g_poll_ret, ret, i, n_poll_fds;
470 PollingEntry *pe;
471 WaitObjects *w = &wait_objects;
472 gint poll_timeout;
473 int64_t poll_timeout_ns;
474 static struct timeval tv0;
475 fd_set rfds, wfds, xfds;
476 int nfds;
477
478 g_main_context_acquire(context);
479
480 /* XXX: need to suppress polling by better using win32 events */
481 ret = 0;
482 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
483 ret |= pe->func(pe->opaque);
484 }
485 if (ret != 0) {
486 g_main_context_release(context);
487 return ret;
488 }
489
490 FD_ZERO(&rfds);
491 FD_ZERO(&wfds);
492 FD_ZERO(&xfds);
493 nfds = pollfds_fill(gpollfds, &rfds, &wfds, &xfds);
494 if (nfds >= 0) {
495 select_ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
496 if (select_ret != 0) {
497 timeout = 0;
498 }
499 if (select_ret > 0) {
500 pollfds_poll(gpollfds, nfds, &rfds, &wfds, &xfds);
501 }
502 }
503
504 g_main_context_prepare(context, &max_priority);
505 n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
506 poll_fds, ARRAY_SIZE(poll_fds));
507 g_assert(n_poll_fds + w->num <= ARRAY_SIZE(poll_fds));
508
509 for (i = 0; i < w->num; i++) {
510 poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
511 poll_fds[n_poll_fds + i].events = G_IO_IN;
512 }
513
514 if (poll_timeout < 0) {
515 poll_timeout_ns = -1;
516 } else {
517 poll_timeout_ns = (int64_t)poll_timeout * (int64_t)SCALE_MS;
518 }
519
520 poll_timeout_ns = qemu_soonest_timeout(poll_timeout_ns, timeout);
521
522 bql_unlock();
523
524 replay_mutex_unlock();
525
526 g_poll_ret = qemu_poll_ns(poll_fds, n_poll_fds + w->num, poll_timeout_ns);
527
528 replay_mutex_lock();
529
530 bql_lock();
531 if (g_poll_ret > 0) {
532 for (i = 0; i < w->num; i++) {
533 w->revents[i] = poll_fds[n_poll_fds + i].revents;
534 }
535 for (i = 0; i < w->num; i++) {
536 if (w->revents[i] && w->func[i]) {
537 w->func[i](w->opaque[i]);
538 }
539 }
540 }
541
542 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
543 g_main_context_dispatch(context);
544 }
545
546 g_main_context_release(context);
547
548 return select_ret || g_poll_ret;
549 }
550 #endif
551
552 static NotifierList main_loop_poll_notifiers =
553 NOTIFIER_LIST_INITIALIZER(main_loop_poll_notifiers);
554
555 void main_loop_poll_add_notifier(Notifier *notify)
556 {
557 notifier_list_add(&main_loop_poll_notifiers, notify);
558 }
559
560 void main_loop_poll_remove_notifier(Notifier *notify)
561 {
562 notifier_remove(notify);
563 }
564
565 void main_loop_wait(int nonblocking)
566 {
567 MainLoopPoll mlpoll = {
568 .state = MAIN_LOOP_POLL_FILL,
569 .timeout = UINT32_MAX,
570 .pollfds = gpollfds,
571 };
572 int ret;
573 int64_t timeout_ns;
574
575 if (nonblocking) {
576 mlpoll.timeout = 0;
577 }
578
579 /* poll any events */
580 g_array_set_size(gpollfds, 0); /* reset for new iteration */
581 /* XXX: separate device handlers from system ones */
582 notifier_list_notify(&main_loop_poll_notifiers, &mlpoll);
583
584 if (mlpoll.timeout == UINT32_MAX) {
585 timeout_ns = -1;
586 } else {
587 timeout_ns = (uint64_t)mlpoll.timeout * (int64_t)(SCALE_MS);
588 }
589
590 timeout_ns = qemu_soonest_timeout(timeout_ns,
591 timerlistgroup_deadline_ns(
592 &main_loop_tlg));
593
594 ret = os_host_main_loop_wait(timeout_ns);
595 mlpoll.state = ret < 0 ? MAIN_LOOP_POLL_ERR : MAIN_LOOP_POLL_OK;
596 notifier_list_notify(&main_loop_poll_notifiers, &mlpoll);
597
598 if (icount_enabled()) {
599 /*
600 * CPU thread can infinitely wait for event after
601 * missing the warp
602 */
603 icount_start_warp_timer();
604 }
605 qemu_clock_run_all_timers();
606 }
607
608 /* Functions to operate on the main QEMU AioContext. */
609
610 QEMUBH *qemu_bh_new_full(QEMUBHFunc *cb, void *opaque, const char *name,
611 MemReentrancyGuard *reentrancy_guard)
612 {
613 return aio_bh_new_full(qemu_aio_context, cb, opaque, name,
614 reentrancy_guard);
615 }
616
617 /*
618 * Functions to operate on the I/O handler AioContext.
619 * This context runs on top of main loop. We can't reuse qemu_aio_context
620 * because iohandlers mustn't be polled by aio_poll(qemu_aio_context).
621 */
622 static AioContext *iohandler_ctx;
623
624 static void iohandler_init(void)
625 {
626 if (!iohandler_ctx) {
627 iohandler_ctx = aio_context_new(&error_abort);
628 }
629 }
630
631 AioContext *iohandler_get_aio_context(void)
632 {
633 iohandler_init();
634 return iohandler_ctx;
635 }
636
637 GSource *iohandler_get_g_source(void)
638 {
639 iohandler_init();
640 return aio_get_g_source(iohandler_ctx);
641 }
642
643 void qemu_set_fd_handler(int fd,
644 IOHandler *fd_read,
645 IOHandler *fd_write,
646 void *opaque)
647 {
648 iohandler_init();
649 aio_set_fd_handler(iohandler_ctx, fd, fd_read, fd_write, NULL, NULL,
650 opaque);
651 }
652
653 void event_notifier_set_handler(EventNotifier *e,
654 EventNotifierHandler *handler)
655 {
656 iohandler_init();
657 aio_set_event_notifier(iohandler_ctx, e, handler, NULL, NULL);
658 }