Update librseq to co-exist with glibc rseq integration
[librseq.git] / include / rseq / rseq.h
1 /* SPDX-License-Identifier: LGPL-2.1-only 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 <stdint.h>
12 #include <stdbool.h>
13 #include <pthread.h>
14 #include <signal.h>
15 #include <sched.h>
16 #include <errno.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <sched.h>
20 #include <linux/rseq.h>
21
22 /*
23 * Empty code injection macros, override when testing.
24 * It is important to consider that the ASM injection macros need to be
25 * fully reentrant (e.g. do not modify the stack).
26 */
27 #ifndef RSEQ_INJECT_ASM
28 #define RSEQ_INJECT_ASM(n)
29 #endif
30
31 #ifndef RSEQ_INJECT_C
32 #define RSEQ_INJECT_C(n)
33 #endif
34
35 #ifndef RSEQ_INJECT_INPUT
36 #define RSEQ_INJECT_INPUT
37 #endif
38
39 #ifndef RSEQ_INJECT_CLOBBER
40 #define RSEQ_INJECT_CLOBBER
41 #endif
42
43 #ifndef RSEQ_INJECT_FAILED
44 #define RSEQ_INJECT_FAILED
45 #endif
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /* Offset from the thread pointer to the rseq area. */
52 extern int rseq_offset;
53 /* Size of the registered rseq area. 0 if the registration was
54 unsuccessful. */
55 extern unsigned int rseq_size;
56 /* Flags used during rseq registration. */
57 extern unsigned int rseq_flags;
58
59 #ifdef __cplusplus
60 }
61 #endif
62
63 #define rseq_likely(x) __builtin_expect(!!(x), 1)
64 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
65 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
66
67 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
68 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
69 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
70
71 #define __rseq_str_1(x) #x
72 #define __rseq_str(x) __rseq_str_1(x)
73
74 #define rseq_log(fmt, args...) \
75 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
76 ## args, __func__)
77
78 #define rseq_bug(fmt, args...) \
79 do { \
80 rseq_log(fmt, ##args); \
81 abort(); \
82 } while (0)
83
84 #if defined(__x86_64__) || defined(__i386__)
85 #include <rseq/rseq-x86-thread-pointer.h>
86 #elif defined(__PPC__)
87 #include <rseq/rseq-ppc-thread-pointer.h>
88 #else
89 /* Use gcc builtin thread pointer. */
90 static inline void *rseq_thread_pointer(void)
91 {
92 return __builtin_thread_pointer();
93 }
94 #endif
95
96 static inline struct rseq *rseq_get_abi(void)
97 {
98 return (struct rseq *) (rseq_thread_pointer() + rseq_offset);
99 }
100
101 #if defined(__x86_64__) || defined(__i386__)
102 #include <rseq/rseq-x86.h>
103 #elif defined(__ARMEL__) || defined(__ARMEB__)
104 #include <rseq/rseq-arm.h>
105 #elif defined (__AARCH64EL__)
106 #include <rseq/rseq-arm64.h>
107 #elif defined(__PPC__)
108 #include <rseq/rseq-ppc.h>
109 #elif defined(__mips__)
110 #include <rseq/rseq-mips.h>
111 #elif defined(__s390__)
112 #include <rseq/rseq-s390.h>
113 #else
114 #error unsupported target
115 #endif
116
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120
121 /*
122 * Register rseq for the current thread. This needs to be called once
123 * by any thread which uses restartable sequences, before they start
124 * using restartable sequences, to ensure restartable sequences
125 * succeed. A restartable sequence executed from a non-registered
126 * thread will always fail.
127 */
128 int rseq_register_current_thread(void);
129
130 /*
131 * Unregister rseq for current thread.
132 */
133 int rseq_unregister_current_thread(void);
134
135 /*
136 * Restartable sequence fallback for reading the current CPU number.
137 */
138 int32_t rseq_fallback_current_cpu(void);
139
140 int rseq_available(void);
141
142 /*
143 * Values returned can be either the current CPU number, -1 (rseq is
144 * uninitialized), or -2 (rseq initialization has failed).
145 */
146 static inline int32_t rseq_current_cpu_raw(void)
147 {
148 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id);
149 }
150
151 /*
152 * Returns a possible CPU number, which is typically the current CPU.
153 * The returned CPU number can be used to prepare for an rseq critical
154 * section, which will confirm whether the cpu number is indeed the
155 * current one, and whether rseq is initialized.
156 *
157 * The CPU number returned by rseq_cpu_start should always be validated
158 * by passing it to a rseq asm sequence, or by comparing it to the
159 * return value of rseq_current_cpu_raw() if the rseq asm sequence
160 * does not need to be invoked.
161 */
162 static inline uint32_t rseq_cpu_start(void)
163 {
164 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id_start);
165 }
166
167 static inline uint32_t rseq_current_cpu(void)
168 {
169 int32_t cpu;
170
171 cpu = rseq_current_cpu_raw();
172 if (rseq_unlikely(cpu < 0))
173 cpu = rseq_fallback_current_cpu();
174 return cpu;
175 }
176
177 static inline void rseq_clear_rseq_cs(void)
178 {
179 #ifdef __LP64__
180 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.ptr, 0);
181 #else
182 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.ptr.ptr32, 0);
183 #endif
184 }
185
186 /*
187 * rseq_prepare_unload() should be invoked by each thread executing a rseq
188 * critical section at least once between their last critical section and
189 * library unload of the library defining the rseq critical section
190 * (struct rseq_cs). This also applies to use of rseq in code generated by
191 * JIT: rseq_prepare_unload() should be invoked at least once by each
192 * thread executing a rseq critical section before reclaim of the memory
193 * holding the struct rseq_cs.
194 */
195 static inline void rseq_prepare_unload(void)
196 {
197 rseq_clear_rseq_cs();
198 }
199
200 #ifdef __cplusplus
201 }
202 #endif
203
204 #endif /* RSEQ_H_ */
This page took 0.043472 seconds and 4 git commands to generate.