1 /* SPDX-License-Identifier: MIT */
2 /* SPDX-FileCopyrightText: 2016-2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> */
21 #include <rseq/rseq-abi.h>
22 #include <rseq/compiler.h>
24 #ifndef rseq_sizeof_field
25 #define rseq_sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
28 #ifndef rseq_offsetofend
29 #define rseq_offsetofend(TYPE, MEMBER) \
30 (offsetof(TYPE, MEMBER) + rseq_sizeof_field(TYPE, MEMBER))
34 * Empty code injection macros, override when testing.
35 * It is important to consider that the ASM injection macros need to be
36 * fully reentrant (e.g. do not modify the stack).
38 #ifndef RSEQ_INJECT_ASM
39 #define RSEQ_INJECT_ASM(n)
43 #define RSEQ_INJECT_C(n)
46 #ifndef RSEQ_INJECT_INPUT
47 #define RSEQ_INJECT_INPUT
50 #ifndef RSEQ_INJECT_CLOBBER
51 #define RSEQ_INJECT_CLOBBER
54 #ifndef RSEQ_INJECT_FAILED
55 #define RSEQ_INJECT_FAILED
59 * User code can define RSEQ_GET_ABI_OVERRIDE to override the
60 * rseq_get_abi() implementation, for instance to use glibc's symbols
63 #ifndef RSEQ_GET_ABI_OVERRIDE
65 # include <rseq/rseq-thread-pointer.h>
71 /* Offset from the thread pointer to the rseq area. */
72 extern ptrdiff_t rseq_offset
;
75 * Size of the registered rseq area. 0 if the registration was
78 extern unsigned int rseq_size
;
80 /* Flags used during rseq registration. */
81 extern unsigned int rseq_flags
;
84 * rseq feature size supported by the kernel. 0 if the registration was
87 extern unsigned int rseq_feature_size
;
89 static inline struct rseq_abi
*rseq_get_abi(void)
91 return (struct rseq_abi
*) ((uintptr_t) rseq_thread_pointer() + rseq_offset
);
98 #endif /* RSEQ_GET_ABI_OVERRIDE */
102 RSEQ_MO_CONSUME
= 1, /* Unused */
103 RSEQ_MO_ACQUIRE
= 2, /* Unused */
105 RSEQ_MO_ACQ_REL
= 4, /* Unused */
106 RSEQ_MO_SEQ_CST
= 5, /* Unused */
109 enum rseq_percpu_mode
{
110 RSEQ_PERCPU_CPU_ID
= 0,
111 RSEQ_PERCPU_MM_CID
= 1,
114 #define rseq_likely(x) __builtin_expect(!!(x), 1)
115 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
116 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
118 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
119 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
120 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
122 #define __rseq_str_1(x) #x
123 #define __rseq_str(x) __rseq_str_1(x)
125 #define rseq_log(fmt, ...) \
126 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
127 ## __VA_ARGS__, __func__)
129 #define rseq_bug(fmt, ...) \
131 rseq_log(fmt, ## __VA_ARGS__); \
135 #if defined(__x86_64__) || defined(__i386__)
136 #include <rseq/rseq-x86.h>
137 #elif defined(__ARMEL__) || defined(__ARMEB__)
138 #include <rseq/rseq-arm.h>
139 #elif defined (__AARCH64EL__)
140 #include <rseq/rseq-arm64.h>
141 #elif defined(__PPC__)
142 #include <rseq/rseq-ppc.h>
143 #elif defined(__mips__)
144 #include <rseq/rseq-mips.h>
145 #elif defined(__s390__)
146 #include <rseq/rseq-s390.h>
147 #elif defined(__riscv)
148 #include <rseq/rseq-riscv.h>
150 #error unsupported target
158 * Register rseq for the current thread. This needs to be called once
159 * by any thread which uses restartable sequences, before they start
160 * using restartable sequences, to ensure restartable sequences
161 * succeed. A restartable sequence executed from a non-registered
162 * thread will always fail.
164 int rseq_register_current_thread(void);
167 * Unregister rseq for current thread.
169 int rseq_unregister_current_thread(void);
172 * Restartable sequence fallback for reading the current CPU number.
174 int32_t rseq_fallback_current_cpu(void);
177 * Restartable sequence fallback for reading the current node number.
179 int32_t rseq_fallback_current_node(void);
181 enum rseq_available_query
{
182 RSEQ_AVAILABLE_QUERY_KERNEL
= 0,
183 RSEQ_AVAILABLE_QUERY_LIBC
= 1,
187 * Returns true if rseq is supported.
189 bool rseq_available(unsigned int query
);
192 * Values returned can be either the current CPU number, -1 (rseq is
193 * uninitialized), or -2 (rseq initialization has failed).
195 static inline int32_t rseq_current_cpu_raw(void)
197 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id
);
201 * Returns a possible CPU number, which is typically the current CPU.
202 * The returned CPU number can be used to prepare for an rseq critical
203 * section, which will confirm whether the cpu number is indeed the
204 * current one, and whether rseq is initialized.
206 * The CPU number returned by rseq_cpu_start should always be validated
207 * by passing it to a rseq asm sequence, or by comparing it to the
208 * return value of rseq_current_cpu_raw() if the rseq asm sequence
209 * does not need to be invoked.
211 static inline uint32_t rseq_cpu_start(void)
213 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id_start
);
216 static inline uint32_t rseq_current_cpu(void)
220 cpu
= rseq_current_cpu_raw();
221 if (rseq_unlikely(cpu
< 0))
222 cpu
= rseq_fallback_current_cpu();
226 static inline bool rseq_node_id_available(void)
228 return (int) rseq_feature_size
>= (int) rseq_offsetofend(struct rseq_abi
, node_id
);
232 * Current NUMA node number.
234 static inline uint32_t rseq_current_node_id(void)
236 assert(rseq_node_id_available());
237 return RSEQ_READ_ONCE(rseq_get_abi()->node_id
);
240 static inline bool rseq_mm_cid_available(void)
242 return (int) rseq_feature_size
>= (int) rseq_offsetofend(struct rseq_abi
, mm_cid
);
245 static inline uint32_t rseq_current_mm_cid(void)
247 return RSEQ_READ_ONCE(rseq_get_abi()->mm_cid
);
250 static inline void rseq_clear_rseq_cs(void)
252 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs
.arch
.ptr
, 0);
256 * rseq_prepare_unload() should be invoked by each thread executing a rseq
257 * critical section at least once between their last critical section and
258 * library unload of the library defining the rseq critical section (struct
259 * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
260 * post_commit_offset fields. This also applies to use of rseq in code
261 * generated by JIT: rseq_prepare_unload() should be invoked at least once by
262 * each thread executing a rseq critical section before reclaim of the memory
263 * holding the struct rseq_cs or reclaim of the code pointed to by struct
264 * rseq_cs start_ip and post_commit_offset fields.
266 static inline void rseq_prepare_unload(void)
268 rseq_clear_rseq_cs();
271 static inline __attribute__((always_inline
))
272 int rseq_cmpeqv_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
273 intptr_t *v
, intptr_t expect
,
274 intptr_t newv
, int cpu
)
276 if (rseq_mo
!= RSEQ_MO_RELAXED
)
278 switch (percpu_mode
) {
279 case RSEQ_PERCPU_CPU_ID
:
280 return rseq_cmpeqv_storev_relaxed_cpu_id(v
, expect
, newv
, cpu
);
281 case RSEQ_PERCPU_MM_CID
:
282 return rseq_cmpeqv_storev_relaxed_mm_cid(v
, expect
, newv
, cpu
);
289 * Compare @v against @expectnot. When it does _not_ match, load @v
290 * into @load, and store the content of *@v + voffp into @v.
292 static inline __attribute__((always_inline
))
293 int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
294 intptr_t *v
, intptr_t expectnot
, long voffp
, intptr_t *load
,
297 if (rseq_mo
!= RSEQ_MO_RELAXED
)
299 switch (percpu_mode
) {
300 case RSEQ_PERCPU_CPU_ID
:
301 return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v
, expectnot
, voffp
, load
, cpu
);
302 case RSEQ_PERCPU_MM_CID
:
303 return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v
, expectnot
, voffp
, load
, cpu
);
309 static inline __attribute__((always_inline
))
310 int rseq_addv(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
311 intptr_t *v
, intptr_t count
, int cpu
)
313 if (rseq_mo
!= RSEQ_MO_RELAXED
)
315 switch (percpu_mode
) {
316 case RSEQ_PERCPU_CPU_ID
:
317 return rseq_addv_relaxed_cpu_id(v
, count
, cpu
);
318 case RSEQ_PERCPU_MM_CID
:
319 return rseq_addv_relaxed_mm_cid(v
, count
, cpu
);
325 #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
330 static inline __attribute__((always_inline
))
331 int rseq_offset_deref_addv(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
332 intptr_t *ptr
, long off
, intptr_t inc
, int cpu
)
334 if (rseq_mo
!= RSEQ_MO_RELAXED
)
336 switch (percpu_mode
) {
337 case RSEQ_PERCPU_CPU_ID
:
338 return rseq_offset_deref_addv_relaxed_cpu_id(ptr
, off
, inc
, cpu
);
339 case RSEQ_PERCPU_MM_CID
:
340 return rseq_offset_deref_addv_relaxed_mm_cid(ptr
, off
, inc
, cpu
);
347 static inline __attribute__((always_inline
))
348 int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
349 intptr_t *v
, intptr_t expect
,
350 intptr_t *v2
, intptr_t newv2
,
351 intptr_t newv
, int cpu
)
354 case RSEQ_MO_RELAXED
:
355 switch (percpu_mode
) {
356 case RSEQ_PERCPU_CPU_ID
:
357 return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v
, expect
, v2
, newv2
, newv
, cpu
);
358 case RSEQ_PERCPU_MM_CID
:
359 return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v
, expect
, v2
, newv2
, newv
, cpu
);
363 case RSEQ_MO_RELEASE
:
364 switch (percpu_mode
) {
365 case RSEQ_PERCPU_CPU_ID
:
366 return rseq_cmpeqv_trystorev_storev_release_cpu_id(v
, expect
, v2
, newv2
, newv
, cpu
);
367 case RSEQ_PERCPU_MM_CID
:
368 return rseq_cmpeqv_trystorev_storev_release_mm_cid(v
, expect
, v2
, newv2
, newv
, cpu
);
372 case RSEQ_MO_ACQUIRE
: /* Fallthrough */
373 case RSEQ_MO_ACQ_REL
: /* Fallthrough */
374 case RSEQ_MO_CONSUME
: /* Fallthrough */
375 case RSEQ_MO_SEQ_CST
: /* Fallthrough */
381 static inline __attribute__((always_inline
))
382 int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
383 intptr_t *v
, intptr_t expect
,
384 intptr_t *v2
, intptr_t expect2
,
385 intptr_t newv
, int cpu
)
387 if (rseq_mo
!= RSEQ_MO_RELAXED
)
389 switch (percpu_mode
) {
390 case RSEQ_PERCPU_CPU_ID
:
391 return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v
, expect
, v2
, expect2
, newv
, cpu
);
392 case RSEQ_PERCPU_MM_CID
:
393 return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v
, expect
, v2
, expect2
, newv
, cpu
);
399 static inline __attribute__((always_inline
))
400 int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo
, enum rseq_percpu_mode percpu_mode
,
401 intptr_t *v
, intptr_t expect
,
402 void *dst
, void *src
, size_t len
,
403 intptr_t newv
, int cpu
)
406 case RSEQ_MO_RELAXED
:
407 switch (percpu_mode
) {
408 case RSEQ_PERCPU_CPU_ID
:
409 return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v
, expect
, dst
, src
, len
, newv
, cpu
);
410 case RSEQ_PERCPU_MM_CID
:
411 return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v
, expect
, dst
, src
, len
, newv
, cpu
);
415 case RSEQ_MO_RELEASE
:
416 switch (percpu_mode
) {
417 case RSEQ_PERCPU_CPU_ID
:
418 return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v
, expect
, dst
, src
, len
, newv
, cpu
);
419 case RSEQ_PERCPU_MM_CID
:
420 return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v
, expect
, dst
, src
, len
, newv
, cpu
);
424 case RSEQ_MO_ACQUIRE
: /* Fallthrough */
425 case RSEQ_MO_ACQ_REL
: /* Fallthrough */
426 case RSEQ_MO_CONSUME
: /* Fallthrough */
427 case RSEQ_MO_SEQ_CST
: /* Fallthrough */