1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
5 * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
23 #ifndef rseq_sizeof_field
24 #define rseq_sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
27 #ifndef rseq_offsetofend
28 #define rseq_offsetofend(TYPE, MEMBER) \
29 (offsetof(TYPE, MEMBER) + rseq_sizeof_field(TYPE, MEMBER))
33 * Empty code injection macros, override when testing.
34 * It is important to consider that the ASM injection macros need to be
35 * fully reentrant (e.g. do not modify the stack).
37 #ifndef RSEQ_INJECT_ASM
38 #define RSEQ_INJECT_ASM(n)
42 #define RSEQ_INJECT_C(n)
45 #ifndef RSEQ_INJECT_INPUT
46 #define RSEQ_INJECT_INPUT
49 #ifndef RSEQ_INJECT_CLOBBER
50 #define RSEQ_INJECT_CLOBBER
53 #ifndef RSEQ_INJECT_FAILED
54 #define RSEQ_INJECT_FAILED
57 #include "rseq-thread-pointer.h"
59 /* Offset from the thread pointer to the rseq area. */
60 extern ptrdiff_t rseq_offset
;
63 * Size of the registered rseq area. 0 if the registration was
66 extern unsigned int rseq_size
;
68 /* Flags used during rseq registration. */
69 extern unsigned int rseq_flags
;
73 RSEQ_MO_CONSUME
= 1, /* Unused */
74 RSEQ_MO_ACQUIRE
= 2, /* Unused */
76 RSEQ_MO_ACQ_REL
= 4, /* Unused */
77 RSEQ_MO_SEQ_CST
= 5, /* Unused */
80 enum rseq_percpu_mode
{
81 RSEQ_PERCPU_CPU_ID
= 0,
82 RSEQ_PERCPU_MM_CID
= 1,
85 static inline struct rseq_abi
*rseq_get_abi(void)
87 return (struct rseq_abi
*) ((uintptr_t) rseq_thread_pointer() + rseq_offset
);
90 #define rseq_likely(x) __builtin_expect(!!(x), 1)
91 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
92 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
94 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
95 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
96 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
98 #define __rseq_str_1(x) #x
99 #define __rseq_str(x) __rseq_str_1(x)
101 #define rseq_log(fmt, args...) \
102 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
105 #define rseq_bug(fmt, args...) \
107 rseq_log(fmt, ##args); \
111 #if defined(__x86_64__) || defined(__i386__)
112 #include <rseq-x86.h>
113 #elif defined(__ARMEL__)
114 #include <rseq-arm.h>
115 #elif defined (__AARCH64EL__)
116 #include <rseq-arm64.h>
117 #elif defined(__PPC__)
118 #include <rseq-ppc.h>
119 #elif defined(__mips__)
120 #include <rseq-mips.h>
121 #elif defined(__s390__)
122 #include <rseq-s390.h>
123 #elif defined(__riscv)
124 #include <rseq-riscv.h>
126 #error unsupported target
130 * Register rseq for the current thread. This needs to be called once
131 * by any thread which uses restartable sequences, before they start
132 * using restartable sequences, to ensure restartable sequences
133 * succeed. A restartable sequence executed from a non-registered
134 * thread will always fail.
136 int rseq_register_current_thread(void);
139 * Unregister rseq for current thread.
141 int rseq_unregister_current_thread(void);
144 * Restartable sequence fallback for reading the current CPU number.
146 int32_t rseq_fallback_current_cpu(void);
149 * Restartable sequence fallback for reading the current node number.
151 int32_t rseq_fallback_current_node(void);
154 * Values returned can be either the current CPU number, -1 (rseq is
155 * uninitialized), or -2 (rseq initialization has failed).
157 static inline int32_t rseq_current_cpu_raw(void)
159 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id
);
163 * Returns a possible CPU number, which is typically the current CPU.
164 * The returned CPU number can be used to prepare for an rseq critical
165 * section, which will confirm whether the cpu number is indeed the
166 * current one, and whether rseq is initialized.
168 * The CPU number returned by rseq_cpu_start should always be validated
169 * by passing it to a rseq asm sequence, or by comparing it to the
170 * return value of rseq_current_cpu_raw() if the rseq asm sequence
171 * does not need to be invoked.
173 static inline uint32_t rseq_cpu_start(void)
175 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start
);
178 static inline uint32_t rseq_current_cpu(void)
182 cpu
= rseq_current_cpu_raw();
183 if (rseq_unlikely(cpu
< 0))
184 cpu
= rseq_fallback_current_cpu();
188 static inline bool rseq_node_id_available(void)
190 return (int) rseq_size
>= rseq_offsetofend(struct rseq_abi
, node_id
);
194 * Current NUMA node number.
196 static inline uint32_t rseq_current_node_id(void)
198 assert(rseq_node_id_available());
199 return RSEQ_ACCESS_ONCE(rseq_get_abi()->node_id
);
202 static inline bool rseq_mm_cid_available(void)
204 return (int) rseq_size
>= rseq_offsetofend(struct rseq_abi
, mm_cid
);
207 static inline uint32_t rseq_current_mm_cid(void)
209 return RSEQ_ACCESS_ONCE(rseq_get_abi()->mm_cid
);
212 static inline void rseq_clear_rseq_cs(void)
214 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs
.arch
.ptr
, 0);
218 * rseq_prepare_unload() should be invoked by each thread executing a rseq
219 * critical section at least once between their last critical section and
220 * library unload of the library defining the rseq critical section (struct
221 * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
222 * post_commit_offset fields. This also applies to use of rseq in code
223 * generated by JIT: rseq_prepare_unload() should be invoked at least once by
224 * each thread executing a rseq critical section before reclaim of the memory
225 * holding the struct rseq_cs or reclaim of the code pointed to by struct
226 * rseq_cs start_ip and post_commit_offset fields.
228 static inline void rseq_prepare_unload(void)
230 rseq_clear_rseq_cs();
233 static inline __attribute__((always_inline
))
234 int rseq_cmpeqv_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
235 intptr_t *v
, intptr_t expect
,
236 intptr_t newv
, int cpu
)
238 if (rseq_mo
!= RSEQ_MO_RELAXED
)
240 switch (percpu_mode
) {
241 case RSEQ_PERCPU_CPU_ID
:
242 return rseq_cmpeqv_storev_relaxed_cpu_id(v
, expect
, newv
, cpu
);
243 case RSEQ_PERCPU_MM_CID
:
244 return rseq_cmpeqv_storev_relaxed_mm_cid(v
, expect
, newv
, cpu
);
250 * Compare @v against @expectnot. When it does _not_ match, load @v
251 * into @load, and store the content of *@v + voffp into @v.
253 static inline __attribute__((always_inline
))
254 int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
255 intptr_t *v
, intptr_t expectnot
, long voffp
, intptr_t *load
,
258 if (rseq_mo
!= RSEQ_MO_RELAXED
)
260 switch (percpu_mode
) {
261 case RSEQ_PERCPU_CPU_ID
:
262 return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v
, expectnot
, voffp
, load
, cpu
);
263 case RSEQ_PERCPU_MM_CID
:
264 return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v
, expectnot
, voffp
, load
, cpu
);
269 static inline __attribute__((always_inline
))
270 int rseq_addv(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
271 intptr_t *v
, intptr_t count
, int cpu
)
273 if (rseq_mo
!= RSEQ_MO_RELAXED
)
275 switch (percpu_mode
) {
276 case RSEQ_PERCPU_CPU_ID
:
277 return rseq_addv_relaxed_cpu_id(v
, count
, cpu
);
278 case RSEQ_PERCPU_MM_CID
:
279 return rseq_addv_relaxed_mm_cid(v
, count
, cpu
);
284 #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
289 static inline __attribute__((always_inline
))
290 int rseq_offset_deref_addv(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
291 intptr_t *ptr
, long off
, intptr_t inc
, int cpu
)
293 if (rseq_mo
!= RSEQ_MO_RELAXED
)
295 switch (percpu_mode
) {
296 case RSEQ_PERCPU_CPU_ID
:
297 return rseq_offset_deref_addv_relaxed_cpu_id(ptr
, off
, inc
, cpu
);
298 case RSEQ_PERCPU_MM_CID
:
299 return rseq_offset_deref_addv_relaxed_mm_cid(ptr
, off
, inc
, cpu
);
305 static inline __attribute__((always_inline
))
306 int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
307 intptr_t *v
, intptr_t expect
,
308 intptr_t *v2
, intptr_t newv2
,
309 intptr_t newv
, int cpu
)
312 case RSEQ_MO_RELAXED
:
313 switch (percpu_mode
) {
314 case RSEQ_PERCPU_CPU_ID
:
315 return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v
, expect
, v2
, newv2
, newv
, cpu
);
316 case RSEQ_PERCPU_MM_CID
:
317 return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v
, expect
, v2
, newv2
, newv
, cpu
);
320 case RSEQ_MO_RELEASE
:
321 switch (percpu_mode
) {
322 case RSEQ_PERCPU_CPU_ID
:
323 return rseq_cmpeqv_trystorev_storev_release_cpu_id(v
, expect
, v2
, newv2
, newv
, cpu
);
324 case RSEQ_PERCPU_MM_CID
:
325 return rseq_cmpeqv_trystorev_storev_release_mm_cid(v
, expect
, v2
, newv2
, newv
, cpu
);
333 static inline __attribute__((always_inline
))
334 int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
335 intptr_t *v
, intptr_t expect
,
336 intptr_t *v2
, intptr_t expect2
,
337 intptr_t newv
, int cpu
)
339 if (rseq_mo
!= RSEQ_MO_RELAXED
)
341 switch (percpu_mode
) {
342 case RSEQ_PERCPU_CPU_ID
:
343 return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v
, expect
, v2
, expect2
, newv
, cpu
);
344 case RSEQ_PERCPU_MM_CID
:
345 return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v
, expect
, v2
, expect2
, newv
, cpu
);
350 static inline __attribute__((always_inline
))
351 int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
352 intptr_t *v
, intptr_t expect
,
353 void *dst
, void *src
, size_t len
,
354 intptr_t newv
, int cpu
)
357 case RSEQ_MO_RELAXED
:
358 switch (percpu_mode
) {
359 case RSEQ_PERCPU_CPU_ID
:
360 return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v
, expect
, dst
, src
, len
, newv
, cpu
);
361 case RSEQ_PERCPU_MM_CID
:
362 return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v
, expect
, dst
, src
, len
, newv
, cpu
);
365 case RSEQ_MO_RELEASE
:
366 switch (percpu_mode
) {
367 case RSEQ_PERCPU_CPU_ID
:
368 return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v
, expect
, dst
, src
, len
, newv
, cpu
);
369 case RSEQ_PERCPU_MM_CID
:
370 return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v
, expect
, dst
, src
, len
, newv
, cpu
);