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