2
0

main-loop.c 14 KB

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