db_object.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_object.c
  17. *
  18. * This file contains APIs for closing or polling PAL handles.
  19. */
  20. #include "pal_defs.h"
  21. #include "pal_linux_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_linux.h"
  25. #include "pal_error.h"
  26. #include "pal_debug.h"
  27. #include "api.h"
  28. #include <linux/time.h>
  29. #include <linux/poll.h>
  30. #include <linux/wait.h>
  31. #include <atomic.h>
  32. #include <cmpxchg.h>
  33. #include <asm/errno.h>
  34. #define DEFAULT_QUANTUM 500
  35. /* internally to wait for one object. Also used as a shortcut to wait
  36. * on events and semaphores.
  37. *
  38. * Returns 0 on success, negative value on failure (e.g., -PAL_ERROR_TRYAGAIN)
  39. */
  40. static int _DkObjectWaitOne (PAL_HANDLE handle, uint64_t timeout)
  41. {
  42. /* only for all these handle which has a file descriptor, or
  43. a eventfd. events and semaphores will skip this part */
  44. if (HANDLE_HDR(handle)->flags & HAS_FDS) {
  45. struct timespec timeout_ts;
  46. if (timeout >= 0) {
  47. long sec = (unsigned long) timeout / 1000000;
  48. long microsec = (unsigned long) timeout - (sec * 1000000);
  49. timeout_ts.tv_sec = sec;
  50. timeout_ts.tv_nsec = microsec * 1000;
  51. }
  52. struct pollfd fds[MAX_FDS];
  53. int off[MAX_FDS];
  54. int nfds = 0;
  55. for (int i = 0 ; i < MAX_FDS ; i++) {
  56. int events = 0;
  57. if ((HANDLE_HDR(handle)->flags & RFD(i)) &&
  58. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  59. events |= POLLIN;
  60. if ((HANDLE_HDR(handle)->flags & WFD(i)) &&
  61. !(HANDLE_HDR(handle)->flags & WRITEABLE(i)) &&
  62. !(HANDLE_HDR(handle)->flags & ERROR(i)))
  63. events |= POLLOUT;
  64. if (events) {
  65. fds[nfds].fd = HANDLE_HDR(handle)->fds[i];
  66. fds[nfds].events = events|POLLHUP|POLLERR;
  67. fds[nfds].revents = 0;
  68. off[nfds] = i;
  69. nfds++;
  70. }
  71. }
  72. if (!nfds)
  73. return -PAL_ERROR_TRYAGAIN;
  74. int ret = INLINE_SYSCALL(ppoll, 5, &fds, nfds,
  75. timeout >= 0 ? &timeout_ts : NULL,
  76. NULL, 0);
  77. if (IS_ERR(ret))
  78. switch (ERRNO(ret)) {
  79. case EINTR:
  80. case ERESTART:
  81. return -PAL_ERROR_INTERRUPTED;
  82. default:
  83. return unix_to_pal_error(ERRNO(ret));
  84. }
  85. if (!ret)
  86. return -PAL_ERROR_TRYAGAIN;
  87. for (int i = 0 ; i < nfds ; i++) {
  88. if (!fds[i].revents)
  89. continue;
  90. if (fds[i].revents & POLLOUT)
  91. HANDLE_HDR(handle)->flags |= WRITEABLE(off[i]);
  92. if (fds[i].revents & (POLLHUP|POLLERR))
  93. HANDLE_HDR(handle)->flags |= ERROR(off[i]);
  94. }
  95. return 0;
  96. }
  97. const struct handle_ops * ops = HANDLE_OPS(handle);
  98. if (!ops->wait)
  99. return -PAL_ERROR_NOTSUPPORT;
  100. return ops->wait(handle, timeout);
  101. }
  102. /* _DkObjectsWaitAny for internal use. The function wait for any of the handle
  103. in the handle array. timeout can be set for the wait. */
  104. int _DkObjectsWaitAny (int count, PAL_HANDLE * handleArray, uint64_t timeout,
  105. PAL_HANDLE * polled)
  106. {
  107. if (count <= 0)
  108. return 0;
  109. if (count == 1) {
  110. int rv = _DkObjectWaitOne(handleArray[0], timeout);
  111. if (rv == 0)
  112. *polled = handleArray[0];
  113. return rv;
  114. }
  115. int i, j, ret, maxfds = 0, nfds = 0;
  116. /* we are not gonna to allow any polling on muliple synchronous
  117. objects, doing this is simply violating the division of
  118. labor between PAL and library OS */
  119. for (i = 0 ; i < count ; i++) {
  120. PAL_HANDLE hdl = handleArray[i];
  121. if (!hdl)
  122. continue;
  123. if (!(HANDLE_HDR(hdl)->flags & HAS_FDS))
  124. return -PAL_ERROR_NOTSUPPORT;
  125. /* eliminate repeated entries */
  126. for (j = 0 ; j < i ; j++)
  127. if (hdl == handleArray[j])
  128. break;
  129. if (j == i) {
  130. for (j = 0 ; j < MAX_FDS ; j++)
  131. if (HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j)))
  132. maxfds++;
  133. }
  134. }
  135. struct pollfd * fds = __alloca(sizeof(struct pollfd) * maxfds);
  136. PAL_HANDLE * hdls = __alloca(sizeof(PAL_HANDLE) * maxfds);
  137. for (i = 0 ; i < count ; i++) {
  138. PAL_HANDLE hdl = handleArray[i];
  139. if (!hdl)
  140. continue;
  141. for (j = 0 ; j < i ; j++)
  142. if (hdl == handleArray[j])
  143. break;
  144. if (j < i)
  145. continue;
  146. for (j = 0 ; j < MAX_FDS ; j++) {
  147. int events = 0;
  148. if ((HANDLE_HDR(hdl)->flags & RFD(j)) &&
  149. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  150. events |= POLLIN;
  151. if ((HANDLE_HDR(hdl)->flags & WFD(j)) &&
  152. !(HANDLE_HDR(hdl)->flags & WRITEABLE(j)) &&
  153. !(HANDLE_HDR(hdl)->flags & ERROR(j)))
  154. events |= POLLOUT;
  155. if (events && HANDLE_HDR(hdl)->fds[j] != PAL_IDX_POISON) {
  156. fds[nfds].fd = HANDLE_HDR(hdl)->fds[j];
  157. fds[nfds].events = events|POLLHUP|POLLERR;
  158. fds[nfds].revents = 0;
  159. hdls[nfds] = hdl;
  160. nfds++;
  161. }
  162. }
  163. }
  164. if (!nfds)
  165. return -PAL_ERROR_TRYAGAIN;
  166. struct timespec timeout_ts;
  167. if (timeout >= 0) {
  168. long sec = (unsigned long) timeout / 1000000;
  169. long microsec = (unsigned long) timeout - (sec * 1000000);
  170. timeout_ts.tv_sec = sec;
  171. timeout_ts.tv_nsec = microsec * 1000;
  172. }
  173. ret = INLINE_SYSCALL(ppoll, 5, fds, nfds,
  174. timeout >= 0 ? &timeout_ts : NULL,
  175. NULL, 0);
  176. if (IS_ERR(ret))
  177. switch (ERRNO(ret)) {
  178. case EINTR:
  179. case ERESTART:
  180. return -PAL_ERROR_INTERRUPTED;
  181. default:
  182. return unix_to_pal_error(ERRNO(ret));
  183. }
  184. if (!ret)
  185. return -PAL_ERROR_TRYAGAIN;
  186. PAL_HANDLE polled_hdl = NULL;
  187. for (i = 0 ; i < nfds ; i++) {
  188. if (!fds[i].revents)
  189. continue;
  190. PAL_HANDLE hdl = hdls[i];
  191. if (polled_hdl) {
  192. if (hdl != polled_hdl)
  193. continue;
  194. } else {
  195. polled_hdl = hdl;
  196. }
  197. for (j = 0 ; j < MAX_FDS ; j++)
  198. if ((HANDLE_HDR(hdl)->flags & (RFD(j)|WFD(j))) &&
  199. HANDLE_HDR(hdl)->fds[j] == fds[i].fd)
  200. break;
  201. if (j == MAX_FDS)
  202. continue;
  203. if (fds[i].revents & POLLOUT)
  204. HANDLE_HDR(hdl)->flags |= WRITEABLE(j);
  205. if (fds[i].revents & (POLLHUP|POLLERR))
  206. HANDLE_HDR(hdl)->flags |= ERROR(j);
  207. }
  208. *polled = polled_hdl;
  209. return polled_hdl ? 0 : -PAL_ERROR_TRYAGAIN;
  210. }