db_object.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_object.c
  15. *
  16. * This file contains APIs for waiting on PAL handles (polling).
  17. */
  18. #include <asm/errno.h>
  19. #include <linux/poll.h>
  20. #include <linux/time.h>
  21. #include <linux/wait.h>
  22. #include "api.h"
  23. #include "pal.h"
  24. #include "pal_debug.h"
  25. #include "pal_defs.h"
  26. #include "pal_error.h"
  27. #include "pal_internal.h"
  28. #include "pal_linux.h"
  29. #include "pal_linux_defs.h"
  30. /* Wait for an event on any handle in the handle array and return this handle in `polled`. If no
  31. * ready-event handle was found, `polled` is set to NULL. */
  32. int _DkObjectsWaitAny(size_t count, PAL_HANDLE* handle_array, int64_t timeout_us,
  33. PAL_HANDLE* polled) {
  34. int ret;
  35. if (count == 0)
  36. return 0;
  37. if (count == 1 && handle_array[0] &&
  38. (IS_HANDLE_TYPE(handle_array[0], mutex) || IS_HANDLE_TYPE(handle_array[0], event))) {
  39. /* Special case of DkObjectsWaitAny(1, mutex/event, ...): perform a mutex-specific or
  40. * event-specific wait() callback instead of host-OS poll. */
  41. const struct handle_ops* ops = HANDLE_OPS(handle_array[0]);
  42. assert(ops && ops->wait);
  43. int rv = ops->wait(handle_array[0], timeout_us);
  44. if (!rv)
  45. *polled = handle_array[0];
  46. return rv;
  47. }
  48. /* Normal case of not mutex/event: poll on all handles in the array (their handle types can be
  49. * process, socket, pipe, device, file, eventfd). Note that this function is used only for
  50. * Graphene-internal purposes, so we can allocate arrays on stack (since they are small). */
  51. struct pollfd fds[count * MAX_FDS];
  52. PAL_HANDLE hdls[count * MAX_FDS];
  53. /* collect all FDs of all PAL handles that may report read/write events */
  54. size_t nfds = 0;
  55. for (size_t i = 0; i < count; i++) {
  56. PAL_HANDLE hdl = handle_array[i];
  57. if (!hdl)
  58. continue;
  59. /* ignore duplicate handles */
  60. for (size_t j = 0; j < i; j++)
  61. if (hdl == handle_array[j])
  62. continue;
  63. /* collect all internal-handle FDs (only those which are readable/writable) */
  64. for (size_t j = 0; j < MAX_FDS; j++) {
  65. PAL_FLG flags = HANDLE_HDR(hdl)->flags;
  66. /* hdl might be a mutex/event/non-pollable object, simply ignore it */
  67. if (hdl->generic.fds[j] == PAL_IDX_POISON)
  68. continue;
  69. if (flags & ERROR(j))
  70. continue;
  71. /* always ask host to wait for read event (if FD allows read events); however, no need
  72. * to ask host to wait for write event if FD is already known to be writable */
  73. int events = 0;
  74. events |= (flags & RFD(j)) ? POLLIN : 0;
  75. events |= ((flags & WFD(j)) && !(flags & WRITABLE(j))) ? POLLOUT : 0;
  76. if (events) {
  77. fds[nfds].fd = hdl->generic.fds[j];
  78. fds[nfds].events = events;
  79. fds[nfds].revents = 0;
  80. hdls[nfds] = hdl;
  81. nfds++;
  82. }
  83. }
  84. }
  85. if (!nfds) {
  86. /* did not find any waitable FDs (probably because their events were already cached) */
  87. ret = -PAL_ERROR_TRYAGAIN;
  88. goto out;
  89. }
  90. struct timespec timeout_ts;
  91. if (timeout_us >= 0) {
  92. int64_t sec = timeout_us / 1000000;
  93. int64_t microsec = timeout_us - sec * 1000000;
  94. timeout_ts.tv_sec = sec;
  95. timeout_ts.tv_nsec = microsec * 1000;
  96. }
  97. ret = INLINE_SYSCALL(ppoll, 5, fds, nfds, timeout_us >= 0 ? &timeout_ts : NULL, NULL, 0);
  98. if (IS_ERR(ret)) {
  99. switch (ERRNO(ret)) {
  100. case EINTR:
  101. case ERESTART:
  102. ret = -PAL_ERROR_INTERRUPTED;
  103. break;
  104. default:
  105. ret = unix_to_pal_error(ERRNO(ret));
  106. break;
  107. }
  108. goto out;
  109. }
  110. if (!ret) {
  111. /* timed out */
  112. ret = -PAL_ERROR_TRYAGAIN;
  113. goto out;
  114. }
  115. PAL_HANDLE polled_hdl = NULL;
  116. for (size_t i = 0; i < nfds; i++) {
  117. if (!fds[i].revents)
  118. continue;
  119. /* One PAL handle can have MAX_FDS internal FDs, so we must select one handle (first found)
  120. * from the ones on which the host reported events and then collect all revents on this
  121. * handle's internal FDs. Note that this is very inefficient. Each DkObjectsWaitAny()
  122. * returns only one of possibly many event-ready PAL handles. */
  123. if (!polled_hdl)
  124. polled_hdl = hdls[i];
  125. if (polled_hdl != hdls[i])
  126. continue;
  127. for (size_t j = 0; j < MAX_FDS; j++) {
  128. if (!(HANDLE_HDR(polled_hdl)->flags & (RFD(j) | WFD(j))))
  129. continue;
  130. if (polled_hdl->generic.fds[j] != (PAL_IDX)fds[i].fd)
  131. continue;
  132. /* found internal FD of PAL handle that corresponds to the FD of event-ready fds[i] */
  133. if (fds[i].revents & POLLOUT)
  134. HANDLE_HDR(polled_hdl)->flags |= WRITABLE(j);
  135. if (fds[i].revents & (POLLHUP | POLLERR))
  136. HANDLE_HDR(polled_hdl)->flags |= ERROR(j);
  137. }
  138. }
  139. *polled = polled_hdl;
  140. ret = polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  141. out:
  142. return ret;
  143. }
  144. /* Improved version of _DkObjectsWaitAny(): wait for specific events on all handles in the handle
  145. * array and return multiple events (including errors) reported by the host. Returns 0 on success,
  146. * PAL error on failure. */
  147. int _DkObjectsWaitEvents(size_t count, PAL_HANDLE* handle_array, PAL_FLG* events,
  148. PAL_FLG* ret_events, int64_t timeout_us) {
  149. int ret;
  150. if (count == 0)
  151. return 0;
  152. struct pollfd* fds = malloc(count * MAX_FDS * sizeof(*fds));
  153. if (!fds) {
  154. return -PAL_ERROR_NOMEM;
  155. }
  156. size_t* offsets = malloc(count * MAX_FDS * sizeof(*offsets));
  157. if (!offsets) {
  158. free(fds);
  159. return -PAL_ERROR_NOMEM;
  160. }
  161. /* collect all FDs of all PAL handles that may report read/write events */
  162. size_t nfds = 0;
  163. for (size_t i = 0; i < count; i++) {
  164. ret_events[i] = 0;
  165. PAL_HANDLE hdl = handle_array[i];
  166. if (!hdl)
  167. continue;
  168. /* collect all internal-handle FDs (only those which are readable/writable) */
  169. for (size_t j = 0; j < MAX_FDS; j++) {
  170. PAL_FLG flags = HANDLE_HDR(hdl)->flags;
  171. /* hdl might be a mutex/event/non-pollable object, simply ignore it */
  172. if (hdl->generic.fds[j] == PAL_IDX_POISON)
  173. continue;
  174. if (flags & ERROR(j))
  175. continue;
  176. int fdevents = 0;
  177. fdevents |= ((flags & RFD(j)) && (events[i] & PAL_WAIT_READ)) ? POLLIN : 0;
  178. fdevents |= ((flags & WFD(j)) && (events[i] & PAL_WAIT_WRITE)) ? POLLOUT : 0;
  179. if (fdevents) {
  180. fds[nfds].fd = hdl->generic.fds[j];
  181. fds[nfds].events = fdevents;
  182. fds[nfds].revents = 0;
  183. offsets[nfds] = i;
  184. nfds++;
  185. }
  186. }
  187. }
  188. if (!nfds) {
  189. /* did not find any waitable FDs (LibOS supplied closed/errored FDs or empty events) */
  190. ret = -PAL_ERROR_TRYAGAIN;
  191. goto out;
  192. }
  193. struct timespec timeout_ts;
  194. if (timeout_us >= 0) {
  195. int64_t sec = timeout_us / 1000000;
  196. int64_t microsec = timeout_us - sec * 1000000;
  197. timeout_ts.tv_sec = sec;
  198. timeout_ts.tv_nsec = microsec * 1000;
  199. }
  200. ret = INLINE_SYSCALL(ppoll, 5, fds, nfds, timeout_us >= 0 ? &timeout_ts : NULL, NULL, 0);
  201. if (IS_ERR(ret)) {
  202. switch (ERRNO(ret)) {
  203. case EINTR:
  204. case ERESTART:
  205. ret = -PAL_ERROR_INTERRUPTED;
  206. break;
  207. default:
  208. ret = unix_to_pal_error(ERRNO(ret));
  209. break;
  210. }
  211. goto out;
  212. }
  213. if (!ret) {
  214. /* timed out */
  215. ret = -PAL_ERROR_TRYAGAIN;
  216. goto out;
  217. }
  218. for (size_t i = 0; i < nfds; i++) {
  219. if (!fds[i].revents)
  220. continue;
  221. size_t j = offsets[i];
  222. if (fds[i].revents & POLLIN)
  223. ret_events[j] |= PAL_WAIT_READ;
  224. if (fds[i].revents & POLLOUT)
  225. ret_events[j] |= PAL_WAIT_WRITE;
  226. if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
  227. ret_events[j] |= PAL_WAIT_ERROR;
  228. }
  229. ret = 0;
  230. out:
  231. free(fds);
  232. free(offsets);
  233. return ret;
  234. }