main-loop.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_handler2(sigfd, NULL, sigfd_handler, NULL,
  91. (void *)(intptr_t)sigfd);
  92. return 0;
  93. }
  94. #else /* _WIN32 */
  95. static int qemu_signal_init(void)
  96. {
  97. return 0;
  98. }
  99. #endif
  100. static AioContext *qemu_aio_context;
  101. AioContext *qemu_get_aio_context(void)
  102. {
  103. return qemu_aio_context;
  104. }
  105. void qemu_notify_event(void)
  106. {
  107. if (!qemu_aio_context) {
  108. return;
  109. }
  110. aio_notify(qemu_aio_context);
  111. }
  112. static GArray *gpollfds;
  113. int qemu_init_main_loop(Error **errp)
  114. {
  115. int ret;
  116. GSource *src;
  117. Error *local_error = NULL;
  118. init_clocks();
  119. ret = qemu_signal_init();
  120. if (ret) {
  121. return ret;
  122. }
  123. qemu_aio_context = aio_context_new(&local_error);
  124. if (!qemu_aio_context) {
  125. error_propagate(errp, local_error);
  126. return -EMFILE;
  127. }
  128. gpollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
  129. src = aio_get_g_source(qemu_aio_context);
  130. g_source_attach(src, NULL);
  131. g_source_unref(src);
  132. return 0;
  133. }
  134. static int max_priority;
  135. #ifndef _WIN32
  136. static int glib_pollfds_idx;
  137. static int glib_n_poll_fds;
  138. static void glib_pollfds_fill(int64_t *cur_timeout)
  139. {
  140. GMainContext *context = g_main_context_default();
  141. int timeout = 0;
  142. int64_t timeout_ns;
  143. int n;
  144. g_main_context_prepare(context, &max_priority);
  145. glib_pollfds_idx = gpollfds->len;
  146. n = glib_n_poll_fds;
  147. do {
  148. GPollFD *pfds;
  149. glib_n_poll_fds = n;
  150. g_array_set_size(gpollfds, glib_pollfds_idx + glib_n_poll_fds);
  151. pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
  152. n = g_main_context_query(context, max_priority, &timeout, pfds,
  153. glib_n_poll_fds);
  154. } while (n != glib_n_poll_fds);
  155. if (timeout < 0) {
  156. timeout_ns = -1;
  157. } else {
  158. timeout_ns = (int64_t)timeout * (int64_t)SCALE_MS;
  159. }
  160. *cur_timeout = qemu_soonest_timeout(timeout_ns, *cur_timeout);
  161. }
  162. static void glib_pollfds_poll(void)
  163. {
  164. GMainContext *context = g_main_context_default();
  165. GPollFD *pfds = &g_array_index(gpollfds, GPollFD, glib_pollfds_idx);
  166. if (g_main_context_check(context, max_priority, pfds, glib_n_poll_fds)) {
  167. g_main_context_dispatch(context);
  168. }
  169. }
  170. #define MAX_MAIN_LOOP_SPIN (1000)
  171. static int os_host_main_loop_wait(int64_t timeout)
  172. {
  173. int ret;
  174. static int spin_counter;
  175. glib_pollfds_fill(&timeout);
  176. /* If the I/O thread is very busy or we are incorrectly busy waiting in
  177. * the I/O thread, this can lead to starvation of the BQL such that the
  178. * VCPU threads never run. To make sure we can detect the later case,
  179. * print a message to the screen. If we run into this condition, create
  180. * a fake timeout in order to give the VCPU threads a chance to run.
  181. */
  182. if (!timeout && (spin_counter > MAX_MAIN_LOOP_SPIN)) {
  183. static bool notified;
  184. if (!notified && !qtest_enabled()) {
  185. fprintf(stderr,
  186. "main-loop: WARNING: I/O thread spun for %d iterations\n",
  187. MAX_MAIN_LOOP_SPIN);
  188. notified = true;
  189. }
  190. timeout = SCALE_MS;
  191. }
  192. if (timeout) {
  193. spin_counter = 0;
  194. qemu_mutex_unlock_iothread();
  195. } else {
  196. spin_counter++;
  197. }
  198. ret = qemu_poll_ns((GPollFD *)gpollfds->data, gpollfds->len, timeout);
  199. if (timeout) {
  200. qemu_mutex_lock_iothread();
  201. }
  202. glib_pollfds_poll();
  203. return ret;
  204. }
  205. #else
  206. /***********************************************************/
  207. /* Polling handling */
  208. typedef struct PollingEntry {
  209. PollingFunc *func;
  210. void *opaque;
  211. struct PollingEntry *next;
  212. } PollingEntry;
  213. static PollingEntry *first_polling_entry;
  214. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  215. {
  216. PollingEntry **ppe, *pe;
  217. pe = g_malloc0(sizeof(PollingEntry));
  218. pe->func = func;
  219. pe->opaque = opaque;
  220. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  221. *ppe = pe;
  222. return 0;
  223. }
  224. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  225. {
  226. PollingEntry **ppe, *pe;
  227. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  228. pe = *ppe;
  229. if (pe->func == func && pe->opaque == opaque) {
  230. *ppe = pe->next;
  231. g_free(pe);
  232. break;
  233. }
  234. }
  235. }
  236. /***********************************************************/
  237. /* Wait objects support */
  238. typedef struct WaitObjects {
  239. int num;
  240. int revents[MAXIMUM_WAIT_OBJECTS + 1];
  241. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  242. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  243. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  244. } WaitObjects;
  245. static WaitObjects wait_objects = {0};
  246. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  247. {
  248. WaitObjects *w = &wait_objects;
  249. if (w->num >= MAXIMUM_WAIT_OBJECTS) {
  250. return -1;
  251. }
  252. w->events[w->num] = handle;
  253. w->func[w->num] = func;
  254. w->opaque[w->num] = opaque;
  255. w->revents[w->num] = 0;
  256. w->num++;
  257. return 0;
  258. }
  259. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  260. {
  261. int i, found;
  262. WaitObjects *w = &wait_objects;
  263. found = 0;
  264. for (i = 0; i < w->num; i++) {
  265. if (w->events[i] == handle) {
  266. found = 1;
  267. }
  268. if (found) {
  269. w->events[i] = w->events[i + 1];
  270. w->func[i] = w->func[i + 1];
  271. w->opaque[i] = w->opaque[i + 1];
  272. w->revents[i] = w->revents[i + 1];
  273. }
  274. }
  275. if (found) {
  276. w->num--;
  277. }
  278. }
  279. void qemu_fd_register(int fd)
  280. {
  281. WSAEventSelect(fd, event_notifier_get_handle(&qemu_aio_context->notifier),
  282. FD_READ | FD_ACCEPT | FD_CLOSE |
  283. FD_CONNECT | FD_WRITE | FD_OOB);
  284. }
  285. static int pollfds_fill(GArray *pollfds, fd_set *rfds, fd_set *wfds,
  286. fd_set *xfds)
  287. {
  288. int nfds = -1;
  289. int i;
  290. for (i = 0; i < pollfds->len; i++) {
  291. GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
  292. int fd = pfd->fd;
  293. int events = pfd->events;
  294. if (events & G_IO_IN) {
  295. FD_SET(fd, rfds);
  296. nfds = MAX(nfds, fd);
  297. }
  298. if (events & G_IO_OUT) {
  299. FD_SET(fd, wfds);
  300. nfds = MAX(nfds, fd);
  301. }
  302. if (events & G_IO_PRI) {
  303. FD_SET(fd, xfds);
  304. nfds = MAX(nfds, fd);
  305. }
  306. }
  307. return nfds;
  308. }
  309. static void pollfds_poll(GArray *pollfds, int nfds, fd_set *rfds,
  310. fd_set *wfds, fd_set *xfds)
  311. {
  312. int i;
  313. for (i = 0; i < pollfds->len; i++) {
  314. GPollFD *pfd = &g_array_index(pollfds, GPollFD, i);
  315. int fd = pfd->fd;
  316. int revents = 0;
  317. if (FD_ISSET(fd, rfds)) {
  318. revents |= G_IO_IN;
  319. }
  320. if (FD_ISSET(fd, wfds)) {
  321. revents |= G_IO_OUT;
  322. }
  323. if (FD_ISSET(fd, xfds)) {
  324. revents |= G_IO_PRI;
  325. }
  326. pfd->revents = revents & pfd->events;
  327. }
  328. }
  329. static int os_host_main_loop_wait(int64_t timeout)
  330. {
  331. GMainContext *context = g_main_context_default();
  332. GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
  333. int select_ret = 0;
  334. int g_poll_ret, ret, i, n_poll_fds;
  335. PollingEntry *pe;
  336. WaitObjects *w = &wait_objects;
  337. gint poll_timeout;
  338. int64_t poll_timeout_ns;
  339. static struct timeval tv0;
  340. fd_set rfds, wfds, xfds;
  341. int nfds;
  342. /* XXX: need to suppress polling by better using win32 events */
  343. ret = 0;
  344. for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
  345. ret |= pe->func(pe->opaque);
  346. }
  347. if (ret != 0) {
  348. return ret;
  349. }
  350. FD_ZERO(&rfds);
  351. FD_ZERO(&wfds);
  352. FD_ZERO(&xfds);
  353. nfds = pollfds_fill(gpollfds, &rfds, &wfds, &xfds);
  354. if (nfds >= 0) {
  355. select_ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv0);
  356. if (select_ret != 0) {
  357. timeout = 0;
  358. }
  359. if (select_ret > 0) {
  360. pollfds_poll(gpollfds, nfds, &rfds, &wfds, &xfds);
  361. }
  362. }
  363. g_main_context_prepare(context, &max_priority);
  364. n_poll_fds = g_main_context_query(context, max_priority, &poll_timeout,
  365. poll_fds, ARRAY_SIZE(poll_fds));
  366. g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
  367. for (i = 0; i < w->num; i++) {
  368. poll_fds[n_poll_fds + i].fd = (DWORD_PTR)w->events[i];
  369. poll_fds[n_poll_fds + i].events = G_IO_IN;
  370. }
  371. if (poll_timeout < 0) {
  372. poll_timeout_ns = -1;
  373. } else {
  374. poll_timeout_ns = (int64_t)poll_timeout * (int64_t)SCALE_MS;
  375. }
  376. poll_timeout_ns = qemu_soonest_timeout(poll_timeout_ns, timeout);
  377. qemu_mutex_unlock_iothread();
  378. g_poll_ret = qemu_poll_ns(poll_fds, n_poll_fds + w->num, poll_timeout_ns);
  379. qemu_mutex_lock_iothread();
  380. if (g_poll_ret > 0) {
  381. for (i = 0; i < w->num; i++) {
  382. w->revents[i] = poll_fds[n_poll_fds + i].revents;
  383. }
  384. for (i = 0; i < w->num; i++) {
  385. if (w->revents[i] && w->func[i]) {
  386. w->func[i](w->opaque[i]);
  387. }
  388. }
  389. }
  390. if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
  391. g_main_context_dispatch(context);
  392. }
  393. return select_ret || g_poll_ret;
  394. }
  395. #endif
  396. int main_loop_wait(int nonblocking)
  397. {
  398. int ret;
  399. uint32_t timeout = UINT32_MAX;
  400. int64_t timeout_ns;
  401. if (nonblocking) {
  402. timeout = 0;
  403. }
  404. /* poll any events */
  405. g_array_set_size(gpollfds, 0); /* reset for new iteration */
  406. /* XXX: separate device handlers from system ones */
  407. #ifdef CONFIG_SLIRP
  408. slirp_pollfds_fill(gpollfds, &timeout);
  409. #endif
  410. qemu_iohandler_fill(gpollfds);
  411. if (timeout == UINT32_MAX) {
  412. timeout_ns = -1;
  413. } else {
  414. timeout_ns = (uint64_t)timeout * (int64_t)(SCALE_MS);
  415. }
  416. timeout_ns = qemu_soonest_timeout(timeout_ns,
  417. timerlistgroup_deadline_ns(
  418. &main_loop_tlg));
  419. ret = os_host_main_loop_wait(timeout_ns);
  420. qemu_iohandler_poll(gpollfds, ret);
  421. #ifdef CONFIG_SLIRP
  422. slirp_pollfds_poll(gpollfds, (ret < 0));
  423. #endif
  424. qemu_clock_run_all_timers();
  425. return ret;
  426. }
  427. /* Functions to operate on the main QEMU AioContext. */
  428. QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
  429. {
  430. return aio_bh_new(qemu_aio_context, cb, opaque);
  431. }