shim_epoll.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * shim_epoll.c
  15. *
  16. * Implementation of system call "epoll_create", "epoll_create1", "epoll_ctl"
  17. * and "epoll_wait".
  18. */
  19. #include <errno.h>
  20. #include <linux/eventpoll.h>
  21. #include <pal.h>
  22. #include <pal_error.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_fs.h>
  25. #include <shim_handle.h>
  26. #include <shim_internal.h>
  27. #include <shim_table.h>
  28. #include <shim_thread.h>
  29. /* Avoid duplicated definitions */
  30. #ifndef EPOLLIN
  31. #define EPOLLIN 0x001
  32. #define EPOLLPRI 0x002
  33. #define EPOLLOUT 0x004
  34. #define EPOLLRDNORM 0x040
  35. #define EPOLLRDBAND 0x080
  36. #define EPOLLWRNORM 0x100
  37. #define EPOLLERBAND 0x200
  38. #define EPOLLERR 0x008
  39. #define EPOLLHUP 0x010
  40. #define EPOLLRDHUP 0x2000
  41. #endif
  42. #define MAX_EPOLL_FDS 1024
  43. struct shim_mount epoll_builtin_fs;
  44. /* shim_epoll_fds are linked as a list (by the list field),
  45. * hanging off of a shim_epoll_handle (by the fds field) */
  46. struct shim_epoll_fd {
  47. FDTYPE fd;
  48. unsigned int events;
  49. __u64 data;
  50. unsigned int revents;
  51. struct shim_handle* handle;
  52. struct shim_handle* epoll;
  53. PAL_HANDLE pal_handle;
  54. LIST_TYPE(shim_epoll_fd) list;
  55. LIST_TYPE(shim_epoll_fd) back;
  56. };
  57. int shim_do_epoll_create1(int flags) {
  58. if ((flags & ~EPOLL_CLOEXEC))
  59. return -EINVAL;
  60. struct shim_handle* hdl = get_new_handle();
  61. if (!hdl)
  62. return -ENOMEM;
  63. struct shim_epoll_handle* epoll = &hdl->info.epoll;
  64. hdl->type = TYPE_EPOLL;
  65. set_handle_fs(hdl, &epoll_builtin_fs);
  66. epoll->maxfds = MAX_EPOLL_FDS;
  67. epoll->nfds = 0;
  68. epoll->pal_fds = malloc(sizeof(FDTYPE) * MAX_EPOLL_FDS);
  69. epoll->pal_handles = malloc(sizeof(PAL_HANDLE) * MAX_EPOLL_FDS);
  70. create_event(&epoll->event);
  71. INIT_LISTP(&epoll->fds);
  72. int vfd = set_new_fd_handle(hdl, (flags & EPOLL_CLOEXEC) ? FD_CLOEXEC : 0, NULL);
  73. put_handle(hdl);
  74. return vfd;
  75. }
  76. /* the 'size' argument of epoll_create is not used */
  77. int shim_do_epoll_create(int size) {
  78. if (size < 0)
  79. return -EINVAL;
  80. return shim_do_epoll_create1(0);
  81. }
  82. static void update_epoll(struct shim_epoll_handle* epoll) {
  83. struct shim_epoll_fd* tmp;
  84. int npals = 0;
  85. epoll->nread = 0;
  86. LISTP_FOR_EACH_ENTRY(tmp, &epoll->fds, list) {
  87. if (!tmp->pal_handle)
  88. continue;
  89. debug("found handle %p (pal handle %p) from epoll handle %p\n", tmp->handle,
  90. tmp->pal_handle, epoll);
  91. epoll->pal_fds[npals] = tmp->fd;
  92. epoll->pal_handles[npals] = tmp->pal_handle;
  93. npals++;
  94. if (tmp->handle->acc_mode & MAY_READ)
  95. epoll->nread++;
  96. }
  97. epoll->npals = npals;
  98. if (epoll->nwaiters)
  99. set_event(&epoll->event, epoll->nwaiters);
  100. }
  101. int delete_from_epoll_handles(struct shim_handle* handle) {
  102. while (1) {
  103. lock(&handle->lock);
  104. if (LISTP_EMPTY(&handle->epolls)) {
  105. unlock(&handle->lock);
  106. break;
  107. }
  108. struct shim_epoll_fd* epoll_fd =
  109. LISTP_FIRST_ENTRY(&handle->epolls, struct shim_epoll_fd, back);
  110. LISTP_DEL(epoll_fd, &handle->epolls, back);
  111. unlock(&handle->lock);
  112. put_handle(handle);
  113. struct shim_handle* epoll_hdl = epoll_fd->epoll;
  114. struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
  115. debug("delete handle %p from epoll handle %p\n", handle, &epoll_hdl->info.epoll);
  116. lock(&epoll_hdl->lock);
  117. LISTP_DEL(epoll_fd, &epoll->fds, list);
  118. free(epoll_fd);
  119. epoll_hdl->info.epoll.nfds--;
  120. update_epoll(&epoll_hdl->info.epoll);
  121. unlock(&epoll_hdl->lock);
  122. put_handle(epoll_hdl);
  123. }
  124. return 0;
  125. }
  126. int shim_do_epoll_ctl(int epfd, int op, int fd, struct __kernel_epoll_event* event) {
  127. struct shim_thread* cur = get_cur_thread();
  128. int ret = 0;
  129. struct shim_handle* epoll_hdl = get_fd_handle(epfd, NULL, cur->handle_map);
  130. if (!epoll_hdl)
  131. return -EBADF;
  132. if (epoll_hdl->type != TYPE_EPOLL) {
  133. put_handle(epoll_hdl);
  134. return -EINVAL;
  135. }
  136. struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
  137. struct shim_epoll_fd* epoll_fd;
  138. lock(&epoll_hdl->lock);
  139. switch (op) {
  140. case EPOLL_CTL_ADD: {
  141. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  142. if (epoll_fd->fd == fd) {
  143. ret = -EEXIST;
  144. goto out;
  145. }
  146. }
  147. struct shim_handle* hdl = get_fd_handle(fd, NULL, cur->handle_map);
  148. if (!hdl) {
  149. ret = -EBADF;
  150. goto out;
  151. }
  152. if ((hdl->type != TYPE_PIPE && hdl->type != TYPE_SOCK) || !hdl->pal_handle) {
  153. ret = -EPERM;
  154. put_handle(hdl);
  155. goto out;
  156. }
  157. if (epoll->nfds == MAX_EPOLL_FDS) {
  158. ret = -ENOSPC;
  159. put_handle(hdl);
  160. goto out;
  161. }
  162. debug("add handle %p to epoll handle %p\n", hdl, epoll);
  163. epoll_fd = malloc(sizeof(struct shim_epoll_fd));
  164. epoll_fd->fd = fd;
  165. epoll_fd->events = event->events;
  166. epoll_fd->data = event->data;
  167. epoll_fd->revents = 0;
  168. epoll_fd->handle = hdl;
  169. epoll_fd->epoll = epoll_hdl;
  170. epoll_fd->pal_handle = hdl->pal_handle;
  171. /* Register the epoll handle */
  172. get_handle(epoll_hdl);
  173. lock(&hdl->lock);
  174. INIT_LIST_HEAD(epoll_fd, back);
  175. LISTP_ADD_TAIL(epoll_fd, &hdl->epolls, back);
  176. unlock(&hdl->lock);
  177. INIT_LIST_HEAD(epoll_fd, list);
  178. LISTP_ADD_TAIL(epoll_fd, &epoll->fds, list);
  179. epoll->nfds++;
  180. goto update;
  181. }
  182. case EPOLL_CTL_MOD: {
  183. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  184. if (epoll_fd->fd == fd) {
  185. epoll_fd->events = event->events;
  186. epoll_fd->data = event->data;
  187. goto update;
  188. }
  189. }
  190. ret = -ENOENT;
  191. goto out;
  192. }
  193. case EPOLL_CTL_DEL: {
  194. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  195. if (epoll_fd->fd == fd) {
  196. struct shim_handle* hdl = epoll_fd->handle;
  197. /* Unregister the epoll handle */
  198. lock(&hdl->lock);
  199. LISTP_DEL(epoll_fd, &hdl->epolls, back);
  200. unlock(&hdl->lock);
  201. debug("delete handle %p from epoll handle %p\n", hdl, epoll);
  202. put_handle(epoll_hdl);
  203. put_handle(hdl);
  204. LISTP_DEL(epoll_fd, &epoll->fds, list);
  205. epoll->nfds--;
  206. free(epoll_fd);
  207. goto update;
  208. }
  209. }
  210. ret = -ENOENT;
  211. goto out;
  212. }
  213. default:
  214. ret = -ENOSYS;
  215. goto out;
  216. }
  217. update:
  218. update_epoll(epoll);
  219. out:
  220. unlock(&epoll_hdl->lock);
  221. put_handle(epoll_hdl);
  222. return ret;
  223. }
  224. int shim_do_epoll_wait(int epfd, struct __kernel_epoll_event* events, int maxevents,
  225. int timeout_ms) {
  226. int ret = 0;
  227. struct shim_handle* epoll_hdl = get_fd_handle(epfd, NULL, NULL);
  228. if (!epoll_hdl)
  229. return -EBADF;
  230. if (epoll_hdl->type != TYPE_EPOLL) {
  231. put_handle(epoll_hdl);
  232. return -EINVAL;
  233. }
  234. struct shim_epoll_handle* epoll = &epoll_hdl->info.epoll;
  235. struct shim_epoll_fd* epoll_fd;
  236. int nevents = 0;
  237. int npals, nread;
  238. bool need_update = false;
  239. lock(&epoll_hdl->lock);
  240. retry:
  241. if (!(npals = epoll->npals))
  242. goto reply;
  243. PAL_HANDLE* pal_handles = __alloca(sizeof(PAL_HANDLE) * (npals + 1));
  244. FDTYPE* fds = __alloca(sizeof(FDTYPE) * npals);
  245. memcpy(fds, epoll->pal_fds, sizeof(FDTYPE) * npals);
  246. memcpy(pal_handles, epoll->pal_handles, sizeof(PAL_HANDLE) * npals);
  247. pal_handles[npals] = epoll->event.event;
  248. if ((nread = epoll->nread))
  249. epoll->nwaiters++;
  250. unlock(&epoll_hdl->lock);
  251. PAL_NUM pal_timeout = timeout_ms == -1 ? NO_TIMEOUT : (PAL_NUM)timeout_ms * 1000;
  252. PAL_HANDLE polled =
  253. DkObjectsWaitAny(nread ? npals + 1 : npals, pal_handles, nread ? pal_timeout : 0);
  254. lock(&epoll_hdl->lock);
  255. if (nread)
  256. epoll->nwaiters--;
  257. if (!polled)
  258. goto reply;
  259. if (polled == epoll->event.event) {
  260. wait_event(&epoll->event);
  261. goto retry;
  262. }
  263. PAL_STREAM_ATTR attr;
  264. if (!DkStreamAttributesQueryByHandle(polled, &attr))
  265. goto reply;
  266. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  267. if (polled == epoll_fd->pal_handle) {
  268. debug("epoll: fd %d (handle %p) polled\n", epoll_fd->fd, epoll_fd->handle);
  269. if (attr.disconnected) {
  270. epoll_fd->revents |= EPOLLERR | EPOLLHUP | EPOLLRDHUP;
  271. epoll_fd->pal_handle = NULL;
  272. need_update = true;
  273. }
  274. if (attr.readable)
  275. epoll_fd->revents |= EPOLLIN;
  276. if (attr.writable)
  277. epoll_fd->revents |= EPOLLOUT;
  278. break;
  279. }
  280. }
  281. reply:
  282. LISTP_FOR_EACH_ENTRY(epoll_fd, &epoll->fds, list) {
  283. if (nevents == maxevents)
  284. break;
  285. if ((epoll_fd->events | EPOLLERR | EPOLLHUP) & epoll_fd->revents) {
  286. events[nevents].events = (epoll_fd->events | EPOLLERR | EPOLLHUP) & epoll_fd->revents;
  287. events[nevents].data = epoll_fd->data;
  288. nevents++;
  289. epoll_fd->revents &= ~epoll_fd->events;
  290. }
  291. }
  292. if (need_update)
  293. update_epoll(epoll);
  294. unlock(&epoll_hdl->lock);
  295. ret = nevents;
  296. put_handle(epoll_hdl);
  297. return ret;
  298. }
  299. int shim_do_epoll_pwait(int epfd, struct __kernel_epoll_event* events, int maxevents,
  300. int timeout_ms, const __sigset_t* sigmask, size_t sigsetsize) {
  301. __UNUSED(sigmask);
  302. __UNUSED(sigsetsize);
  303. int ret = shim_do_epoll_wait(epfd, events, maxevents, timeout_ms);
  304. return ret;
  305. }
  306. static int epoll_close(struct shim_handle* hdl) {
  307. __UNUSED(hdl);
  308. return 0;
  309. }
  310. struct shim_fs_ops epoll_fs_ops = {
  311. .close = &epoll_close,
  312. };
  313. struct shim_mount epoll_builtin_fs = {
  314. .type = "epoll",
  315. .fs_ops = &epoll_fs_ops,
  316. };
  317. BEGIN_CP_FUNC(epoll_fd) {
  318. assert(size == sizeof(LISTP_TYPE(shim_epoll_fd)));
  319. LISTP_TYPE(shim_epoll_fd)* old_list = (LISTP_TYPE(shim_epoll_fd)*)obj;
  320. LISTP_TYPE(shim_epoll_fd)* new_list = (LISTP_TYPE(shim_epoll_fd)*)objp;
  321. struct shim_epoll_fd* epoll_fd;
  322. debug("checkpoint epoll: %p -> %p (base = 0x%08lx)\n", old_list, new_list, base);
  323. INIT_LISTP(new_list);
  324. LISTP_FOR_EACH_ENTRY(epoll_fd, old_list, list) {
  325. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_epoll_fd));
  326. struct shim_epoll_fd* new_epoll_fd = (struct shim_epoll_fd*)(base + off);
  327. new_epoll_fd->fd = epoll_fd->fd;
  328. new_epoll_fd->events = epoll_fd->events;
  329. new_epoll_fd->data = epoll_fd->data;
  330. new_epoll_fd->revents = epoll_fd->revents;
  331. new_epoll_fd->pal_handle = NULL;
  332. LISTP_ADD(new_epoll_fd, new_list, list);
  333. DO_CP(handle, epoll_fd->handle, &new_epoll_fd->handle);
  334. }
  335. ADD_CP_FUNC_ENTRY((ptr_t)objp - base);
  336. }
  337. END_CP_FUNC(epoll_fd)
  338. BEGIN_RS_FUNC(epoll_fd) {
  339. __UNUSED(offset);
  340. LISTP_TYPE(shim_epoll_fd)* list = (void*)(base + GET_CP_FUNC_ENTRY());
  341. struct shim_epoll_fd* epoll_fd;
  342. CP_REBASE(*list);
  343. LISTP_FOR_EACH_ENTRY(epoll_fd, list, list) {
  344. CP_REBASE(epoll_fd->handle);
  345. CP_REBASE(epoll_fd->back);
  346. epoll_fd->pal_handle = epoll_fd->handle->pal_handle;
  347. CP_REBASE(epoll_fd->list);
  348. DEBUG_RS("fd=%d,path=%s,type=%s,uri=%s", epoll_fd->fd, qstrgetstr(&epoll_fd->handle->path),
  349. epoll_fd->handle->fs_type, qstrgetstr(&epoll_fd->handle->uri));
  350. }
  351. }
  352. END_RS_FUNC(epoll_fd)