2
0

main-loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 "main-loop.h"
  28. #ifndef _WIN32
  29. #include "compatfd.h"
  30. static int io_thread_fd = -1;
  31. void qemu_notify_event(void)
  32. {
  33. /* Write 8 bytes to be compatible with eventfd. */
  34. static const uint64_t val = 1;
  35. ssize_t ret;
  36. if (io_thread_fd == -1) {
  37. return;
  38. }
  39. do {
  40. ret = write(io_thread_fd, &val, sizeof(val));
  41. } while (ret < 0 && errno == EINTR);
  42. /* EAGAIN is fine, a read must be pending. */
  43. if (ret < 0 && errno != EAGAIN) {
  44. fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
  45. strerror(errno));
  46. exit(1);
  47. }
  48. }
  49. static void qemu_event_read(void *opaque)
  50. {
  51. int fd = (intptr_t)opaque;
  52. ssize_t len;
  53. char buffer[512];
  54. /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
  55. do {
  56. len = read(fd, buffer, sizeof(buffer));
  57. } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
  58. }
  59. static int qemu_event_init(void)
  60. {
  61. int err;
  62. int fds[2];
  63. err = qemu_eventfd(fds);
  64. if (err == -1) {
  65. return -errno;
  66. }
  67. err = fcntl_setfl(fds[0], O_NONBLOCK);
  68. if (err < 0) {
  69. goto fail;
  70. }
  71. err = fcntl_setfl(fds[1], O_NONBLOCK);
  72. if (err < 0) {
  73. goto fail;
  74. }
  75. qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
  76. (void *)(intptr_t)fds[0]);
  77. io_thread_fd = fds[1];
  78. return 0;
  79. fail:
  80. close(fds[0]);
  81. close(fds[1]);
  82. return err;
  83. }
  84. /* If we have signalfd, we mask out the signals we want to handle and then
  85. * use signalfd to listen for them. We rely on whatever the current signal
  86. * handler is to dispatch the signals when we receive them.
  87. */
  88. static void sigfd_handler(void *opaque)
  89. {
  90. int fd = (intptr_t)opaque;
  91. struct qemu_signalfd_siginfo info;
  92. struct sigaction action;
  93. ssize_t len;
  94. while (1) {
  95. do {
  96. len = read(fd, &info, sizeof(info));
  97. } while (len == -1 && errno == EINTR);
  98. if (len == -1 && errno == EAGAIN) {
  99. break;
  100. }
  101. if (len != sizeof(info)) {
  102. printf("read from sigfd returned %zd: %m\n", len);
  103. return;
  104. }
  105. sigaction(info.ssi_signo, NULL, &action);
  106. if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
  107. action.sa_sigaction(info.ssi_signo,
  108. (siginfo_t *)&info, NULL);
  109. } else if (action.sa_handler) {
  110. action.sa_handler(info.ssi_signo);
  111. }
  112. }
  113. }
  114. static int qemu_signal_init(void)
  115. {
  116. int sigfd;
  117. sigset_t set;
  118. /*
  119. * SIG_IPI must be blocked in the main thread and must not be caught
  120. * by sigwait() in the signal thread. Otherwise, the cpu thread will
  121. * not catch it reliably.
  122. */
  123. sigemptyset(&set);
  124. sigaddset(&set, SIG_IPI);
  125. pthread_sigmask(SIG_BLOCK, &set, NULL);
  126. sigemptyset(&set);
  127. sigaddset(&set, SIGIO);
  128. sigaddset(&set, SIGALRM);
  129. sigaddset(&set, SIGBUS);
  130. pthread_sigmask(SIG_BLOCK, &set, NULL);
  131. sigfd = qemu_signalfd(&set);
  132. if (sigfd == -1) {
  133. fprintf(stderr, "failed to create signalfd\n");
  134. return -errno;
  135. }
  136. fcntl_setfl(sigfd, O_NONBLOCK);
  137. qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
  138. (void *)(intptr_t)sigfd);
  139. return 0;
  140. }
  141. #else /* _WIN32 */
  142. HANDLE qemu_event_handle;
  143. static void dummy_event_handler(void *opaque)
  144. {
  145. }
  146. static int qemu_event_init(void)
  147. {
  148. qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
  149. if (!qemu_event_handle) {
  150. fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
  151. return -1;
  152. }
  153. qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
  154. return 0;
  155. }
  156. void qemu_notify_event(void)
  157. {
  158. if (!SetEvent(qemu_event_handle)) {
  159. fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
  160. GetLastError());
  161. exit(1);
  162. }
  163. }
  164. static int qemu_signal_init(void)
  165. {
  166. return 0;
  167. }
  168. #endif
  169. int qemu_init_main_loop(void)
  170. {
  171. int ret;
  172. qemu_mutex_lock_iothread();
  173. ret = qemu_signal_init();
  174. if (ret) {
  175. return ret;
  176. }
  177. /* Note eventfd must be drained before signalfd handlers run */
  178. ret = qemu_event_init();
  179. if (ret) {
  180. return ret;
  181. }
  182. return 0;
  183. }
  184. static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
  185. static int n_poll_fds;
  186. static int max_priority;
  187. static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
  188. fd_set *xfds, struct timeval *tv)
  189. {
  190. GMainContext *context = g_main_context_default();
  191. int i;
  192. int timeout = 0, cur_timeout;
  193. g_main_context_prepare(context, &max_priority);
  194. n_poll_fds = g_main_context_query(context, max_priority, &timeout,
  195. poll_fds, ARRAY_SIZE(poll_fds));
  196. g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
  197. for (i = 0; i < n_poll_fds; i++) {
  198. GPollFD *p = &poll_fds[i];
  199. if ((p->events & G_IO_IN)) {
  200. FD_SET(p->fd, rfds);
  201. *max_fd = MAX(*max_fd, p->fd);
  202. }
  203. if ((p->events & G_IO_OUT)) {
  204. FD_SET(p->fd, wfds);
  205. *max_fd = MAX(*max_fd, p->fd);
  206. }
  207. if ((p->events & G_IO_ERR)) {
  208. FD_SET(p->fd, xfds);
  209. *max_fd = MAX(*max_fd, p->fd);
  210. }
  211. }
  212. cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
  213. if (timeout >= 0 && timeout < cur_timeout) {
  214. tv->tv_sec = timeout / 1000;
  215. tv->tv_usec = (timeout % 1000) * 1000;
  216. }
  217. }
  218. static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
  219. bool err)
  220. {
  221. GMainContext *context = g_main_context_default();
  222. if (!err) {
  223. int i;
  224. for (i = 0; i < n_poll_fds; i++) {
  225. GPollFD *p = &poll_fds[i];
  226. if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
  227. p->revents |= G_IO_IN;
  228. }
  229. if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
  230. p->revents |= G_IO_OUT;
  231. }
  232. if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
  233. p->revents |= G_IO_ERR;
  234. }
  235. }
  236. }
  237. if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
  238. g_main_context_dispatch(context);
  239. }
  240. }
  241. #ifdef _WIN32
  242. /***********************************************************/
  243. /* Polling handling */
  244. typedef struct PollingEntry {
  245. PollingFunc *func;
  246. void *opaque;
  247. struct PollingEntry *next;
  248. } PollingEntry;
  249. static PollingEntry *first_polling_entry;
  250. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  251. {
  252. PollingEntry **ppe, *pe;
  253. pe = g_malloc0(sizeof(PollingEntry));
  254. pe->func = func;
  255. pe->opaque = opaque;
  256. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  257. *ppe = pe;
  258. return 0;
  259. }
  260. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  261. {
  262. PollingEntry **ppe, *pe;
  263. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  264. pe = *ppe;
  265. if (pe->func == func && pe->opaque == opaque) {
  266. *ppe = pe->next;
  267. g_free(pe);
  268. break;
  269. }
  270. }
  271. }
  272. /***********************************************************/
  273. /* Wait objects support */
  274. typedef struct WaitObjects {
  275. int num;
  276. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  277. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  278. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  279. } WaitObjects;
  280. static WaitObjects wait_objects = {0};
  281. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  282. {
  283. WaitObjects *w = &wait_objects;
  284. if (w->num >= MAXIMUM_WAIT_OBJECTS) {
  285. return -1;
  286. }
  287. w->events[w->num] = handle;
  288. w->func[w->num] = func;
  289. w->opaque[w->num] = opaque;
  290. w->num++;
  291. return 0;
  292. }
  293. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  294. {
  295. int i, found;
  296. WaitObjects *w = &wait_objects;
  297. found = 0;
  298. for (i = 0; i < w->num; i++) {
  299. if (w->events[i] == handle) {
  300. found = 1;
  301. }
  302. if (found) {
  303. w->events[i] = w->events[i + 1];
  304. w->func[i] = w->func[i + 1];
  305. w->opaque[i] = w->opaque[i + 1];
  306. }
  307. }
  308. if (found) {
  309. w->num--;
  310. }
  311. }
  312. static void os_host_main_loop_wait(int *timeout)
  313. {
  314. int ret, ret2, i;
  315. PollingEntry *pe;
  316. /* XXX: need to suppress polling by better using win32 events */
  317. ret = 0;
  318. for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
  319. ret |= pe->func(pe->opaque);
  320. }
  321. if (ret == 0) {
  322. int err;
  323. WaitObjects *w = &wait_objects;
  324. qemu_mutex_unlock_iothread();
  325. ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
  326. qemu_mutex_lock_iothread();
  327. if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
  328. if (w->func[ret - WAIT_OBJECT_0]) {
  329. w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
  330. }
  331. /* Check for additional signaled events */
  332. for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
  333. /* Check if event is signaled */
  334. ret2 = WaitForSingleObject(w->events[i], 0);
  335. if (ret2 == WAIT_OBJECT_0) {
  336. if (w->func[i]) {
  337. w->func[i](w->opaque[i]);
  338. }
  339. } else if (ret2 != WAIT_TIMEOUT) {
  340. err = GetLastError();
  341. fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
  342. }
  343. }
  344. } else if (ret != WAIT_TIMEOUT) {
  345. err = GetLastError();
  346. fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
  347. }
  348. }
  349. *timeout = 0;
  350. }
  351. #else
  352. static inline void os_host_main_loop_wait(int *timeout)
  353. {
  354. }
  355. #endif
  356. int main_loop_wait(int nonblocking)
  357. {
  358. fd_set rfds, wfds, xfds;
  359. int ret, nfds;
  360. struct timeval tv;
  361. int timeout;
  362. if (nonblocking) {
  363. timeout = 0;
  364. } else {
  365. timeout = qemu_calculate_timeout();
  366. qemu_bh_update_timeout(&timeout);
  367. }
  368. os_host_main_loop_wait(&timeout);
  369. tv.tv_sec = timeout / 1000;
  370. tv.tv_usec = (timeout % 1000) * 1000;
  371. /* poll any events */
  372. /* XXX: separate device handlers from system ones */
  373. nfds = -1;
  374. FD_ZERO(&rfds);
  375. FD_ZERO(&wfds);
  376. FD_ZERO(&xfds);
  377. #ifdef CONFIG_SLIRP
  378. slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
  379. #endif
  380. qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
  381. glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
  382. if (timeout > 0) {
  383. qemu_mutex_unlock_iothread();
  384. }
  385. ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
  386. if (timeout > 0) {
  387. qemu_mutex_lock_iothread();
  388. }
  389. glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
  390. qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
  391. #ifdef CONFIG_SLIRP
  392. slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
  393. #endif
  394. qemu_run_all_timers();
  395. /* Check bottom-halves last in case any of the earlier events triggered
  396. them. */
  397. qemu_bh_poll();
  398. return ret;
  399. }