main-loop.c 14 KB

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