futextest.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /******************************************************************************
  3. *
  4. * Copyright © International Business Machines Corp., 2009
  5. *
  6. * DESCRIPTION
  7. * Glibc independent futex library for testing kernel functionality.
  8. *
  9. * AUTHOR
  10. * Darren Hart <dvhart@linux.intel.com>
  11. *
  12. * HISTORY
  13. * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
  14. *
  15. *****************************************************************************/
  16. #ifndef _FUTEXTEST_H
  17. #define _FUTEXTEST_H
  18. #include <unistd.h>
  19. #include <sys/syscall.h>
  20. #include <sys/types.h>
  21. #include <linux/futex.h>
  22. typedef volatile u_int32_t futex_t;
  23. #define FUTEX_INITIALIZER 0
  24. /* Define the newer op codes if the system header file is not up to date. */
  25. #ifndef FUTEX_WAIT_BITSET
  26. #define FUTEX_WAIT_BITSET 9
  27. #endif
  28. #ifndef FUTEX_WAKE_BITSET
  29. #define FUTEX_WAKE_BITSET 10
  30. #endif
  31. #ifndef FUTEX_WAIT_REQUEUE_PI
  32. #define FUTEX_WAIT_REQUEUE_PI 11
  33. #endif
  34. #ifndef FUTEX_CMP_REQUEUE_PI
  35. #define FUTEX_CMP_REQUEUE_PI 12
  36. #endif
  37. #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
  38. #define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \
  39. FUTEX_PRIVATE_FLAG)
  40. #endif
  41. #ifndef FUTEX_REQUEUE_PI_PRIVATE
  42. #define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \
  43. FUTEX_PRIVATE_FLAG)
  44. #endif
  45. /*
  46. * SYS_futex is expected from system C library, in glibc some 32-bit
  47. * architectures (e.g. RV32) are using 64-bit time_t, therefore it doesn't have
  48. * SYS_futex defined but just SYS_futex_time64. Define SYS_futex as
  49. * SYS_futex_time64 in this situation to ensure the compilation and the
  50. * compatibility.
  51. */
  52. #if !defined(SYS_futex) && defined(SYS_futex_time64)
  53. #define SYS_futex SYS_futex_time64
  54. #endif
  55. /*
  56. * On 32bit systems if we use "-D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64" or if
  57. * we are using a newer compiler then the size of the timestamps will be 64bit,
  58. * however, the SYS_futex will still point to the 32bit futex system call.
  59. */
  60. #if __SIZEOF_POINTER__ == 4 && defined(SYS_futex_time64) && \
  61. defined(_TIME_BITS) && _TIME_BITS == 64
  62. # undef SYS_futex
  63. # define SYS_futex SYS_futex_time64
  64. #endif
  65. /**
  66. * futex() - SYS_futex syscall wrapper
  67. * @uaddr: address of first futex
  68. * @op: futex op code
  69. * @val: typically expected value of uaddr, but varies by op
  70. * @timeout: typically an absolute struct timespec (except where noted
  71. * otherwise). Overloaded by some ops
  72. * @uaddr2: address of second futex for some ops\
  73. * @val3: varies by op
  74. * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
  75. *
  76. * futex() is used by all the following futex op wrappers. It can also be
  77. * used for misuse and abuse testing. Generally, the specific op wrappers
  78. * should be used instead. It is a macro instead of an static inline function as
  79. * some of the types over overloaded (timeout is used for nr_requeue for
  80. * example).
  81. *
  82. * These argument descriptions are the defaults for all
  83. * like-named arguments in the following wrappers except where noted below.
  84. */
  85. #define futex(uaddr, op, val, timeout, uaddr2, val3, opflags) \
  86. syscall(SYS_futex, uaddr, op | opflags, val, timeout, uaddr2, val3)
  87. /**
  88. * futex_wait() - block on uaddr with optional timeout
  89. * @timeout: relative timeout
  90. */
  91. static inline int
  92. futex_wait(futex_t *uaddr, futex_t val, struct timespec *timeout, int opflags)
  93. {
  94. return futex(uaddr, FUTEX_WAIT, val, timeout, NULL, 0, opflags);
  95. }
  96. /**
  97. * futex_wake() - wake one or more tasks blocked on uaddr
  98. * @nr_wake: wake up to this many tasks
  99. */
  100. static inline int
  101. futex_wake(futex_t *uaddr, int nr_wake, int opflags)
  102. {
  103. return futex(uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0, opflags);
  104. }
  105. /**
  106. * futex_wait_bitset() - block on uaddr with bitset
  107. * @bitset: bitset to be used with futex_wake_bitset
  108. */
  109. static inline int
  110. futex_wait_bitset(futex_t *uaddr, futex_t val, struct timespec *timeout,
  111. u_int32_t bitset, int opflags)
  112. {
  113. return futex(uaddr, FUTEX_WAIT_BITSET, val, timeout, NULL, bitset,
  114. opflags);
  115. }
  116. /**
  117. * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
  118. * @bitset: bitset to compare with that used in futex_wait_bitset
  119. */
  120. static inline int
  121. futex_wake_bitset(futex_t *uaddr, int nr_wake, u_int32_t bitset, int opflags)
  122. {
  123. return futex(uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL, NULL, bitset,
  124. opflags);
  125. }
  126. /**
  127. * futex_lock_pi() - block on uaddr as a PI mutex
  128. * @detect: whether (1) or not (0) to perform deadlock detection
  129. */
  130. static inline int
  131. futex_lock_pi(futex_t *uaddr, struct timespec *timeout, int detect,
  132. int opflags)
  133. {
  134. return futex(uaddr, FUTEX_LOCK_PI, detect, timeout, NULL, 0, opflags);
  135. }
  136. /**
  137. * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
  138. */
  139. static inline int
  140. futex_unlock_pi(futex_t *uaddr, int opflags)
  141. {
  142. return futex(uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0, opflags);
  143. }
  144. /**
  145. * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
  146. */
  147. static inline int
  148. futex_wake_op(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_wake2,
  149. int wake_op, int opflags)
  150. {
  151. return futex(uaddr, FUTEX_WAKE_OP, nr_wake, nr_wake2, uaddr2, wake_op,
  152. opflags);
  153. }
  154. /**
  155. * futex_requeue() - requeue without expected value comparison, deprecated
  156. * @nr_wake: wake up to this many tasks
  157. * @nr_requeue: requeue up to this many tasks
  158. *
  159. * Due to its inherently racy implementation, futex_requeue() is deprecated in
  160. * favor of futex_cmp_requeue().
  161. */
  162. static inline int
  163. futex_requeue(futex_t *uaddr, futex_t *uaddr2, int nr_wake, int nr_requeue,
  164. int opflags)
  165. {
  166. return futex(uaddr, FUTEX_REQUEUE, nr_wake, nr_requeue, uaddr2, 0,
  167. opflags);
  168. }
  169. /**
  170. * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
  171. * @nr_wake: wake up to this many tasks
  172. * @nr_requeue: requeue up to this many tasks
  173. */
  174. static inline int
  175. futex_cmp_requeue(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
  176. int nr_requeue, int opflags)
  177. {
  178. return futex(uaddr, FUTEX_CMP_REQUEUE, nr_wake, nr_requeue, uaddr2,
  179. val, opflags);
  180. }
  181. /**
  182. * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
  183. * @uaddr: non-PI futex source
  184. * @uaddr2: PI futex target
  185. *
  186. * This is the first half of the requeue_pi mechanism. It shall always be
  187. * paired with futex_cmp_requeue_pi().
  188. */
  189. static inline int
  190. futex_wait_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2,
  191. struct timespec *timeout, int opflags)
  192. {
  193. return futex(uaddr, FUTEX_WAIT_REQUEUE_PI, val, timeout, uaddr2, 0,
  194. opflags);
  195. }
  196. /**
  197. * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
  198. * @uaddr: non-PI futex source
  199. * @uaddr2: PI futex target
  200. * @nr_wake: wake up to this many tasks
  201. * @nr_requeue: requeue up to this many tasks
  202. */
  203. static inline int
  204. futex_cmp_requeue_pi(futex_t *uaddr, futex_t val, futex_t *uaddr2, int nr_wake,
  205. int nr_requeue, int opflags)
  206. {
  207. return futex(uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake, nr_requeue, uaddr2,
  208. val, opflags);
  209. }
  210. /**
  211. * futex_cmpxchg() - atomic compare and exchange
  212. * @uaddr: The address of the futex to be modified
  213. * @oldval: The expected value of the futex
  214. * @newval: The new value to try and assign the futex
  215. *
  216. * Implement cmpxchg using gcc atomic builtins.
  217. * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
  218. *
  219. * Return the old futex value.
  220. */
  221. static inline u_int32_t
  222. futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
  223. {
  224. return __sync_val_compare_and_swap(uaddr, oldval, newval);
  225. }
  226. /**
  227. * futex_dec() - atomic decrement of the futex value
  228. * @uaddr: The address of the futex to be modified
  229. *
  230. * Return the new futex value.
  231. */
  232. static inline u_int32_t
  233. futex_dec(futex_t *uaddr)
  234. {
  235. return __sync_sub_and_fetch(uaddr, 1);
  236. }
  237. /**
  238. * futex_inc() - atomic increment of the futex value
  239. * @uaddr: the address of the futex to be modified
  240. *
  241. * Return the new futex value.
  242. */
  243. static inline u_int32_t
  244. futex_inc(futex_t *uaddr)
  245. {
  246. return __sync_add_and_fetch(uaddr, 1);
  247. }
  248. /**
  249. * futex_set() - atomic decrement of the futex value
  250. * @uaddr: the address of the futex to be modified
  251. * @newval: New value for the atomic_t
  252. *
  253. * Return the new futex value.
  254. */
  255. static inline u_int32_t
  256. futex_set(futex_t *uaddr, u_int32_t newval)
  257. {
  258. *uaddr = newval;
  259. return newval;
  260. }
  261. #endif