main-loop.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 "qemu/sockets.h" // struct in_addr needed for libslirp.h
  27. #include "sysemu/qtest.h"
  28. #include "slirp/libslirp.h"
  29. #include "qemu/main-loop.h"
  30. #include "block/aio.h"
  31. #ifndef _WIN32
  32. #include "qemu/compatfd.h"
  33. /* If we have signalfd, we mask out the signals we want to handle and then
  34. * use signalfd to listen for them. We rely on whatever the current signal
  35. * handler is to dispatch the signals when we receive them.
  36. */
  37. static void sigfd_handler(void *opaque)
  38. {
  39. int fd = (intptr_t)opaque;
  40. struct qemu_signalfd_siginfo info;
  41. struct sigaction action;
  42. ssize_t len;
  43. while (1) {
  44. do {
  45. len = read(fd, &info, sizeof(info));
  46. } while (len == -1 && errno == EINTR);
  47. if (len == -1 && errno == EAGAIN) {
  48. break;
  49. }
  50. if (len != sizeof(info)) {
  51. printf("read from sigfd returned %zd: %m\n", len);
  52. return;
  53. }
  54. sigaction(info.ssi_signo, NULL, &action);
  55. if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
  56. action.sa_sigaction(info.ssi_signo,
  57. (siginfo_t *)&info, NULL);
  58. } else if (action.sa_handler) {
  59. action.sa_handler(info.ssi_signo);
  60. }
  61. }
  62. }
  63. static int qemu_signal_init(void)
  64. {
  65. int sigfd;
  66. sigset_t set;
  67. /*
  68. * SIG_IPI must be blocked in the main thread and must not be caught
  69. * by sigwait() in the signal thread. Otherwise, the cpu thread will
  70. * not catch it reliably.
  71. */
  72. sigemptyset(&set);
  73. sigaddset(&set, SIG_IPI);
  74. sigaddset(&set, SIGIO);
  75. sigaddset(&set, SIGALRM);
  76. sigaddset(&set, SIGBUS);
  77. /* SIGINT cannot be handled via signalfd, so that ^C can be used
  78. * to interrupt QEMU when it is being run under gdb. SIGHUP and
  79. * SIGTERM are also handled asynchronously, even though it is not
  80. * strictly necessary, because they use the same handler as SIGINT.
  81. */
  82. pthread_sigmask(SIG_BLOCK, &set, NULL);
  83. sigdelset(&set, SIG_IPI);
  84. sigfd = qemu_signalfd(&set);
  85. if (sigfd == -1) {
  86. fprintf(stderr, "failed to create signalfd\n");
  87. return -errno;
  88. }
  89. fcntl_setfl(sigfd, O_NONBLOCK);
  90. qemu_set_fd_handler(sigfd, sigfd_handler, NULL, (void *)(intptr_t)sigfd);
  91. return 0;
  92. }
  93. #else /* _WIN32 */
  94. static int qemu_signal_init(void)
  95. {
  96. return 0;
  97. }
  98. #endif
  99. static AioContext *qemu_aio_context;
  100. static QEMUBH *qemu_notify_bh;
  101. static void notify_event_cb(void *opaque)
  102. {
  103. /* No need to do anything; this bottom half is only used to
  104. * kick the kernel out of ppoll/poll/WaitForMultipleObjects.
  105. */
  106. }
  107. AioContext *qemu_get_aio_context(void)
  108. {
  109. return qemu_aio_context;
  110. }
  111. void qemu_notify_event(void)
  112. {
  113. if (!qemu_aio_context) {
  114. return;
  115. }
  116. qemu_bh_schedule(qemu_notify_bh);
  117. }
  118. static GArray *gpollfds;
  119. int qemu_init_main_loop(Error **errp)
  120. {
  121. int ret;
  122. GSource *src;
  123. Error *local_error = NULL;
  124. init_clocks();
  125. ret = qemu_signal_init();
  126. if (ret) {
  127. return ret;
  128. }
  129. qemu_aio_context = aio_context_new(&local_error);
  130. qemu_notify_bh = qemu_bh_new(notify_event_cb, NULL);
  131. if (!qemu_aio_context) {
  132. error_propagate(errp, local_error);
  133. return -EMFILE;
  134. }
  135. gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
  136. src = aio_get_g_source(qemu_aio_context);
  137. g_source_attach(src, NULL);
  138. g_source_unref(src);
  139. src = iohandler_get_g_source();
  140. g_source_attach(src, NULL);
  141. g_source_unref(src);
  142. return 0;
  143. }
  144. static int max_priority;
  145. #ifndef _WIN32
  146. static int glib_pollfds_idx;
  147. static int glib_n_poll_fds;
  148. static void glib_pollfds_fill(int64_t *cur_timeout)
  149. {
  150. GMainContext *context = g_main_context_default();
  151. int timeout = 0;
  152. int64_t timeout_ns;
  153. int n;
  154. g_main_context_prepare(context, &max_priority);
  155. glib_pollfds_idx = gpollfds->len;
  156. n = glib_n_poll_fds;
  157. do {
  158. GPollFD *pfds;
  159. glib_n_poll_fds = n;
  160. g_array_set_size(gpollfds, glib_pollfds_idx + glib_n_poll_fds);
  161. pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
  162. n = g_main_context_query(context, max_priority, &timeout, pfds,
  163. glib_n_poll_fds);
  164. } while (n != glib_n_poll_fds);
  165. if (timeout < 0) {
  166. timeout_ns = -1;
  167. } else {
  168. timeout_ns = (int64_t)timeout * (int64_t)SCALE_MS;
  169. }
  170. *cur_timeout = qemu_soonest_timeout(timeout_ns, *cur_timeout);
  171. }
  172. static void glib_pollfds_poll(void)
  173. {
  174. GMainContext *context = g_main_context_default();
  175. GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
  176. if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) {
  177. g_main_context_dispatch(context);
  178. }
  179. }
  180. #define MAX_MAIN_LOOP_SPIN (1000)
  181. static int os_host_main_loop_wait(int64_t timeout)
  182. {
  183. int ret;
  184. static int spin_counter;
  185. glib_pollfds_fill(&timeout);
  186. /* If the I/O thread is very busy or we are incorrectly busy waiting in
  187. * the I/O thread, this can lead to starvation of the BQL such that the
  188. * VCPU threads never run. To make sure we can detect the later case,
  189. * print a message to the screen. If we run into this condition, create
  190. * a fake timeout in order to give the VCPU threads a chance to run.
  191. */
  192. if (!timeout && (spin_counter > MAX_MAIN_LOOP_SPIN)) {
  193. static bool notified;
  194. if (!notified && !qtest_enabled()) {
  195. fprintf(stderr,
  196. "main-loop: WARNING: I/O thread spun for %d iterations\n",
  197. MAX_MAIN_LOOP_SPIN);
  198. notified = true;
  199. }
  200. timeout = SCALE_MS;
  201. }
  202. if (timeout) {
  203. spin_counter = 0;
  204. qemu_mutex_unlock_iothread();
  205. } else {
  206. spin_counter++;
  207. }
  208. ret = qemu_poll_ns((GPollFD *)gpollfds->data, gpollfds->len, timeout);
  209. if (timeout) {
  210. qemu_mutex_lock_iothread();
  211. }
  212. glib_pollfds_poll();
  213. return ret;
  214. }
  215. #else
  216. /***********************************************************/
  217. /* Polling handling */
  218. typedef struct PollingEntry {
  219. PollingFunc *func;
  220. void *opaque;
  221. struct PollingEntry *next;
  222. } PollingEntry;
  223. static PollingEntry *first_polling_entry;
  224. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  225. {
  226. PollingEntry **ppe, *pe;
  227. pe = g_malloc0(sizeof(PollingEntry));
  228. pe->func = func;
  229. pe->opaque = opaque;
  230. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  231. *ppe = pe;
  232. return 0;
  233. }
  234. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  235. {
  236. PollingEntry **ppe, *pe;
  237. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  238. pe = *ppe;
  239. if (pe->func == func && pe->opaque == opaque) {
  240. *ppe = pe->next;
  241. g_free(pe);
  242. break;
  243. }
  244. }
  245. }
  246. /***********************************************************/
  247. /* Wait objects support */
  248. typedef struct WaitObjects {
  249. int num;
  250. int revents[MAXIMUM_WAIT_OBJECTS + 1];
  251. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  252. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  253. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  254. } WaitObjects;
  255. static WaitObjects wait_objects = {0};
  256. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  257. {
  258. WaitObjects *w = &wait_objects;
  259. if (w->num >= MAXIMUM_WAIT_OBJECTS) {
  260. return -1;
  261. }
  262. w->events[w->num] = handle;
  263. w->func[w->num] = func;
  264. w->opaque[w->num] = opaque;
  265. w->revents[w->num] = 0;
  266. w->num++;
  267. return 0;
  268. }
  269. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  270. {
  271. int i, found;
  272. WaitObjects *w = &wait_objects;
  273. found = 0;
  274. for (i = 0; i < w->num; i++) {
  275. if (w->events[i] == handle) {
  276. found = 1;
  277. }
  278. if (found) {
  279. w->events[i] = w->events[i + 1];
  280. w->func[i] = w->func[i + 1];
  281. w->opaque[i] = w->opaque[i + 1];
  282. w->revents[i] = w->revents[i + 1];
  283. }
  284. }
  285. if (found) {
  286. w->num--;
  287. }
  288. }
  289. void qemu_fd_register(int fd)
  290. {
  291. WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier),
  292. FD_READ | FD_ACCEPT | FD_CLOSE |
  293. FD_CONNECT | FD_WRITE | FD_OOB);
  294. }
  295. static int pollfds_fill(GArray *pollfds, fd_set *rfds, fd_set *wfds,
  296. fd_set *xfds)
  297. {
  298. int nfds = -1;
  299. int i;
  300. for (i = 0; i < pollfds->len; i++) {
  301. GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
  302. int fd = pfd->fd;
  303. int events = pfd->events;
  304. if (events & G_IO_IN) {
  305. FD_SET(fd, rfds);
  306. nfds = MAX(nfds, fd);
  307. }
  308. if (events & G_IO_OUT) {
  309. FD_SET(fd, wfds);
  310. nfds = MAX(nfds, fd);
  311. }
  312. if (events & G_IO_PRI) {
  313. FD_SET(fd, xfds);
  314. nfds = MAX(nfds, fd);
  315. }
  316. }
  317. return nfds;
  318. }
  319. static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,
  320. fd_set *wfds, fd_set *xfds)
  321. {
  322. int i;
  323. for (i = 0; i < pollfds->len; i++) {
  324. GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
  325. int fd = pfd->fd;
  326. int revents = 0;
  327. if (FD_ISSET(fd, rfds)) {
  328. revents |= G_IO_IN;
  329. }
  330. if (FD_ISSET(fd, wfds)) {
  331. revents |= G_IO_OUT;
  332. }
  333. if (FD_ISSET(fd, xfds)) {
  334. revents |= G_IO_PRI;
  335. }
  336. pfd->revents = revents & pfd->events;
  337. }
  338. }
  339. static int os_host_main_loop_wait(int64_t timeout)
  340. {
  341. GMainContext *context = g_main_context_default();
  342. GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
  343. int select_ret = 0;
  344. int g_poll_ret, ret, i, n_poll_fds;
  345. PollingEntry *pe;
  346. WaitObjects *w = &wait_objects;
  347. gint poll_timeout;
  348. int64_t poll_timeout_ns;
  349. static struct timeval tv0;
  350. fd_set rfds, wfds, xfds;
  351. int nfds;
  352. /* XXX: need to suppress polling by better using win32 events */
  353. ret = 0;
  354. for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
  355. ret |= pe->func(pe->opaque);
  356. }
  357. if (ret != 0) {
  358. return ret;
  359. }
  360. FD_ZERO(&rfds);
  361. FD_ZERO(&wfds);
  362. FD_ZERO(&xfds);
  363. nfds = pollfds_fill(gpollfds, &rfds, &wfds, &xfds);
  364. if (nfds >= 0) {
  365. select_ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
  366. if (select_ret != 0) {
  367. timeout = 0;
  368. }
  369. if (select_ret > 0) {
  370. pollfds_poll(gpollfds, nfds, &rfds, &wfds, &xfds);
  371. }
  372. }
  373. g_main_context_prepare(context, &max_priority);
  374. n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
  375. poll_fds, ARRAY_SIZE(poll_fds));
  376. g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
  377. for (i = 0; i < w->num; i++) {
  378. poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
  379. poll_fds[n_poll_fds + i].events = G_IO_IN;
  380. }
  381. if (poll_timeout < 0) {
  382. poll_timeout_ns = -1;
  383. } else {
  384. poll_timeout_ns = (int64_t)poll_timeout * (int64_t)SCALE_MS;
  385. }
  386. poll_timeout_ns = qemu_soonest_timeout(poll_timeout_ns, timeout);
  387. qemu_mutex_unlock_iothread();
  388. g_poll_ret = qemu_poll_ns(poll_fds, n_poll_fds + w->num, poll_timeout_ns);
  389. qemu_mutex_lock_iothread();
  390. if (g_poll_ret > 0) {
  391. for (i = 0; i < w->num; i++) {
  392. w->revents[i] = poll_fds[n_poll_fds + i].revents;
  393. }
  394. for (i = 0; i < w->num; i++) {
  395. if (w->revents[i] && w->func[i]) {
  396. w->func[i](w->opaque[i]);
  397. }
  398. }
  399. }
  400. if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
  401. g_main_context_dispatch(context);
  402. }
  403. return select_ret || g_poll_ret;
  404. }
  405. #endif
  406. int main_loop_wait(int nonblocking)
  407. {
  408. int ret;
  409. uint32_t timeout = UINT32_MAX;
  410. int64_t timeout_ns;
  411. if (nonblocking) {
  412. timeout = 0;
  413. }
  414. /* poll any events */
  415. g_array_set_size(gpollfds, 0); /* reset for new iteration */
  416. /* XXX: separate device handlers from system ones */
  417. #ifdef CONFIG_SLIRP
  418. slirp_pollfds_fill(gpollfds, &timeout);
  419. #endif
  420. if (timeout == UINT32_MAX) {
  421. timeout_ns = -1;
  422. } else {
  423. timeout_ns = (uint64_t)timeout * (int64_t)(SCALE_MS);
  424. }
  425. timeout_ns = qemu_soonest_timeout(timeout_ns,
  426. timerlistgroup_deadline_ns(
  427. &main_loop_tlg));
  428. ret = os_host_main_loop_wait(timeout_ns);
  429. #ifdef CONFIG_SLIRP
  430. slirp_pollfds_poll(gpollfds, (ret < 0));
  431. #endif
  432. qemu_clock_run_all_timers();
  433. return ret;
  434. }
  435. /* Functions to operate on the main QEMU AioContext. */
  436. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  437. {
  438. return aio_bh_new(qemu_aio_context, cb, opaque);
  439. }