| 1 | /* |
| 2 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | * |
| 4 | * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | * |
| 6 | * Userspace RCU - sys_futex/compat_futex header. |
| 7 | */ |
| 8 | |
| 9 | #ifndef _LTTNG_UST_FUTEX_H |
| 10 | #define _LTTNG_UST_FUTEX_H |
| 11 | |
| 12 | #include <errno.h> |
| 13 | #include <stdint.h> |
| 14 | #include <time.h> |
| 15 | #include <sys/syscall.h> |
| 16 | |
| 17 | #include "ust-helper.h" |
| 18 | |
| 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
| 23 | #define FUTEX_WAIT 0 |
| 24 | #define FUTEX_WAKE 1 |
| 25 | |
| 26 | /* |
| 27 | * sys_futex compatibility header. |
| 28 | * Use *only* *either of* futex_noasync OR futex_async on a given address. |
| 29 | * |
| 30 | * futex_noasync cannot be executed in signal handlers, but ensures that |
| 31 | * it will be put in a wait queue even in compatibility mode. |
| 32 | * |
| 33 | * futex_async is signal-handler safe for the wakeup. It uses polling |
| 34 | * on the wait-side in compatibility mode. |
| 35 | * |
| 36 | * BEWARE: sys_futex() FUTEX_WAIT may return early if interrupted |
| 37 | * (returns EINTR). |
| 38 | */ |
| 39 | |
| 40 | LTTNG_HIDDEN |
| 41 | extern int lttng_ust_compat_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 42 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); |
| 43 | LTTNG_HIDDEN |
| 44 | extern int lttng_ust_compat_futex_async(int32_t *uaddr, int op, int32_t val, |
| 45 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3); |
| 46 | |
| 47 | #if (defined(__linux__) && defined(__NR_futex)) |
| 48 | |
| 49 | #include <unistd.h> |
| 50 | #include <errno.h> |
| 51 | #include <urcu/compiler.h> |
| 52 | #include <urcu/arch.h> |
| 53 | |
| 54 | static inline int lttng_ust_futex(int32_t *uaddr, int op, int32_t val, |
| 55 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 56 | { |
| 57 | return syscall(__NR_futex, uaddr, op, val, timeout, |
| 58 | uaddr2, val3); |
| 59 | } |
| 60 | |
| 61 | static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 62 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 63 | { |
| 64 | int ret; |
| 65 | |
| 66 | ret = lttng_ust_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 67 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { |
| 68 | /* |
| 69 | * The fallback on ENOSYS is the async-safe version of |
| 70 | * the compat futex implementation, because the |
| 71 | * async-safe compat implementation allows being used |
| 72 | * concurrently with calls to futex(). Indeed, sys_futex |
| 73 | * FUTEX_WAIT, on some architectures (mips and parisc), |
| 74 | * within a given process, spuriously return ENOSYS due |
| 75 | * to signal restart bugs on some kernel versions. |
| 76 | */ |
| 77 | return lttng_ust_compat_futex_async(uaddr, op, val, timeout, |
| 78 | uaddr2, val3); |
| 79 | } |
| 80 | return ret; |
| 81 | |
| 82 | } |
| 83 | |
| 84 | static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val, |
| 85 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 86 | { |
| 87 | int ret; |
| 88 | |
| 89 | ret = lttng_ust_futex(uaddr, op, val, timeout, uaddr2, val3); |
| 90 | if (caa_unlikely(ret < 0 && errno == ENOSYS)) { |
| 91 | return lttng_ust_compat_futex_async(uaddr, op, val, timeout, |
| 92 | uaddr2, val3); |
| 93 | } |
| 94 | return ret; |
| 95 | } |
| 96 | |
| 97 | #elif defined(__FreeBSD__) |
| 98 | |
| 99 | #include <sys/types.h> |
| 100 | #include <sys/umtx.h> |
| 101 | |
| 102 | static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val, |
| 103 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 104 | { |
| 105 | int umtx_op; |
| 106 | void *umtx_uaddr = NULL, *umtx_uaddr2 = NULL; |
| 107 | struct _umtx_time umtx_timeout = { |
| 108 | ._flags = UMTX_ABSTIME, |
| 109 | ._clockid = CLOCK_MONOTONIC, |
| 110 | }; |
| 111 | |
| 112 | switch (op) { |
| 113 | case FUTEX_WAIT: |
| 114 | /* On FreeBSD, a "u_int" is a 32-bit integer. */ |
| 115 | umtx_op = UMTX_OP_WAIT_UINT; |
| 116 | if (timeout != NULL) { |
| 117 | umtx_timeout._timeout = *timeout; |
| 118 | umtx_uaddr = (void *) sizeof(umtx_timeout); |
| 119 | umtx_uaddr2 = (void *) &umtx_timeout; |
| 120 | } |
| 121 | break; |
| 122 | case FUTEX_WAKE: |
| 123 | umtx_op = UMTX_OP_WAKE; |
| 124 | break; |
| 125 | default: |
| 126 | errno = EINVAL; |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | return _umtx_op(uaddr, umtx_op, (uint32_t) val, umtx_uaddr, |
| 131 | umtx_uaddr2); |
| 132 | } |
| 133 | |
| 134 | static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 135 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 136 | { |
| 137 | return lttng_ust_futex_async(uaddr, op, val, timeout, uaddr2, val3); |
| 138 | } |
| 139 | |
| 140 | #elif defined(__CYGWIN__) |
| 141 | |
| 142 | /* |
| 143 | * The futex_noasync compat code uses a weak symbol to share state across |
| 144 | * different shared object which is not possible on Windows with the |
| 145 | * Portable Executable format. Use the async compat code for both cases. |
| 146 | */ |
| 147 | static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 148 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 149 | { |
| 150 | return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); |
| 151 | } |
| 152 | |
| 153 | static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val, |
| 154 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 155 | { |
| 156 | return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); |
| 157 | } |
| 158 | |
| 159 | #else |
| 160 | |
| 161 | static inline int lttng_ust_futex_noasync(int32_t *uaddr, int op, int32_t val, |
| 162 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 163 | { |
| 164 | return lttng_ust_compat_futex_noasync(uaddr, op, val, timeout, uaddr2, val3); |
| 165 | } |
| 166 | |
| 167 | static inline int lttng_ust_futex_async(int32_t *uaddr, int op, int32_t val, |
| 168 | const struct timespec *timeout, int32_t *uaddr2, int32_t val3) |
| 169 | { |
| 170 | return lttng_ust_compat_futex_async(uaddr, op, val, timeout, uaddr2, val3); |
| 171 | } |
| 172 | |
| 173 | #endif |
| 174 | |
| 175 | #ifdef __cplusplus |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #endif /* _LTTNG_UST_FUTEX_H */ |