shim_futex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. static LIST_HEAD(futex_list);
  37. static LOCKTYPE futex_list_lock;
  38. int shim_do_futex (unsigned int * uaddr, int op, int val, void * utime,
  39. unsigned int * uaddr2, int val3)
  40. {
  41. struct shim_futex_handle * tmp = NULL, * futex = NULL;
  42. struct shim_handle * hdl;
  43. int ret = 0;
  44. if (!uaddr || ((uintptr_t) uaddr % sizeof(unsigned int)))
  45. return -EINVAL;
  46. create_lock_runtime(&futex_list_lock);
  47. lock(futex_list_lock);
  48. list_for_each_entry(tmp, &futex_list, list)
  49. if (tmp->uaddr == uaddr) {
  50. futex = tmp;
  51. break;
  52. }
  53. if (futex) {
  54. hdl = container_of(tmp, struct shim_handle, info.futex);
  55. get_handle(hdl);
  56. } else {
  57. struct shim_vma * vma_addr = NULL;
  58. if (!(hdl = get_new_handle())) {
  59. unlock(futex_list_lock);
  60. return -ENOMEM;
  61. }
  62. hdl->type = TYPE_FUTEX;
  63. futex = &hdl->info.futex;
  64. futex->uaddr = uaddr;
  65. futex->vma = vma_addr;
  66. futex->event = DkSynchronizationEventCreate(0);
  67. if (futex->event == NULL) {
  68. ret = -PAL_ERRNO;
  69. unlock(futex_list_lock);
  70. goto out;
  71. }
  72. get_handle(hdl);
  73. INIT_LIST_HEAD(&futex->waiters);
  74. INIT_LIST_HEAD(&futex->list);
  75. list_add_tail(&futex->list, &futex_list);
  76. }
  77. unlock(futex_list_lock);
  78. lock(hdl->lock);
  79. struct futex_waiter {
  80. struct shim_thread * thread;
  81. struct list_head list;
  82. };
  83. switch (op & FUTEX_CMD_MASK) {
  84. case FUTEX_WAIT:
  85. if (*uaddr != val)
  86. break;
  87. struct futex_waiter waiter;
  88. thread_setwait(&waiter.thread, NULL);
  89. INIT_LIST_HEAD(&waiter.list);
  90. list_add_tail(&waiter.list, &futex->waiters);
  91. unlock(hdl->lock);
  92. thread_sleep();
  93. lock(hdl->lock);
  94. break;
  95. case FUTEX_WAKE: {
  96. for (int cnt = 0 ; cnt < val ; cnt++) {
  97. if (list_empty(&futex->waiters))
  98. break;
  99. struct futex_waiter * waiter = list_entry(futex->waiters.next,
  100. struct futex_waiter,
  101. list);
  102. list_del(&waiter->list);
  103. thread_wakeup(waiter->thread);
  104. }
  105. break;
  106. }
  107. case FUTEX_FD:
  108. ret = set_new_fd_handle(hdl, 0, NULL);
  109. break;
  110. default:
  111. ret = -ENOSYS;
  112. break;
  113. }
  114. unlock(hdl->lock);
  115. out:
  116. put_handle(hdl);
  117. return ret;
  118. }
  119. int shim_do_set_robust_list (struct robust_list_head * head, size_t len)
  120. {
  121. struct shim_thread * self = get_cur_thread();
  122. assert(self);
  123. if (len != sizeof(struct robust_list_head))
  124. return -EINVAL;
  125. self->robust_list = head;
  126. return 0;
  127. }
  128. int shim_do_get_robust_list (pid_t pid, struct robust_list_head ** head,
  129. size_t * len)
  130. {
  131. if (!head)
  132. return -EFAULT;
  133. struct shim_thread * thread;
  134. if (pid) {
  135. thread = lookup_thread(pid);
  136. if (!thread)
  137. return -ESRCH;
  138. } else {
  139. thread = get_cur_thread();
  140. }
  141. *head = (struct robust_list_head *) thread->robust_list;
  142. *len = sizeof(struct robust_list_head);
  143. return 0;
  144. }
  145. void release_robust_list (struct robust_list_head * head)
  146. {
  147. long futex_offset = head->futex_offset;
  148. struct robust_list * robust, * prev = &head->list;
  149. create_lock_runtime(&futex_list_lock);
  150. for (robust = prev->next ; robust && robust != prev ;
  151. prev = robust, robust = robust->next) {
  152. void * futex_addr = (void *) robust + futex_offset;
  153. struct shim_futex_handle * tmp, * futex = NULL;
  154. lock(futex_list_lock);
  155. list_for_each_entry(tmp, &futex_list, list)
  156. if (tmp->uaddr == futex_addr) {
  157. futex = tmp;
  158. break;
  159. }
  160. unlock(futex_list_lock);
  161. if (!futex)
  162. continue;
  163. DkEventSet(futex->event);
  164. }
  165. }
  166. void release_clear_child_id (int * clear_child_tid)
  167. {
  168. debug("clear child tid at %p\n", clear_child_tid);
  169. *clear_child_tid = 0;
  170. create_lock_runtime(&futex_list_lock);
  171. struct shim_futex_handle * tmp, * futex = NULL;
  172. lock(futex_list_lock);
  173. list_for_each_entry(tmp, &futex_list, list)
  174. if (tmp->uaddr == (void *) clear_child_tid) {
  175. futex = tmp;
  176. break;
  177. }
  178. unlock(futex_list_lock);
  179. if (!futex)
  180. return;
  181. debug("release futex at %p\n", clear_child_tid);
  182. DkEventSet(futex->event);
  183. }