1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2 /*
3 * rseq.h
4 *
5 * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #ifndef RSEQ_H
9 #define RSEQ_H
10
11 #include <assert.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <pthread.h>
15 #include <signal.h>
16 #include <sched.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include "rseq-abi.h"
22 #include "compiler.h"
23
24 #ifndef rseq_sizeof_field
25 #define rseq_sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
26 #endif
27
28 #ifndef rseq_offsetofend
29 #define rseq_offsetofend(TYPE, MEMBER) \
30 (offsetof(TYPE, MEMBER) + rseq_sizeof_field(TYPE, MEMBER))
31 #endif
32
33 /*
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).
37 */
38 #ifndef RSEQ_INJECT_ASM
39 #define RSEQ_INJECT_ASM(n)
40 #endif
41
42 #ifndef RSEQ_INJECT_C
43 #define RSEQ_INJECT_C(n)
44 #endif
45
46 #ifndef RSEQ_INJECT_INPUT
47 #define RSEQ_INJECT_INPUT
48 #endif
49
50 #ifndef RSEQ_INJECT_CLOBBER
51 #define RSEQ_INJECT_CLOBBER
52 #endif
53
54 #ifndef RSEQ_INJECT_FAILED
55 #define RSEQ_INJECT_FAILED
56 #endif
57
58 #include "rseq-thread-pointer.h"
59
60 /* Offset from the thread pointer to the rseq area. */
61 extern ptrdiff_t rseq_offset;
62
63 /*
64 * The rseq ABI is composed of extensible feature fields. The extensions
65 * are done by appending additional fields at the end of the structure.
66 * The rseq_size defines the size of the active feature set which can be
67 * used by the application for the current rseq registration. Features
68 * starting at offset >= rseq_size are inactive and should not be used.
69 *
70 * The rseq_size is the intersection between the available allocation
71 * size for the rseq area and the feature size supported by the kernel.
72 * unsuccessful.
73 */
74 extern unsigned int rseq_size;
75
76 /* Flags used during rseq registration. */
77 extern unsigned int rseq_flags;
78
79 enum rseq_mo {
80 RSEQ_MO_RELAXED = 0,
81 RSEQ_MO_CONSUME = 1, /* Unused */
82 RSEQ_MO_ACQUIRE = 2, /* Unused */
83 RSEQ_MO_RELEASE = 3,
84 RSEQ_MO_ACQ_REL = 4, /* Unused */
85 RSEQ_MO_SEQ_CST = 5, /* Unused */
86 };
87
88 enum rseq_percpu_mode {
89 RSEQ_PERCPU_CPU_ID = 0,
90 RSEQ_PERCPU_MM_CID = 1,
91 };
92
rseq_get_abi(void)93 static inline struct rseq_abi *rseq_get_abi(void)
94 {
95 return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);
96 }
97
98 #define rseq_likely(x) __builtin_expect(!!(x), 1)
99 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
100 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
101
102 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
103 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
104 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
105
106 #define __rseq_str_1(x) #x
107 #define __rseq_str(x) __rseq_str_1(x)
108
109 #define rseq_log(fmt, args...) \
110 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
111 ## args, __func__)
112
113 #define rseq_bug(fmt, args...) \
114 do { \
115 rseq_log(fmt, ##args); \
116 abort(); \
117 } while (0)
118
119 #if defined(__x86_64__) || defined(__i386__)
120 #include <rseq-x86.h>
121 #elif defined(__ARMEL__)
122 #include <rseq-arm.h>
123 #elif defined (__AARCH64EL__)
124 #include <rseq-arm64.h>
125 #elif defined(__PPC__)
126 #include <rseq-ppc.h>
127 #elif defined(__mips__)
128 #include <rseq-mips.h>
129 #elif defined(__s390__)
130 #include <rseq-s390.h>
131 #elif defined(__riscv)
132 #include <rseq-riscv.h>
133 #elif defined(__or1k__)
134 #include <rseq-or1k.h>
135 #else
136 #error unsupported target
137 #endif
138
139 /*
140 * Register rseq for the current thread. This needs to be called once
141 * by any thread which uses restartable sequences, before they start
142 * using restartable sequences, to ensure restartable sequences
143 * succeed. A restartable sequence executed from a non-registered
144 * thread will always fail.
145 */
146 int __rseq_register_current_thread(bool nolibc, bool legacy);
147
rseq_register_current_thread(void)148 static inline int rseq_register_current_thread(void)
149 {
150 return __rseq_register_current_thread(false, false);
151 }
152
153 /*
154 * Unregister rseq for current thread.
155 */
156 int rseq_unregister_current_thread(void);
157
158 /*
159 * Restartable sequence fallback for reading the current CPU number.
160 */
161 int32_t rseq_fallback_current_cpu(void);
162
163 /*
164 * Restartable sequence fallback for reading the current node number.
165 */
166 int32_t rseq_fallback_current_node(void);
167
168 /*
169 * Returns true if rseq is supported.
170 */
171 bool rseq_available(void);
172
173 /*
174 * Values returned can be either the current CPU number, -1 (rseq is
175 * uninitialized), or -2 (rseq initialization has failed).
176 */
rseq_current_cpu_raw(void)177 static inline int32_t rseq_current_cpu_raw(void)
178 {
179 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
180 }
181
182 /*
183 * Returns a possible CPU number, which is typically the current CPU.
184 * The returned CPU number can be used to prepare for an rseq critical
185 * section, which will confirm whether the cpu number is indeed the
186 * current one, and whether rseq is initialized.
187 *
188 * The CPU number returned by rseq_cpu_start should always be validated
189 * by passing it to a rseq asm sequence, or by comparing it to the
190 * return value of rseq_current_cpu_raw() if the rseq asm sequence
191 * does not need to be invoked.
192 */
rseq_cpu_start(void)193 static inline uint32_t rseq_cpu_start(void)
194 {
195 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
196 }
197
rseq_current_cpu(void)198 static inline uint32_t rseq_current_cpu(void)
199 {
200 int32_t cpu;
201
202 cpu = rseq_current_cpu_raw();
203 if (rseq_unlikely(cpu < 0))
204 cpu = rseq_fallback_current_cpu();
205 return cpu;
206 }
207
rseq_node_id_available(void)208 static inline bool rseq_node_id_available(void)
209 {
210 return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, node_id);
211 }
212
213 /*
214 * Current NUMA node number.
215 */
rseq_current_node_id(void)216 static inline uint32_t rseq_current_node_id(void)
217 {
218 assert(rseq_node_id_available());
219 return RSEQ_ACCESS_ONCE(rseq_get_abi()->node_id);
220 }
221
rseq_mm_cid_available(void)222 static inline bool rseq_mm_cid_available(void)
223 {
224 return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, mm_cid);
225 }
226
rseq_current_mm_cid(void)227 static inline uint32_t rseq_current_mm_cid(void)
228 {
229 return RSEQ_ACCESS_ONCE(rseq_get_abi()->mm_cid);
230 }
231
rseq_clear_rseq_cs(void)232 static inline void rseq_clear_rseq_cs(void)
233 {
234 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
235 }
236
237 /*
238 * rseq_prepare_unload() should be invoked by each thread executing a rseq
239 * critical section at least once between their last critical section and
240 * library unload of the library defining the rseq critical section (struct
241 * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
242 * post_commit_offset fields. This also applies to use of rseq in code
243 * generated by JIT: rseq_prepare_unload() should be invoked at least once by
244 * each thread executing a rseq critical section before reclaim of the memory
245 * holding the struct rseq_cs or reclaim of the code pointed to by struct
246 * rseq_cs start_ip and post_commit_offset fields.
247 */
rseq_prepare_unload(void)248 static inline void rseq_prepare_unload(void)
249 {
250 rseq_clear_rseq_cs();
251 }
252
253 static inline __attribute__((always_inline))
rseq_cmpeqv_storev(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t expect,intptr_t newv,int cpu)254 int rseq_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
255 intptr_t *v, intptr_t expect,
256 intptr_t newv, int cpu)
257 {
258 if (rseq_mo != RSEQ_MO_RELAXED)
259 return -1;
260 switch (percpu_mode) {
261 case RSEQ_PERCPU_CPU_ID:
262 return rseq_cmpeqv_storev_relaxed_cpu_id(v, expect, newv, cpu);
263 case RSEQ_PERCPU_MM_CID:
264 return rseq_cmpeqv_storev_relaxed_mm_cid(v, expect, newv, cpu);
265 }
266 return -1;
267 }
268
269 /*
270 * Compare @v against @expectnot. When it does _not_ match, load @v
271 * into @load, and store the content of *@v + voffp into @v.
272 */
273 static inline __attribute__((always_inline))
rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t expectnot,long voffp,intptr_t * load,int cpu)274 int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
275 intptr_t *v, intptr_t expectnot, long voffp, intptr_t *load,
276 int cpu)
277 {
278 if (rseq_mo != RSEQ_MO_RELAXED)
279 return -1;
280 switch (percpu_mode) {
281 case RSEQ_PERCPU_CPU_ID:
282 return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v, expectnot, voffp, load, cpu);
283 case RSEQ_PERCPU_MM_CID:
284 return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v, expectnot, voffp, load, cpu);
285 }
286 return -1;
287 }
288
289 static inline __attribute__((always_inline))
rseq_addv(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t count,int cpu)290 int rseq_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
291 intptr_t *v, intptr_t count, int cpu)
292 {
293 if (rseq_mo != RSEQ_MO_RELAXED)
294 return -1;
295 switch (percpu_mode) {
296 case RSEQ_PERCPU_CPU_ID:
297 return rseq_addv_relaxed_cpu_id(v, count, cpu);
298 case RSEQ_PERCPU_MM_CID:
299 return rseq_addv_relaxed_mm_cid(v, count, cpu);
300 }
301 return -1;
302 }
303
304 #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
305 /*
306 * pval = *(ptr+off)
307 * *pval += inc;
308 */
309 static inline __attribute__((always_inline))
rseq_offset_deref_addv(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * ptr,long off,intptr_t inc,int cpu)310 int rseq_offset_deref_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
311 intptr_t *ptr, long off, intptr_t inc, int cpu)
312 {
313 if (rseq_mo != RSEQ_MO_RELAXED)
314 return -1;
315 switch (percpu_mode) {
316 case RSEQ_PERCPU_CPU_ID:
317 return rseq_offset_deref_addv_relaxed_cpu_id(ptr, off, inc, cpu);
318 case RSEQ_PERCPU_MM_CID:
319 return rseq_offset_deref_addv_relaxed_mm_cid(ptr, off, inc, cpu);
320 }
321 return -1;
322 }
323 #endif
324
325 static inline __attribute__((always_inline))
rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t expect,intptr_t * v2,intptr_t newv2,intptr_t newv,int cpu)326 int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
327 intptr_t *v, intptr_t expect,
328 intptr_t *v2, intptr_t newv2,
329 intptr_t newv, int cpu)
330 {
331 switch (rseq_mo) {
332 case RSEQ_MO_RELAXED:
333 switch (percpu_mode) {
334 case RSEQ_PERCPU_CPU_ID:
335 return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v, expect, v2, newv2, newv, cpu);
336 case RSEQ_PERCPU_MM_CID:
337 return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v, expect, v2, newv2, newv, cpu);
338 }
339 return -1;
340 case RSEQ_MO_RELEASE:
341 switch (percpu_mode) {
342 case RSEQ_PERCPU_CPU_ID:
343 return rseq_cmpeqv_trystorev_storev_release_cpu_id(v, expect, v2, newv2, newv, cpu);
344 case RSEQ_PERCPU_MM_CID:
345 return rseq_cmpeqv_trystorev_storev_release_mm_cid(v, expect, v2, newv2, newv, cpu);
346 }
347 return -1;
348 default:
349 return -1;
350 }
351 }
352
353 static inline __attribute__((always_inline))
rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t expect,intptr_t * v2,intptr_t expect2,intptr_t newv,int cpu)354 int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
355 intptr_t *v, intptr_t expect,
356 intptr_t *v2, intptr_t expect2,
357 intptr_t newv, int cpu)
358 {
359 if (rseq_mo != RSEQ_MO_RELAXED)
360 return -1;
361 switch (percpu_mode) {
362 case RSEQ_PERCPU_CPU_ID:
363 return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v, expect, v2, expect2, newv, cpu);
364 case RSEQ_PERCPU_MM_CID:
365 return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v, expect, v2, expect2, newv, cpu);
366 }
367 return -1;
368 }
369
370 static inline __attribute__((always_inline))
rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo,enum rseq_percpu_mode percpu_mode,intptr_t * v,intptr_t expect,void * dst,void * src,size_t len,intptr_t newv,int cpu)371 int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
372 intptr_t *v, intptr_t expect,
373 void *dst, void *src, size_t len,
374 intptr_t newv, int cpu)
375 {
376 switch (rseq_mo) {
377 case RSEQ_MO_RELAXED:
378 switch (percpu_mode) {
379 case RSEQ_PERCPU_CPU_ID:
380 return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v, expect, dst, src, len, newv, cpu);
381 case RSEQ_PERCPU_MM_CID:
382 return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v, expect, dst, src, len, newv, cpu);
383 }
384 return -1;
385 case RSEQ_MO_RELEASE:
386 switch (percpu_mode) {
387 case RSEQ_PERCPU_CPU_ID:
388 return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v, expect, dst, src, len, newv, cpu);
389 case RSEQ_PERCPU_MM_CID:
390 return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v, expect, dst, src, len, newv, cpu);
391 }
392 return -1;
393 default:
394 return -1;
395 }
396 }
397
398 #endif /* RSEQ_H_ */
399