shim_futex.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /* Copyright (C) 2014 Stony Brook University
  2. Copyright (C) 2019 Invisible Things Lab
  3. This file is part of Graphene Library OS.
  4. Graphene Library OS is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License
  6. as published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. Graphene Library OS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /*
  15. * "The futexes are also cursed."
  16. * "But they come in a choice of three flavours!"
  17. * ~ the Linux kernel source
  18. */
  19. /*
  20. * Current implementation is limited to one process i.e. threads calling futex syscall on the same
  21. * futex word must reside in the same process.
  22. * As a result we can distinguish futexes by their virtual address.
  23. */
  24. #include <linux/futex.h>
  25. #include <linux/time.h>
  26. #include <stdint.h>
  27. #include "api.h"
  28. #include "assert.h"
  29. #include "list.h"
  30. #include "pal.h"
  31. #include "shim_internal.h"
  32. #include "shim_thread.h"
  33. #include "shim_types.h"
  34. #include "spinlock.h"
  35. struct shim_futex;
  36. struct futex_waiter;
  37. DEFINE_LIST(futex_waiter);
  38. DEFINE_LISTP(futex_waiter);
  39. struct futex_waiter {
  40. struct shim_thread* thread;
  41. uint32_t bitset;
  42. LIST_TYPE(futex_waiter) list;
  43. /* futex field is guarded by g_futex_list_lock, do not use it without taking that lock first.
  44. * This is needed to ensure that a waiter knows what futex they were sleeping on, after they
  45. * wake-up (because they could have been requeued to another futex).*/
  46. struct shim_futex* futex;
  47. };
  48. DEFINE_LIST(shim_futex);
  49. DEFINE_LISTP(shim_futex);
  50. struct shim_futex {
  51. uint32_t* uaddr;
  52. LISTP_TYPE(futex_waiter) waiters;
  53. LIST_TYPE(shim_futex) list;
  54. /* This lock guards every access to *uaddr (futex word value) and waiters (above).
  55. * Always take g_futex_list_lock before taking this lock. */
  56. spinlock_t lock;
  57. REFTYPE _ref_count;
  58. };
  59. static LISTP_TYPE(shim_futex) g_futex_list = LISTP_INIT;
  60. static spinlock_t g_futex_list_lock = INIT_SPINLOCK_UNLOCKED;
  61. static void get_futex(struct shim_futex* futex) {
  62. REF_INC(futex->_ref_count);
  63. }
  64. static void put_futex(struct shim_futex* futex) {
  65. if (!REF_DEC(futex->_ref_count)) {
  66. free(futex);
  67. }
  68. }
  69. /* Since we distinguish futexes by their virtual address, we can as well create a total ordering
  70. * based on it. */
  71. static int cmp_futexes(struct shim_futex* futex1, struct shim_futex* futex2) {
  72. uintptr_t f1 = (uintptr_t)futex1->uaddr;
  73. uintptr_t f2 = (uintptr_t)futex2->uaddr;
  74. if (f1 < f2) {
  75. return -1;
  76. } else if (f1 == f2) {
  77. return 0;
  78. } else {
  79. return 1;
  80. }
  81. }
  82. /*
  83. * Locks two futexes in ascending order (defined by cmp_futexes).
  84. * If a futex is NULL, it is just skipped.
  85. */
  86. static void lock_two_futexes(struct shim_futex* futex1, struct shim_futex* futex2) {
  87. if (!futex1 && !futex2) {
  88. return;
  89. } else if (futex1 && !futex2) {
  90. spinlock_lock(&futex1->lock);
  91. return;
  92. } else if (!futex1 && futex2) {
  93. spinlock_lock(&futex2->lock);
  94. return;
  95. }
  96. /* Both are not NULL. */
  97. /* To avoid deadlocks we always take the locks in ascending order of futexes.
  98. * If both futexes are equal, just take one lock. */
  99. int cmp = cmp_futexes(futex1, futex2);
  100. if (cmp < 0) {
  101. spinlock_lock(&futex1->lock);
  102. spinlock_lock(&futex2->lock);
  103. } else if (cmp == 0) {
  104. spinlock_lock(&futex1->lock);
  105. } else {
  106. spinlock_lock(&futex2->lock);
  107. spinlock_lock(&futex1->lock);
  108. }
  109. }
  110. static void unlock_two_futexes(struct shim_futex* futex1, struct shim_futex* futex2) {
  111. if (!futex1 && !futex2) {
  112. return;
  113. } else if (futex1 && !futex2) {
  114. spinlock_unlock(&futex1->lock);
  115. return;
  116. } else if (!futex1 && futex2) {
  117. spinlock_unlock(&futex2->lock);
  118. return;
  119. }
  120. /* Both are not NULL. */
  121. /* For unlocking order does not matter. */
  122. int cmp = cmp_futexes(futex1, futex2);
  123. if (cmp) {
  124. spinlock_unlock(&futex1->lock);
  125. spinlock_unlock(&futex2->lock);
  126. } else {
  127. spinlock_unlock(&futex1->lock);
  128. }
  129. }
  130. /*
  131. * Adds `futex` to `g_futex_list`.
  132. *
  133. * `g_futex_list_lock` should be held while calling this function and you must ensure that nobody
  134. * is using `futex` (e.g. you have just created it).
  135. */
  136. static void enqueue_futex(struct shim_futex* futex) {
  137. assert(spinlock_is_locked(&g_futex_list_lock));
  138. get_futex(futex);
  139. LISTP_ADD_TAIL(futex, &g_futex_list, list);
  140. }
  141. /*
  142. * Checks whether `futex` has no waiters and is on `g_futex_list`.
  143. *
  144. * This requires only `futex->lock` to be held.
  145. */
  146. static bool check_dequeue_futex(struct shim_futex* futex) {
  147. assert(spinlock_is_locked(&futex->lock));
  148. return LISTP_EMPTY(&futex->waiters) && !LIST_EMPTY(futex, list);
  149. }
  150. static void _maybe_dequeue_futex(struct shim_futex* futex) {
  151. assert(spinlock_is_locked(&futex->lock));
  152. assert(spinlock_is_locked(&g_futex_list_lock));
  153. if (check_dequeue_futex(futex)) {
  154. LISTP_DEL_INIT(futex, &g_futex_list, list);
  155. /* We still hold this futex reference (in the caller), so this won't call free. */
  156. put_futex(futex);
  157. }
  158. }
  159. /*
  160. * If `futex` has no waiters and is on `g_futex_list`, takes it off that list.
  161. *
  162. * Neither `g_futex_list_lock` nor `futex->lock` should be held while calling this,
  163. * it acquires these locks itself.
  164. */
  165. static void maybe_dequeue_futex(struct shim_futex* futex) {
  166. spinlock_lock(&g_futex_list_lock);
  167. spinlock_lock(&futex->lock);
  168. _maybe_dequeue_futex(futex);
  169. spinlock_unlock(&futex->lock);
  170. spinlock_unlock(&g_futex_list_lock);
  171. }
  172. /*
  173. * Same as `maybe_dequeue_futex`, but works for two futexes, any of which might be NULL.
  174. */
  175. static void maybe_dequeue_two_futexes(struct shim_futex* futex1, struct shim_futex* futex2) {
  176. spinlock_lock(&g_futex_list_lock);
  177. lock_two_futexes(futex1, futex2);
  178. if (futex1) {
  179. _maybe_dequeue_futex(futex1);
  180. }
  181. if (futex2) {
  182. _maybe_dequeue_futex(futex2);
  183. }
  184. unlock_two_futexes(futex1, futex2);
  185. spinlock_unlock(&g_futex_list_lock);
  186. }
  187. /*
  188. * Adds `waiter` to `futex` waiters list.
  189. * You need to make sure that this futex is still on `g_futex_list`, but in most cases it follows
  190. * from the program control flow.
  191. *
  192. * Increases refcount of current thread by 1 (in thread_setwait)
  193. * and of `futex` by 1.
  194. * `futex->lock` needs to be held.
  195. */
  196. static void add_futex_waiter(struct futex_waiter* waiter,
  197. struct shim_futex* futex,
  198. uint32_t bitset) {
  199. assert(spinlock_is_locked(&futex->lock));
  200. thread_setwait(&waiter->thread, NULL);
  201. INIT_LIST_HEAD(waiter, list);
  202. waiter->bitset = bitset;
  203. get_futex(futex);
  204. waiter->futex = futex;
  205. LISTP_ADD_TAIL(waiter, &futex->waiters, list);
  206. }
  207. /*
  208. * Ownership of the `waiter->thread` is passed to the caller; we do not change its refcount because
  209. * we take it of `futex->waiters` list (-1) and give it to caller (+1).
  210. *
  211. * `futex->lock` needs to be held.
  212. */
  213. static struct shim_thread* remove_futex_waiter(struct futex_waiter* waiter,
  214. struct shim_futex* futex) {
  215. assert(spinlock_is_locked(&futex->lock));
  216. LISTP_DEL_INIT(waiter, &futex->waiters, list);
  217. return waiter->thread;
  218. }
  219. /*
  220. * Moves waiter from `futex1` to `futex2`.
  221. * As in `add_futex_waiter`, `futex2` needs to be on `g_futex_list`.
  222. *
  223. * `futex1->lock` and `futex2->lock` need to be held.
  224. */
  225. static void move_futex_waiter(struct futex_waiter* waiter,
  226. struct shim_futex* futex1,
  227. struct shim_futex* futex2) {
  228. assert(spinlock_is_locked(&futex1->lock));
  229. assert(spinlock_is_locked(&futex2->lock));
  230. LISTP_DEL_INIT(waiter, &futex1->waiters, list);
  231. get_futex(futex2);
  232. put_futex(waiter->futex);
  233. waiter->futex = futex2;
  234. LISTP_ADD_TAIL(waiter, &futex2->waiters, list);
  235. }
  236. /*
  237. * Creates a new futex.
  238. * Sets the new futex refcount to 1.
  239. */
  240. static struct shim_futex* create_new_futex(uint32_t* uaddr) {
  241. struct shim_futex* futex;
  242. futex = calloc(1, sizeof(*futex));
  243. if (!futex) {
  244. return NULL;
  245. }
  246. REF_SET(futex->_ref_count, 1);
  247. futex->uaddr = uaddr;
  248. INIT_LISTP(&futex->waiters);
  249. INIT_LIST_HEAD(futex, list);
  250. spinlock_init(&futex->lock);
  251. return futex;
  252. }
  253. /*
  254. * Finds a futex in `g_futex_list`.
  255. * Must be called with `g_futex_list_lock` held.
  256. * Increases refcount of futex by 1.
  257. */
  258. static struct shim_futex* find_futex(uint32_t* uaddr) {
  259. assert(spinlock_is_locked(&g_futex_list_lock));
  260. struct shim_futex* futex;
  261. LISTP_FOR_EACH_ENTRY(futex, &g_futex_list, list) {
  262. if (futex->uaddr == uaddr) {
  263. get_futex(futex);
  264. return futex;
  265. }
  266. }
  267. return NULL;
  268. }
  269. static uint64_t timespec_to_us(const struct timespec* ts) {
  270. return (uint64_t)ts->tv_sec * 1000000u + (uint64_t)ts->tv_nsec / 1000u;
  271. }
  272. static int futex_wait(uint32_t* uaddr, uint32_t val, uint64_t timeout, uint32_t bitset) {
  273. int ret = 0;
  274. struct shim_futex* futex = NULL;
  275. struct shim_thread* thread = NULL;
  276. struct shim_futex* tmp = NULL;
  277. spinlock_lock(&g_futex_list_lock);
  278. futex = find_futex(uaddr);
  279. if (!futex) {
  280. spinlock_unlock(&g_futex_list_lock);
  281. tmp = create_new_futex(uaddr);
  282. if (!tmp) {
  283. return -ENOMEM;
  284. }
  285. spinlock_lock(&g_futex_list_lock);
  286. futex = find_futex(uaddr);
  287. if (!futex) {
  288. enqueue_futex(tmp);
  289. futex = tmp;
  290. tmp = NULL;
  291. }
  292. }
  293. spinlock_lock(&futex->lock);
  294. spinlock_unlock(&g_futex_list_lock);
  295. if (__atomic_load_n(uaddr, __ATOMIC_RELAXED) != val) {
  296. ret = -EAGAIN;
  297. goto out_with_futex_lock;
  298. }
  299. struct futex_waiter waiter = { 0 };
  300. add_futex_waiter(&waiter, futex, bitset);
  301. spinlock_unlock(&futex->lock);
  302. /* Give up this futex reference - we have no idea what futex we will be on once we wake up
  303. * (due to possible requeues). */
  304. put_futex(futex);
  305. futex = NULL;
  306. ret = thread_sleep(timeout);
  307. /* On timeout thread_sleep returns -EAGAIN. */
  308. if (ret == -EAGAIN) {
  309. ret = -ETIMEDOUT;
  310. }
  311. spinlock_lock(&g_futex_list_lock);
  312. /* We might have been requeued. Grab the (possibly new) futex reference. */
  313. futex = waiter.futex;
  314. assert(futex);
  315. get_futex(futex);
  316. spinlock_lock(&futex->lock);
  317. spinlock_unlock(&g_futex_list_lock);
  318. if (!LIST_EMPTY(&waiter, list)) {
  319. /* If we woke up due to time out, we were not removed from the waiters list (opposite
  320. * of when another thread calls FUTEX_WAKE, which would remove us from the list). */
  321. thread = remove_futex_waiter(&waiter, futex);
  322. }
  323. /* At this point we are done using the `waiter` struct and need to give up the futex reference
  324. * it was holding.
  325. * NB: actually `futex` and this point to the same futex, so this won't call free. */
  326. put_futex(waiter.futex);
  327. out_with_futex_lock: ; // C is awesome!
  328. /* Because dequeuing a futex requires `g_futex_list_lock` which we do not hold at this moment,
  329. * we check if we actually need to do it now (locks acquisition and dequeuing). */
  330. bool needs_dequeue = check_dequeue_futex(futex);
  331. spinlock_unlock(&futex->lock);
  332. if (needs_dequeue) {
  333. maybe_dequeue_futex(futex);
  334. }
  335. if (thread) {
  336. put_thread(thread);
  337. }
  338. put_futex(futex);
  339. if (tmp) {
  340. put_futex(tmp);
  341. }
  342. return ret;
  343. }
  344. /*
  345. * Moves at most `to_wake` waiters from futex to wake queue;
  346. * In the Linux kernel the number of waiters to wake has type `int` and we follow that here.
  347. * Normally `bitset` has to be non-zero, here zero means: do not even check it.
  348. *
  349. * Must be called with `futex->lock` held.
  350. *
  351. * Returns number of threads woken.
  352. */
  353. static int move_to_wake_queue(struct shim_futex* futex, uint32_t bitset, int to_wake,
  354. struct wake_queue_head* queue) {
  355. assert(spinlock_is_locked(&futex->lock));
  356. struct futex_waiter* waiter;
  357. struct futex_waiter* wtmp;
  358. struct shim_thread* thread;
  359. int woken = 0;
  360. LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex->waiters, list) {
  361. if (bitset && !(waiter->bitset & bitset)) {
  362. continue;
  363. }
  364. thread = remove_futex_waiter(waiter, futex);
  365. if (add_thread_to_queue(queue, thread)) {
  366. put_thread(thread);
  367. }
  368. /* If to_wake (3rd argument of futex syscall) is 0, the Linux kernel still wakes up
  369. * one thread - so we do the same here. */
  370. if (++woken >= to_wake) {
  371. break;
  372. }
  373. }
  374. return woken;
  375. }
  376. static int futex_wake(uint32_t* uaddr, int to_wake, uint32_t bitset) {
  377. struct shim_futex* futex;
  378. struct wake_queue_head queue = { .first = WAKE_QUEUE_TAIL };
  379. int woken = 0;
  380. if (!bitset) {
  381. return -EINVAL;
  382. }
  383. spinlock_lock(&g_futex_list_lock);
  384. futex = find_futex(uaddr);
  385. if (!futex) {
  386. spinlock_unlock(&g_futex_list_lock);
  387. return 0;
  388. }
  389. spinlock_lock(&futex->lock);
  390. spinlock_unlock(&g_futex_list_lock);
  391. woken = move_to_wake_queue(futex, bitset, to_wake, &queue);
  392. bool needs_dequeue = check_dequeue_futex(futex);
  393. spinlock_unlock(&futex->lock);
  394. if (needs_dequeue) {
  395. maybe_dequeue_futex(futex);
  396. }
  397. wake_queue(&queue);
  398. put_futex(futex);
  399. return woken;
  400. }
  401. /*
  402. * Sign-extends 12 bit argument to 32 bits.
  403. */
  404. static int wakeop_arg_extend(int x) {
  405. if (x >= 0x800) {
  406. return 0xfffff000 | x;
  407. }
  408. return x;
  409. }
  410. static int futex_wake_op(uint32_t* uaddr1, uint32_t* uaddr2, int to_wake1, int to_wake2, uint32_t val3) {
  411. struct shim_futex* futex1 = NULL;
  412. struct shim_futex* futex2 = NULL;
  413. struct wake_queue_head queue = { .first = WAKE_QUEUE_TAIL };
  414. int ret = 0;
  415. bool needs_dequeue1 = false;
  416. bool needs_dequeue2 = false;
  417. spinlock_lock(&g_futex_list_lock);
  418. futex1 = find_futex(uaddr1);
  419. futex2 = find_futex(uaddr2);
  420. lock_two_futexes(futex1, futex2);
  421. spinlock_unlock(&g_futex_list_lock);
  422. unsigned int op = (val3 >> 28) & 0x7; // highest bit is for FUTEX_OP_OPARG_SHIFT
  423. unsigned int cmp = (val3 >> 24) & 0xf;
  424. int oparg = wakeop_arg_extend((val3 >> 12) & 0xfff);
  425. int cmparg = wakeop_arg_extend(val3 & 0xfff);
  426. int oldval;
  427. bool cmpval;
  428. if ((val3 >> 28) & FUTEX_OP_OPARG_SHIFT) {
  429. if (oparg < 0 || oparg > 31) {
  430. /* In case of invalid argument to shift the Linux kernel just fixes the argument,
  431. * so we do the same. */
  432. oparg &= 0x1f;
  433. }
  434. if (oparg == 31) {
  435. // left shift by 31 would be UB here
  436. oparg = -2147483648;
  437. } else {
  438. oparg = 1 << oparg;
  439. }
  440. }
  441. switch (op) {
  442. case FUTEX_OP_SET:
  443. oldval = __atomic_exchange_n(uaddr2, oparg, __ATOMIC_RELAXED);
  444. break;
  445. case FUTEX_OP_ADD:
  446. oldval = __atomic_fetch_add(uaddr2, oparg, __ATOMIC_RELAXED);
  447. break;
  448. case FUTEX_OP_OR:
  449. oldval = __atomic_fetch_or(uaddr2, oparg, __ATOMIC_RELAXED);
  450. break;
  451. case FUTEX_OP_ANDN:
  452. oldval = __atomic_fetch_nand(uaddr2, oparg, __ATOMIC_RELAXED);
  453. break;
  454. case FUTEX_OP_XOR:
  455. oldval = __atomic_fetch_xor(uaddr2, oparg, __ATOMIC_RELAXED);
  456. break;
  457. default:
  458. ret = -ENOSYS;
  459. goto out_unlock;
  460. }
  461. switch (cmp) {
  462. case FUTEX_OP_CMP_EQ:
  463. cmpval = oldval == cmparg;
  464. break;
  465. case FUTEX_OP_CMP_NE:
  466. cmpval = oldval != cmparg;
  467. break;
  468. case FUTEX_OP_CMP_LT:
  469. cmpval = oldval < cmparg;
  470. break;
  471. case FUTEX_OP_CMP_LE:
  472. cmpval = oldval <= cmparg;
  473. break;
  474. case FUTEX_OP_CMP_GT:
  475. cmpval = oldval > cmparg;
  476. break;
  477. case FUTEX_OP_CMP_GE:
  478. cmpval = oldval >= cmparg;
  479. break;
  480. default:
  481. ret = -ENOSYS;
  482. goto out_unlock;
  483. }
  484. if (futex1) {
  485. ret += move_to_wake_queue(futex1, 0, to_wake1, &queue);
  486. needs_dequeue1 = check_dequeue_futex(futex1);
  487. }
  488. if (futex2 && cmpval) {
  489. ret += move_to_wake_queue(futex2, 0, to_wake2, &queue);
  490. needs_dequeue2 = check_dequeue_futex(futex2);
  491. }
  492. out_unlock:
  493. unlock_two_futexes(futex1, futex2);
  494. if (needs_dequeue1 || needs_dequeue2) {
  495. maybe_dequeue_two_futexes(futex1, futex2);
  496. }
  497. if (ret > 0) {
  498. wake_queue(&queue);
  499. }
  500. if (futex1) {
  501. put_futex(futex1);
  502. }
  503. if (futex2) {
  504. put_futex(futex2);
  505. }
  506. return ret;
  507. }
  508. static int futex_requeue(uint32_t* uaddr1, uint32_t* uaddr2, int to_wake, int to_requeue, uint32_t* val) {
  509. struct shim_futex* futex1 = NULL;
  510. struct shim_futex* futex2 = NULL;
  511. struct shim_futex* tmp = NULL;
  512. struct wake_queue_head queue = { .first = WAKE_QUEUE_TAIL };
  513. int ret = 0;
  514. int woken = 0;
  515. int requeued = 0;
  516. struct futex_waiter* waiter;
  517. struct futex_waiter* wtmp;
  518. struct shim_thread* thread;
  519. bool needs_dequeue1 = false;
  520. bool needs_dequeue2 = false;
  521. if (to_wake < 0 || to_requeue < 0) {
  522. return -EINVAL;
  523. }
  524. spinlock_lock(&g_futex_list_lock);
  525. futex2 = find_futex(uaddr2);
  526. if (!futex2) {
  527. spinlock_unlock(&g_futex_list_lock);
  528. tmp = create_new_futex(uaddr2);
  529. if (!tmp) {
  530. return -ENOMEM;
  531. }
  532. needs_dequeue2 = true;
  533. spinlock_lock(&g_futex_list_lock);
  534. futex2 = find_futex(uaddr2);
  535. if (!futex2) {
  536. enqueue_futex(tmp);
  537. futex2 = tmp;
  538. tmp = NULL;
  539. }
  540. }
  541. futex1 = find_futex(uaddr1);
  542. lock_two_futexes(futex1, futex2);
  543. spinlock_unlock(&g_futex_list_lock);
  544. if (val != NULL) {
  545. if (__atomic_load_n(uaddr1, __ATOMIC_RELAXED) != *val) {
  546. ret = -EAGAIN;
  547. goto out_unlock;
  548. }
  549. }
  550. if (futex1) {
  551. /* We cannot call move_to_wake_queue here, as this function wakes at least 1 thread,
  552. * (even if to_wake is 0) and here we want to wake-up exactly to_wake threads.
  553. * I guess it's better to be compatible and replicate these weird corner cases. */
  554. LISTP_FOR_EACH_ENTRY_SAFE(waiter, wtmp, &futex1->waiters, list) {
  555. if (woken < to_wake) {
  556. thread = remove_futex_waiter(waiter, futex1);
  557. if (add_thread_to_queue(&queue, thread)) {
  558. put_thread(thread);
  559. }
  560. ++woken;
  561. } else if (requeued < to_requeue) {
  562. move_futex_waiter(waiter, futex1, futex2);
  563. ++requeued;
  564. } else {
  565. break;
  566. }
  567. }
  568. needs_dequeue1 = check_dequeue_futex(futex1);
  569. needs_dequeue2 = check_dequeue_futex(futex2);
  570. ret = woken + requeued;
  571. }
  572. out_unlock:
  573. unlock_two_futexes(futex1, futex2);
  574. if (needs_dequeue1 || needs_dequeue2) {
  575. maybe_dequeue_two_futexes(futex1, futex2);
  576. }
  577. if (woken > 0) {
  578. wake_queue(&queue);
  579. }
  580. if (futex1) {
  581. put_futex(futex1);
  582. }
  583. assert(futex2);
  584. put_futex(futex2);
  585. if (tmp) {
  586. put_futex(tmp);
  587. }
  588. return ret;
  589. }
  590. #define FUTEX_CHECK_READ false
  591. #define FUTEX_CHECK_WRITE true
  592. static int is_valid_futex_ptr(uint32_t* ptr, bool check_write) {
  593. if (!IS_ALIGNED_PTR(ptr, alignof(*ptr))) {
  594. return -EINVAL;
  595. }
  596. if (test_user_memory(ptr, sizeof(*ptr), check_write)) {
  597. return -EFAULT;
  598. }
  599. return 0;
  600. }
  601. static int _shim_do_futex(uint32_t* uaddr, int op, uint32_t val, void* utime, uint32_t* uaddr2, uint32_t val3) {
  602. int cmd = op & FUTEX_CMD_MASK;
  603. uint64_t timeout = NO_TIMEOUT;
  604. uint32_t val2 = 0;
  605. if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_WAIT_BITSET ||
  606. cmd == FUTEX_LOCK_PI || cmd == FUTEX_WAIT_REQUEUE_PI)) {
  607. if (test_user_memory(utime, sizeof(struct timespec), /*write=*/false)) {
  608. return -EFAULT;
  609. }
  610. timeout = timespec_to_us((struct timespec*)utime);
  611. if (cmd != FUTEX_WAIT) {
  612. /* For FUTEX_WAIT, timeout is interpreted as a relative value, which differs from other
  613. * futex operations, where timeout is interpreted as an absolute value. */
  614. uint64_t current_time = DkSystemTimeQuery();
  615. if (!current_time) {
  616. return -EINVAL;
  617. }
  618. if (timeout < current_time) {
  619. /* We timeouted even before reaching this point. */
  620. return -ETIMEDOUT;
  621. }
  622. timeout -= current_time;
  623. }
  624. }
  625. if (cmd == FUTEX_CMP_REQUEUE || cmd == FUTEX_REQUEUE || cmd == FUTEX_WAKE_OP ||
  626. cmd == FUTEX_CMP_REQUEUE_PI) {
  627. val2 = (uint32_t)(unsigned long)utime;
  628. }
  629. if (op & FUTEX_CLOCK_REALTIME) {
  630. if (cmd != FUTEX_WAIT && cmd != FUTEX_WAIT_BITSET && cmd != FUTEX_WAIT_REQUEUE_PI) {
  631. return -ENOSYS;
  632. }
  633. /* Graphene has only one clock for now. */
  634. debug("Ignoring FUTEX_CLOCK_REALTIME flag\n");
  635. }
  636. if (!(op & FUTEX_PRIVATE_FLAG)) {
  637. debug("Non-private futexes are not supported, assuming implicit FUTEX_PRIVATE_FLAG\n");
  638. }
  639. int ret = 0;
  640. /* `uaddr` should be valid pointer in all cases. */
  641. ret = is_valid_futex_ptr(uaddr, FUTEX_CHECK_READ);
  642. if (ret) {
  643. return ret;
  644. }
  645. switch (cmd) {
  646. case FUTEX_WAIT:
  647. val3 = FUTEX_BITSET_MATCH_ANY;
  648. /* fallthrough */
  649. case FUTEX_WAIT_BITSET:
  650. return futex_wait(uaddr, val, timeout, val3);
  651. case FUTEX_WAKE:
  652. val3 = FUTEX_BITSET_MATCH_ANY;
  653. /* fallthrough */
  654. case FUTEX_WAKE_BITSET:
  655. return futex_wake(uaddr, val, val3);
  656. case FUTEX_WAKE_OP:
  657. ret = is_valid_futex_ptr(uaddr2, FUTEX_CHECK_WRITE);
  658. if (ret) {
  659. return ret;
  660. }
  661. return futex_wake_op(uaddr, uaddr2, val, val2, val3);
  662. case FUTEX_REQUEUE:
  663. ret = is_valid_futex_ptr(uaddr2, FUTEX_CHECK_READ);
  664. if (ret) {
  665. return ret;
  666. }
  667. return futex_requeue(uaddr, uaddr2, val, val2, NULL);
  668. case FUTEX_CMP_REQUEUE:
  669. ret = is_valid_futex_ptr(uaddr2, FUTEX_CHECK_READ);
  670. if (ret) {
  671. return ret;
  672. }
  673. return futex_requeue(uaddr, uaddr2, val, val2, &val3);
  674. case FUTEX_LOCK_PI:
  675. case FUTEX_TRYLOCK_PI:
  676. case FUTEX_UNLOCK_PI:
  677. case FUTEX_CMP_REQUEUE_PI:
  678. case FUTEX_WAIT_REQUEUE_PI:
  679. debug("PI futexes are not yet supported!\n");
  680. return -ENOSYS;
  681. default:
  682. debug("Invalid futex op: %d\n", cmd);
  683. return -ENOSYS;
  684. }
  685. }
  686. int shim_do_futex(int* uaddr, int op, int val, void* utime, int* uaddr2, int val3) {
  687. static_assert(sizeof(int) == 4, "futexes are defined to be 32-bit");
  688. return _shim_do_futex((uint32_t*)uaddr, op, (uint32_t)val, utime, (uint32_t*)uaddr2, (uint32_t)val3);
  689. }
  690. int shim_do_set_robust_list(struct robust_list_head* head, size_t len) {
  691. if (len != sizeof(struct robust_list_head)) {
  692. return -EINVAL;
  693. }
  694. get_cur_thread()->robust_list = head;
  695. return 0;
  696. }
  697. int shim_do_get_robust_list(pid_t pid, struct robust_list_head** head, size_t* len) {
  698. struct shim_thread* thread;
  699. int ret = 0;
  700. if (pid) {
  701. thread = lookup_thread(pid);
  702. if (!thread) {
  703. return -ESRCH;
  704. }
  705. } else {
  706. thread = get_cur_thread();
  707. get_thread(thread);
  708. }
  709. if (test_user_memory(head, sizeof(*head), /*write=*/true) || test_user_memory(len, sizeof(*len), /*write=*/true)) {
  710. ret = -EFAULT;
  711. goto out;
  712. }
  713. *head = thread->robust_list;
  714. *len = sizeof(**head);
  715. out:
  716. put_thread(thread);
  717. return ret;
  718. }
  719. /*
  720. * Process one robust futex, waking a waiter if present.
  721. * Returns 0 on success, negative value otherwise.
  722. */
  723. static bool handle_futex_death(uint32_t* uaddr) {
  724. uint32_t val;
  725. if (!IS_ALIGNED_PTR(uaddr, alignof(*uaddr))) {
  726. return -EINVAL;
  727. }
  728. if (!is_valid_futex_ptr(uaddr, FUTEX_CHECK_WRITE)) {
  729. return -EFAULT;
  730. }
  731. /* Loop until we successfully set the futex word or see someone else taking this futex. */
  732. while (1) {
  733. val = __atomic_load_n(uaddr, __ATOMIC_RELAXED);
  734. if ((val & FUTEX_TID_MASK) != get_cur_thread()->tid) {
  735. /* Someone else is holding this futex. */
  736. return 0;
  737. }
  738. /* Mark the FUTEX_OWNER_DIED bit, clear all tid bits. */
  739. uint32_t new_val = (val & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
  740. if (__atomic_compare_exchange_n(uaddr, &val, new_val,
  741. /*weak=*/true, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
  742. /* Successfully set the new value, end the loop. */
  743. break;
  744. }
  745. }
  746. if (val & FUTEX_WAITERS) {
  747. /* There are waiters present, wake one of them. */
  748. futex_wake(uaddr, 1, FUTEX_BITSET_MATCH_ANY);
  749. }
  750. return 0;
  751. }
  752. /*
  753. * Fetches robust list entry from user memory, checking invalid pointers.
  754. * Returns 0 on success, negative value on error.
  755. */
  756. static bool fetch_robust_entry(struct robust_list** entry, struct robust_list** head) {
  757. if (test_user_memory(head, sizeof(*head), /*write=*/false)) {
  758. return -EFAULT;
  759. }
  760. *entry = *head;
  761. return 0;
  762. }
  763. static uint32_t* entry_to_futex(struct robust_list* entry, long futex_offset) {
  764. return (uint32_t*)((char*)entry + futex_offset);
  765. }
  766. /*
  767. * Release all robust futexes.
  768. * The list itself is in user provided memory - we need to check each pointer before dereferencing
  769. * it. If any check fails, we silently return and ignore the rest.
  770. */
  771. void release_robust_list(struct robust_list_head* head) {
  772. struct robust_list* entry;
  773. struct robust_list* pending;
  774. long futex_offset;
  775. unsigned long limit = ROBUST_LIST_LIMIT;
  776. /* `&head->list.next` does not dereference head, hence is safe. */
  777. if (fetch_robust_entry(&entry, &head->list.next)) {
  778. return;
  779. }
  780. if (test_user_memory(&head->futex_offset, sizeof(head->futex_offset), /*write=*/false)) {
  781. return;
  782. }
  783. futex_offset = head->futex_offset;
  784. if (fetch_robust_entry(&pending, &head->list_op_pending)) {
  785. return;
  786. }
  787. /* Last entry (or first, if the list is empty) points to the list head. */
  788. while (entry != &head->list) {
  789. struct robust_list* next_entry;
  790. /* Fetch the next entry before waking the next thread. */
  791. bool ret = fetch_robust_entry(&next_entry, &entry->next);
  792. if (entry != pending) {
  793. if (handle_futex_death(entry_to_futex(entry, futex_offset))) {
  794. return;
  795. }
  796. }
  797. if (ret) {
  798. return;
  799. }
  800. entry = next_entry;
  801. /* This mostly guards from circular lists. */
  802. if (!--limit) {
  803. break;
  804. }
  805. }
  806. if (pending) {
  807. if (handle_futex_death(entry_to_futex(pending, futex_offset))) {
  808. return;
  809. }
  810. }
  811. }
  812. void release_clear_child_tid(int* clear_child_tid) {
  813. if (!clear_child_tid || !IS_ALIGNED_PTR(clear_child_tid, alignof(*clear_child_tid)) ||
  814. test_user_memory(clear_child_tid, sizeof(*clear_child_tid), /*write=*/true))
  815. return;
  816. /* child thread exited, now parent can wake up */
  817. __atomic_store_n(clear_child_tid, 0, __ATOMIC_RELAXED);
  818. futex_wake((uint32_t*)clear_child_tid, 1, FUTEX_BITSET_MATCH_ANY);
  819. }