2
0

main-loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "qemu-common.h"
  25. #include "qemu/timer.h"
  26. #include "slirp/slirp.h"
  27. #include "qemu/main-loop.h"
  28. #include "block/aio.h"
  29. #ifndef _WIN32
  30. #include "qemu/compatfd.h"
  31. /* If we have signalfd, we mask out the signals we want to handle and then
  32. * use signalfd to listen for them. We rely on whatever the current signal
  33. * handler is to dispatch the signals when we receive them.
  34. */
  35. static void sigfd_handler(void *opaque)
  36. {
  37. int fd = (intptr_t)opaque;
  38. struct qemu_signalfd_siginfo info;
  39. struct sigaction action;
  40. ssize_t len;
  41. while (1) {
  42. do {
  43. len = read(fd, &info, sizeof(info));
  44. } while (len == -1 && errno == EINTR);
  45. if (len == -1 && errno == EAGAIN) {
  46. break;
  47. }
  48. if (len != sizeof(info)) {
  49. printf("read from sigfd returned %zd: %m\n", len);
  50. return;
  51. }
  52. sigaction(info.ssi_signo, NULL, &action);
  53. if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
  54. action.sa_sigaction(info.ssi_signo,
  55. (siginfo_t *)&info, NULL);
  56. } else if (action.sa_handler) {
  57. action.sa_handler(info.ssi_signo);
  58. }
  59. }
  60. }
  61. static int qemu_signal_init(void)
  62. {
  63. int sigfd;
  64. sigset_t set;
  65. /*
  66. * SIG_IPI must be blocked in the main thread and must not be caught
  67. * by sigwait() in the signal thread. Otherwise, the cpu thread will
  68. * not catch it reliably.
  69. */
  70. sigemptyset(&set);
  71. sigaddset(&set, SIG_IPI);
  72. sigaddset(&set, SIGIO);
  73. sigaddset(&set, SIGALRM);
  74. sigaddset(&set, SIGBUS);
  75. pthread_sigmask(SIG_BLOCK, &set, NULL);
  76. sigdelset(&set, SIG_IPI);
  77. sigfd = qemu_signalfd(&set);
  78. if (sigfd == -1) {
  79. fprintf(stderr, "failed to create signalfd\n");
  80. return -errno;
  81. }
  82. fcntl_setfl(sigfd, O_NONBLOCK);
  83. qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
  84. (void *)(intptr_t)sigfd);
  85. return 0;
  86. }
  87. #else /* _WIN32 */
  88. static int qemu_signal_init(void)
  89. {
  90. return 0;
  91. }
  92. #endif
  93. static AioContext *qemu_aio_context;
  94. void qemu_notify_event(void)
  95. {
  96. if (!qemu_aio_context) {
  97. return;
  98. }
  99. aio_notify(qemu_aio_context);
  100. }
  101. int qemu_init_main_loop(void)
  102. {
  103. int ret;
  104. GSource *src;
  105. init_clocks();
  106. if (init_timer_alarm() < 0) {
  107. fprintf(stderr, "could not initialize alarm timer\n");
  108. exit(1);
  109. }
  110. ret = qemu_signal_init();
  111. if (ret) {
  112. return ret;
  113. }
  114. qemu_aio_context = aio_context_new();
  115. src = aio_get_g_source(qemu_aio_context);
  116. g_source_attach(src, NULL);
  117. g_source_unref(src);
  118. return 0;
  119. }
  120. static fd_set rfds, wfds, xfds;
  121. static int nfds;
  122. static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
  123. static int n_poll_fds;
  124. static int max_priority;
  125. #ifndef _WIN32
  126. static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
  127. fd_set *xfds, uint32_t *cur_timeout)
  128. {
  129. GMainContext *context = g_main_context_default();
  130. int i;
  131. int timeout = 0;
  132. g_main_context_prepare(context, &max_priority);
  133. n_poll_fds = g_main_context_query(context, max_priority, &timeout,
  134. poll_fds, ARRAY_SIZE(poll_fds));
  135. g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
  136. for (i = 0; i < n_poll_fds; i++) {
  137. GPollFD *p = &poll_fds[i];
  138. if ((p->events & G_IO_IN)) {
  139. FD_SET(p->fd, rfds);
  140. *max_fd = MAX(*max_fd, p->fd);
  141. }
  142. if ((p->events & G_IO_OUT)) {
  143. FD_SET(p->fd, wfds);
  144. *max_fd = MAX(*max_fd, p->fd);
  145. }
  146. if ((p->events & G_IO_ERR)) {
  147. FD_SET(p->fd, xfds);
  148. *max_fd = MAX(*max_fd, p->fd);
  149. }
  150. }
  151. if (timeout >= 0 && timeout < *cur_timeout) {
  152. *cur_timeout = timeout;
  153. }
  154. }
  155. static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
  156. bool err)
  157. {
  158. GMainContext *context = g_main_context_default();
  159. if (!err) {
  160. int i;
  161. for (i = 0; i < n_poll_fds; i++) {
  162. GPollFD *p = &poll_fds[i];
  163. if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
  164. p->revents |= G_IO_IN;
  165. }
  166. if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
  167. p->revents |= G_IO_OUT;
  168. }
  169. if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
  170. p->revents |= G_IO_ERR;
  171. }
  172. }
  173. }
  174. if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
  175. g_main_context_dispatch(context);
  176. }
  177. }
  178. static int os_host_main_loop_wait(uint32_t timeout)
  179. {
  180. struct timeval tv, *tvarg = NULL;
  181. int ret;
  182. glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout);
  183. if (timeout < UINT32_MAX) {
  184. tvarg = &tv;
  185. tv.tv_sec = timeout / 1000;
  186. tv.tv_usec = (timeout % 1000) * 1000;
  187. }
  188. if (timeout > 0) {
  189. qemu_mutex_unlock_iothread();
  190. }
  191. ret = select(nfds + 1, &rfds, &wfds, &xfds, tvarg);
  192. if (timeout > 0) {
  193. qemu_mutex_lock_iothread();
  194. }
  195. glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
  196. return ret;
  197. }
  198. #else
  199. /***********************************************************/
  200. /* Polling handling */
  201. typedef struct PollingEntry {
  202. PollingFunc *func;
  203. void *opaque;
  204. struct PollingEntry *next;
  205. } PollingEntry;
  206. static PollingEntry *first_polling_entry;
  207. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  208. {
  209. PollingEntry **ppe, *pe;
  210. pe = g_malloc0(sizeof(PollingEntry));
  211. pe->func = func;
  212. pe->opaque = opaque;
  213. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  214. *ppe = pe;
  215. return 0;
  216. }
  217. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  218. {
  219. PollingEntry **ppe, *pe;
  220. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  221. pe = *ppe;
  222. if (pe->func == func && pe->opaque == opaque) {
  223. *ppe = pe->next;
  224. g_free(pe);
  225. break;
  226. }
  227. }
  228. }
  229. /***********************************************************/
  230. /* Wait objects support */
  231. typedef struct WaitObjects {
  232. int num;
  233. int revents[MAXIMUM_WAIT_OBJECTS + 1];
  234. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  235. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  236. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  237. } WaitObjects;
  238. static WaitObjects wait_objects = {0};
  239. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  240. {
  241. WaitObjects *w = &wait_objects;
  242. if (w->num >= MAXIMUM_WAIT_OBJECTS) {
  243. return -1;
  244. }
  245. w->events[w->num] = handle;
  246. w->func[w->num] = func;
  247. w->opaque[w->num] = opaque;
  248. w->revents[w->num] = 0;
  249. w->num++;
  250. return 0;
  251. }
  252. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  253. {
  254. int i, found;
  255. WaitObjects *w = &wait_objects;
  256. found = 0;
  257. for (i = 0; i < w->num; i++) {
  258. if (w->events[i] == handle) {
  259. found = 1;
  260. }
  261. if (found) {
  262. w->events[i] = w->events[i + 1];
  263. w->func[i] = w->func[i + 1];
  264. w->opaque[i] = w->opaque[i + 1];
  265. w->revents[i] = w->revents[i + 1];
  266. }
  267. }
  268. if (found) {
  269. w->num--;
  270. }
  271. }
  272. void qemu_fd_register(int fd)
  273. {
  274. WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier),
  275. FD_READ | FD_ACCEPT | FD_CLOSE |
  276. FD_CONNECT | FD_WRITE | FD_OOB);
  277. }
  278. static int os_host_main_loop_wait(uint32_t timeout)
  279. {
  280. GMainContext *context = g_main_context_default();
  281. int select_ret, g_poll_ret, ret, i;
  282. PollingEntry *pe;
  283. WaitObjects *w = &wait_objects;
  284. gint poll_timeout;
  285. static struct timeval tv0;
  286. /* XXX: need to suppress polling by better using win32 events */
  287. ret = 0;
  288. for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
  289. ret |= pe->func(pe->opaque);
  290. }
  291. if (ret != 0) {
  292. return ret;
  293. }
  294. g_main_context_prepare(context, &max_priority);
  295. n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
  296. poll_fds, ARRAY_SIZE(poll_fds));
  297. g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
  298. for (i = 0; i < w->num; i++) {
  299. poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
  300. poll_fds[n_poll_fds + i].events = G_IO_IN;
  301. }
  302. if (poll_timeout < 0 || timeout < poll_timeout) {
  303. poll_timeout = timeout;
  304. }
  305. qemu_mutex_unlock_iothread();
  306. g_poll_ret = g_poll(poll_fds, n_poll_fds + w->num, poll_timeout);
  307. qemu_mutex_lock_iothread();
  308. if (g_poll_ret > 0) {
  309. for (i = 0; i < w->num; i++) {
  310. w->revents[i] = poll_fds[n_poll_fds + i].revents;
  311. }
  312. for (i = 0; i < w->num; i++) {
  313. if (w->revents[i] && w->func[i]) {
  314. w->func[i](w->opaque[i]);
  315. }
  316. }
  317. }
  318. if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
  319. g_main_context_dispatch(context);
  320. }
  321. /* Call select after g_poll to avoid a useless iteration and therefore
  322. * improve socket latency.
  323. */
  324. if (nfds >= 0) {
  325. select_ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
  326. if (select_ret != 0) {
  327. timeout = 0;
  328. }
  329. }
  330. return select_ret || g_poll_ret;
  331. }
  332. #endif
  333. int main_loop_wait(int nonblocking)
  334. {
  335. int ret;
  336. uint32_t timeout = UINT32_MAX;
  337. if (nonblocking) {
  338. timeout = 0;
  339. }
  340. /* poll any events */
  341. /* XXX: separate device handlers from system ones */
  342. nfds = -1;
  343. FD_ZERO(&rfds);
  344. FD_ZERO(&wfds);
  345. FD_ZERO(&xfds);
  346. #ifdef CONFIG_SLIRP
  347. slirp_update_timeout(&timeout);
  348. slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
  349. #endif
  350. qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
  351. ret = os_host_main_loop_wait(timeout);
  352. qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
  353. #ifdef CONFIG_SLIRP
  354. slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
  355. #endif
  356. qemu_run_all_timers();
  357. return ret;
  358. }
  359. /* Functions to operate on the main QEMU AioContext. */
  360. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  361. {
  362. return aio_bh_new(qemu_aio_context, cb, opaque);
  363. }
  364. bool qemu_aio_wait(void)
  365. {
  366. return aio_poll(qemu_aio_context, true);
  367. }
  368. #ifdef CONFIG_POSIX
  369. void qemu_aio_set_fd_handler(int fd,
  370. IOHandler *io_read,
  371. IOHandler *io_write,
  372. AioFlushHandler *io_flush,
  373. void *opaque)
  374. {
  375. aio_set_fd_handler(qemu_aio_context, fd, io_read, io_write, io_flush,
  376. opaque);
  377. }
  378. #endif
  379. void qemu_aio_set_event_notifier(EventNotifier *notifier,
  380. EventNotifierHandler *io_read,
  381. AioFlushEventNotifierHandler *io_flush)
  382. {
  383. aio_set_event_notifier(qemu_aio_context, notifier, io_read, io_flush);
  384. }