shim_futex.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. * shim_futex.c
  17. *
  18. * Implementation of system call "futex", "set_robust_list" and
  19. * "get_robust_list".
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_table.h>
  23. #include <shim_thread.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_utils.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <linux_list.h>
  29. #include <sys/syscall.h>
  30. #include <sys/mman.h>
  31. #include <asm/prctl.h>
  32. #include <linux/futex.h>
  33. #include <errno.h>
  34. #define FUTEX_MIN_VALUE 0
  35. #define FUTEX_MAX_VALUE 255
  36. struct futex_waiter {
  37. struct shim_thread * thread;
  38. struct list_head list;
  39. };
  40. static LIST_HEAD(futex_list);
  41. static LOCKTYPE futex_list_lock;
  42. int shim_do_futex (unsigned int * uaddr, int op, int val, void * utime,
  43. unsigned int * uaddr2, int val3)
  44. {
  45. struct shim_futex_handle * tmp = NULL, * futex = NULL;
  46. struct shim_handle * hdl;
  47. int ret = 0;
  48. if (!uaddr || ((uintptr_t) uaddr % sizeof(unsigned int)))
  49. return -EINVAL;
  50. create_lock_runtime(&futex_list_lock);
  51. lock(futex_list_lock);
  52. list_for_each_entry(tmp, &futex_list, list)
  53. if (tmp->uaddr == uaddr) {
  54. futex = tmp;
  55. break;
  56. }
  57. if (futex) {
  58. hdl = container_of(futex, struct shim_handle, info.futex);
  59. get_handle(hdl);
  60. } else {
  61. if (!(hdl = get_new_handle())) {
  62. unlock(futex_list_lock);
  63. return -ENOMEM;
  64. }
  65. hdl->type = TYPE_FUTEX;
  66. futex = &hdl->info.futex;
  67. futex->uaddr = uaddr;
  68. get_handle(hdl);
  69. INIT_LIST_HEAD(&futex->waiters);
  70. INIT_LIST_HEAD(&futex->list);
  71. list_add_tail(&futex->list, &futex_list);
  72. }
  73. unlock(futex_list_lock);
  74. lock(hdl->lock);
  75. switch (op & FUTEX_CMD_MASK) {
  76. case FUTEX_WAIT:
  77. debug("FUTEX_WAIT: %p (val = %d) vs %d\n", uaddr, *uaddr, val);
  78. if (*uaddr != val)
  79. break;
  80. struct futex_waiter waiter;
  81. thread_setwait(&waiter.thread, NULL);
  82. INIT_LIST_HEAD(&waiter.list);
  83. list_add_tail(&waiter.list, &futex->waiters);
  84. unlock(hdl->lock);
  85. thread_sleep();
  86. lock(hdl->lock);
  87. break;
  88. case FUTEX_WAKE: {
  89. debug("FUTEX_WAKE: %p (val = %d)\n", uaddr, *uaddr);
  90. int cnt;
  91. for (cnt = 0 ; cnt < val ; cnt++) {
  92. if (list_empty(&futex->waiters))
  93. break;
  94. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  95. struct futex_waiter,
  96. list);
  97. debug("FUTEX_WAKE wake thread %d: %p (val = %d)\n",
  98. waiter->thread->tid, uaddr, *uaddr);
  99. list_del(&waiter->list);
  100. thread_wakeup(waiter->thread);
  101. }
  102. ret = cnt;
  103. debug("FUTEX_WAKE done: %p (val = %d)\n", uaddr, *uaddr);
  104. break;
  105. }
  106. case FUTEX_CMP_REQUEUE:
  107. if (*uaddr != val3) {
  108. ret = -EAGAIN;
  109. break;
  110. }
  111. case FUTEX_REQUEUE: {
  112. struct shim_futex_handle * futex2 = NULL;
  113. struct shim_handle * hdl2;
  114. lock(futex_list_lock);
  115. list_for_each_entry(tmp, &futex_list, list)
  116. if (tmp->uaddr == uaddr2) {
  117. futex2 = tmp;
  118. break;
  119. }
  120. if (futex2) {
  121. hdl2 = container_of(futex2, struct shim_handle, info.futex);
  122. get_handle(hdl2);
  123. } else {
  124. if (!(hdl2 = get_new_handle())) {
  125. unlock(futex_list_lock);
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. hdl2->type = TYPE_FUTEX;
  130. futex2 = &hdl2->info.futex;
  131. futex2->uaddr = uaddr2;
  132. #if 0
  133. futex2->event = DkSynchronizationEventCreate(0);
  134. if (futex2->event == NULL) {
  135. ret = -PAL_ERRNO;
  136. unlock(futex_list_lock);
  137. put_handle(hdl2);
  138. goto out;
  139. }
  140. #endif
  141. get_handle(hdl2);
  142. INIT_LIST_HEAD(&futex2->waiters);
  143. INIT_LIST_HEAD(&futex2->list);
  144. list_add_tail(&futex2->list, &futex_list);
  145. }
  146. unlock(futex_list_lock);
  147. int cnt;
  148. for (cnt = 0 ; cnt < val ; cnt++) {
  149. if (list_empty(&futex->waiters))
  150. break;
  151. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  152. struct futex_waiter,
  153. list);
  154. list_del(&waiter->list);
  155. thread_wakeup(waiter->thread);
  156. }
  157. lock(hdl2->lock);
  158. while (!list_empty(&futex->waiters)) {
  159. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  160. struct futex_waiter,
  161. list);
  162. list_del(&waiter->list);
  163. list_add_tail(&waiter->list, &futex2->waiters);
  164. }
  165. unlock(hdl2->lock);
  166. put_handle(hdl2);
  167. ret = cnt;
  168. break;
  169. }
  170. case FUTEX_FD:
  171. ret = set_new_fd_handle(hdl, 0, NULL);
  172. break;
  173. default:
  174. debug("unsupported futex op: 0x%x\n", op);
  175. ret = -ENOSYS;
  176. break;
  177. }
  178. unlock(hdl->lock);
  179. out:
  180. put_handle(hdl);
  181. return ret;
  182. }
  183. int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
  184. {
  185. struct shim_thread * self = get_cur_thread();
  186. assert(self);
  187. if (len != sizeof(struct robust_list_head))
  188. return -EINVAL;
  189. self->robust_list = head;
  190. return 0;
  191. }
  192. int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
  193. size_t * len)
  194. {
  195. if (!head)
  196. return -EFAULT;
  197. struct shim_thread * thread;
  198. if (pid) {
  199. thread = lookup_thread(pid);
  200. if (!thread)
  201. return -ESRCH;
  202. } else {
  203. thread = get_cur_thread();
  204. }
  205. *head = (struct robust_list_head *) thread->robust_list;
  206. *len = sizeof(struct robust_list_head);
  207. return 0;
  208. }
  209. void release_robust_list (struct robust_list_head * head)
  210. {
  211. long futex_offset = head->futex_offset;
  212. struct robust_list * robust, * prev = &head->list;
  213. create_lock_runtime(&futex_list_lock);
  214. for (robust = prev->next ; robust && robust != prev ;
  215. prev = robust, robust = robust->next) {
  216. void * futex_addr = (void *) robust + futex_offset;
  217. struct shim_futex_handle * tmp, * futex = NULL;
  218. lock(futex_list_lock);
  219. list_for_each_entry(tmp, &futex_list, list)
  220. if (tmp->uaddr == futex_addr) {
  221. futex = tmp;
  222. break;
  223. }
  224. unlock(futex_list_lock);
  225. if (!futex)
  226. continue;
  227. struct shim_handle * hdl =
  228. container_of(futex, struct shim_handle, info.futex);
  229. get_handle(hdl);
  230. lock(hdl->lock);
  231. debug("release robust list: %p\n", futex_addr);
  232. *(int *) futex_addr = 0;
  233. while (!list_empty(&futex->waiters)) {
  234. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  235. struct futex_waiter,
  236. list);
  237. list_del(&waiter->list);
  238. thread_wakeup(waiter->thread);
  239. }
  240. unlock(hdl->lock);
  241. put_handle(hdl);
  242. }
  243. }
  244. void release_clear_child_id (int * clear_child_tid)
  245. {
  246. debug("clear child tid at %p\n", clear_child_tid);
  247. *clear_child_tid = 0;
  248. create_lock_runtime(&futex_list_lock);
  249. struct shim_futex_handle * tmp, * futex = NULL;
  250. lock(futex_list_lock);
  251. list_for_each_entry(tmp, &futex_list, list)
  252. if (tmp->uaddr == (void *) clear_child_tid) {
  253. futex = tmp;
  254. break;
  255. }
  256. unlock(futex_list_lock);
  257. if (!futex)
  258. return;
  259. struct shim_handle * hdl =
  260. container_of(futex, struct shim_handle, info.futex);
  261. get_handle(hdl);
  262. lock(hdl->lock);
  263. debug("release futex at %p\n", clear_child_tid);
  264. *clear_child_tid = 0;
  265. while (!list_empty(&futex->waiters)) {
  266. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  267. struct futex_waiter,
  268. list);
  269. list_del(&waiter->list);
  270. thread_wakeup(waiter->thread);
  271. }
  272. unlock(hdl->lock);
  273. put_handle(hdl);
  274. }