shim_futex.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. get_handle(hdl2);
  133. INIT_LIST_HEAD(&futex2->waiters);
  134. INIT_LIST_HEAD(&futex2->list);
  135. list_add_tail(&futex2->list, &futex_list);
  136. }
  137. unlock(futex_list_lock);
  138. int cnt;
  139. for (cnt = 0 ; cnt < val ; cnt++) {
  140. if (list_empty(&futex->waiters))
  141. break;
  142. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  143. struct futex_waiter,
  144. list);
  145. list_del(&waiter->list);
  146. thread_wakeup(waiter->thread);
  147. }
  148. lock(hdl2->lock);
  149. while (!list_empty(&futex->waiters)) {
  150. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  151. struct futex_waiter,
  152. list);
  153. list_del(&waiter->list);
  154. list_add_tail(&waiter->list, &futex2->waiters);
  155. }
  156. unlock(hdl2->lock);
  157. put_handle(hdl2);
  158. ret = cnt;
  159. break;
  160. }
  161. case FUTEX_FD:
  162. ret = set_new_fd_handle(hdl, 0, NULL);
  163. break;
  164. default:
  165. debug("unsupported futex op: 0x%x\n", op);
  166. ret = -ENOSYS;
  167. break;
  168. }
  169. unlock(hdl->lock);
  170. out:
  171. put_handle(hdl);
  172. return ret;
  173. }
  174. int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
  175. {
  176. struct shim_thread * self = get_cur_thread();
  177. assert(self);
  178. if (len != sizeof(struct robust_list_head))
  179. return -EINVAL;
  180. self->robust_list = head;
  181. return 0;
  182. }
  183. int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
  184. size_t * len)
  185. {
  186. if (!head)
  187. return -EFAULT;
  188. struct shim_thread * thread;
  189. if (pid) {
  190. thread = lookup_thread(pid);
  191. if (!thread)
  192. return -ESRCH;
  193. } else {
  194. thread = get_cur_thread();
  195. }
  196. *head = (struct robust_list_head *) thread->robust_list;
  197. *len = sizeof(struct robust_list_head);
  198. return 0;
  199. }
  200. void release_robust_list (struct robust_list_head * head)
  201. {
  202. long futex_offset = head->futex_offset;
  203. struct robust_list * robust, * prev = &head->list;
  204. create_lock_runtime(&futex_list_lock);
  205. for (robust = prev->next ; robust && robust != prev ;
  206. prev = robust, robust = robust->next) {
  207. void * futex_addr = (void *) robust + futex_offset;
  208. struct shim_futex_handle * tmp, * futex = NULL;
  209. lock(futex_list_lock);
  210. list_for_each_entry(tmp, &futex_list, list)
  211. if (tmp->uaddr == futex_addr) {
  212. futex = tmp;
  213. break;
  214. }
  215. unlock(futex_list_lock);
  216. if (!futex)
  217. continue;
  218. struct shim_handle * hdl =
  219. container_of(futex, struct shim_handle, info.futex);
  220. get_handle(hdl);
  221. lock(hdl->lock);
  222. debug("release robust list: %p\n", futex_addr);
  223. *(int *) futex_addr = 0;
  224. while (!list_empty(&futex->waiters)) {
  225. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  226. struct futex_waiter,
  227. list);
  228. list_del(&waiter->list);
  229. thread_wakeup(waiter->thread);
  230. }
  231. unlock(hdl->lock);
  232. put_handle(hdl);
  233. }
  234. }
  235. void release_clear_child_id (int * clear_child_tid)
  236. {
  237. debug("clear child tid at %p\n", clear_child_tid);
  238. *clear_child_tid = 0;
  239. create_lock_runtime(&futex_list_lock);
  240. struct shim_futex_handle * tmp, * futex = NULL;
  241. lock(futex_list_lock);
  242. list_for_each_entry(tmp, &futex_list, list)
  243. if (tmp->uaddr == (void *) clear_child_tid) {
  244. futex = tmp;
  245. break;
  246. }
  247. unlock(futex_list_lock);
  248. if (!futex)
  249. return;
  250. struct shim_handle * hdl =
  251. container_of(futex, struct shim_handle, info.futex);
  252. get_handle(hdl);
  253. lock(hdl->lock);
  254. debug("release futex at %p\n", clear_child_tid);
  255. *clear_child_tid = 0;
  256. while (!list_empty(&futex->waiters)) {
  257. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  258. struct futex_waiter,
  259. list);
  260. list_del(&waiter->list);
  261. thread_wakeup(waiter->thread);
  262. }
  263. unlock(hdl->lock);
  264. put_handle(hdl);
  265. }