db_object.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <linux/poll.h>
  19. #include <linux/time.h>
  20. #include <linux/wait.h>
  21. #include "api.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_linux_defs.h"
  29. #include "pal_linux_error.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. ret = ocall_poll(fds, nfds, timeout_us);
  91. if (IS_ERR(ret)) {
  92. switch (ERRNO(ret)) {
  93. case EINTR:
  94. case ERESTART:
  95. ret = -PAL_ERROR_INTERRUPTED;
  96. break;
  97. default:
  98. ret = unix_to_pal_error(ERRNO(ret));
  99. break;
  100. }
  101. goto out;
  102. }
  103. if (!ret) {
  104. /* timed out */
  105. ret = -PAL_ERROR_TRYAGAIN;
  106. goto out;
  107. }
  108. PAL_HANDLE polled_hdl = NULL;
  109. for (size_t i = 0; i < nfds; i++) {
  110. if (!fds[i].revents)
  111. continue;
  112. /* One PAL handle can have MAX_FDS internal FDs, so we must select one handle (first one)
  113. * from the ones on which the host reported events and then collect all revents on this
  114. * handle's internal FDs. Note that this is very inefficient. Each DkObjectsWaitAny()
  115. * returns only one of possibly many event-ready PAL handles. */
  116. if (!polled_hdl)
  117. polled_hdl = hdls[i];
  118. if (polled_hdl != hdls[i])
  119. continue;
  120. for (size_t j = 0; j < MAX_FDS; j++) {
  121. if (!(HANDLE_HDR(polled_hdl)->flags & (RFD(j) | WFD(j))))
  122. continue;
  123. if (polled_hdl->generic.fds[j] != (PAL_IDX)fds[i].fd)
  124. continue;
  125. /* found internal FD of PAL handle that corresponds to the FD of
  126. * event-ready fds[i] */
  127. if (fds[i].revents & POLLOUT)
  128. HANDLE_HDR(polled_hdl)->flags |= WRITABLE(j);
  129. if (fds[i].revents & (POLLHUP | POLLERR))
  130. HANDLE_HDR(polled_hdl)->flags |= ERROR(j);
  131. }
  132. }
  133. *polled = polled_hdl;
  134. ret = polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  135. out:
  136. return ret;
  137. }
  138. /* Improved version of _DkObjectsWaitAny(): wait for specific events on all handles in the handle
  139. * array and return multiple events (including errors) reported by the host.
  140. * Returns 0 on success,
  141. * PAL error on failure. */
  142. int _DkObjectsWaitEvents(size_t count, PAL_HANDLE* handle_array, PAL_FLG* events,
  143. PAL_FLG* ret_events, int64_t timeout_us) {
  144. int ret;
  145. if (count == 0)
  146. return 0;
  147. struct pollfd* fds = malloc(count * MAX_FDS * sizeof(*fds));
  148. if (!fds) {
  149. return -PAL_ERROR_NOMEM;
  150. }
  151. size_t* offsets = malloc(count * MAX_FDS * sizeof(*offsets));
  152. if (!offsets) {
  153. free(fds);
  154. return -PAL_ERROR_NOMEM;
  155. }
  156. /* collect all FDs of all PAL handles that may report read/write events */
  157. size_t nfds = 0;
  158. for (size_t i = 0; i < count; i++) {
  159. ret_events[i] = 0;
  160. PAL_HANDLE hdl = handle_array[i];
  161. if (!hdl)
  162. continue;
  163. /* collect all internal-handle FDs (only those which are readable/writable) */
  164. for (size_t j = 0; j < MAX_FDS; j++) {
  165. PAL_FLG flags = HANDLE_HDR(hdl)->flags;
  166. /* hdl might be a mutex/event/non-pollable object, simply ignore it */
  167. if (hdl->generic.fds[j] == PAL_IDX_POISON)
  168. continue;
  169. if (flags & ERROR(j))
  170. continue;
  171. int fdevents = 0;
  172. fdevents |= ((flags & RFD(j)) && (events[i] & PAL_WAIT_READ)) ? POLLIN : 0;
  173. fdevents |= ((flags & WFD(j)) && (events[i] & PAL_WAIT_WRITE)) ? POLLOUT : 0;
  174. if (fdevents) {
  175. fds[nfds].fd = hdl->generic.fds[j];
  176. fds[nfds].events = fdevents;
  177. fds[nfds].revents = 0;
  178. offsets[nfds] = i;
  179. nfds++;
  180. }
  181. }
  182. }
  183. if (!nfds) {
  184. /* did not find any waitable FDs (LibOS supplied closed/errored FDs or empty events) */
  185. ret = -PAL_ERROR_TRYAGAIN;
  186. goto out;
  187. }
  188. ret = ocall_poll(fds, nfds, timeout_us);
  189. if (IS_ERR(ret)) {
  190. switch (ERRNO(ret)) {
  191. case EINTR:
  192. case ERESTART:
  193. ret = -PAL_ERROR_INTERRUPTED;
  194. break;
  195. default:
  196. ret = unix_to_pal_error(ERRNO(ret));
  197. break;
  198. }
  199. goto out;
  200. }
  201. if (!ret) {
  202. /* timed out */
  203. ret = -PAL_ERROR_TRYAGAIN;
  204. goto out;
  205. }
  206. for (size_t i = 0; i < nfds; i++) {
  207. if (!fds[i].revents)
  208. continue;
  209. size_t j = offsets[i];
  210. if (fds[i].revents & POLLIN)
  211. ret_events[j] |= PAL_WAIT_READ;
  212. if (fds[i].revents & POLLOUT)
  213. ret_events[j] |= PAL_WAIT_WRITE;
  214. if (fds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
  215. ret_events[j] |= PAL_WAIT_ERROR;
  216. }
  217. ret = 0;
  218. out:
  219. free(fds);
  220. free(offsets);
  221. return ret;
  222. }