Commit | Line | Data |
---|---|---|
744d0b8b | 1 | /* SPDX-License-Identifier: LGPL-2.1-only OR MIT */ |
784b0012 MD |
2 | /* |
3 | * rseq.h | |
4 | * | |
8d8a94eb | 5 | * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
784b0012 MD |
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> | |
170f840b | 20 | #include <stddef.h> |
2d533093 | 21 | #include <rseq/rseq-abi.h> |
dd76f2d6 | 22 | #include <rseq/compiler.h> |
784b0012 MD |
23 | |
24 | /* | |
25 | * Empty code injection macros, override when testing. | |
26 | * It is important to consider that the ASM injection macros need to be | |
27 | * fully reentrant (e.g. do not modify the stack). | |
28 | */ | |
29 | #ifndef RSEQ_INJECT_ASM | |
30 | #define RSEQ_INJECT_ASM(n) | |
31 | #endif | |
32 | ||
33 | #ifndef RSEQ_INJECT_C | |
34 | #define RSEQ_INJECT_C(n) | |
35 | #endif | |
36 | ||
37 | #ifndef RSEQ_INJECT_INPUT | |
38 | #define RSEQ_INJECT_INPUT | |
39 | #endif | |
40 | ||
41 | #ifndef RSEQ_INJECT_CLOBBER | |
42 | #define RSEQ_INJECT_CLOBBER | |
43 | #endif | |
44 | ||
45 | #ifndef RSEQ_INJECT_FAILED | |
46 | #define RSEQ_INJECT_FAILED | |
47 | #endif | |
48 | ||
96b6ce39 MD |
49 | |
50 | /* | |
51 | * User code can define RSEQ_GET_ABI_OVERRIDE to override the | |
52 | * rseq_get_abi() implementation, for instance to use glibc's symbols | |
53 | * directly. | |
54 | */ | |
55 | #ifndef RSEQ_GET_ABI_OVERRIDE | |
56 | ||
57 | # include <rseq/rseq-thread-pointer.h> | |
58 | ||
59 | # ifdef __cplusplus | |
60a27517 | 60 | extern "C" { |
96b6ce39 | 61 | # endif |
60a27517 | 62 | |
9698c399 | 63 | /* Offset from the thread pointer to the rseq area. */ |
170f840b | 64 | extern ptrdiff_t rseq_offset; |
9698c399 MD |
65 | /* Size of the registered rseq area. 0 if the registration was |
66 | unsuccessful. */ | |
67 | extern unsigned int rseq_size; | |
68 | /* Flags used during rseq registration. */ | |
69 | extern unsigned int rseq_flags; | |
784b0012 | 70 | |
2d533093 | 71 | static inline struct rseq_abi *rseq_get_abi(void) |
96b6ce39 | 72 | { |
2d533093 | 73 | return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset); |
60a27517 | 74 | } |
96b6ce39 MD |
75 | |
76 | # ifdef __cplusplus | |
77 | } | |
78 | # endif | |
79 | ||
80 | #endif /* RSEQ_GET_ABI_OVERRIDE */ | |
60a27517 | 81 | |
784b0012 MD |
82 | #define rseq_likely(x) __builtin_expect(!!(x), 1) |
83 | #define rseq_unlikely(x) __builtin_expect(!!(x), 0) | |
84 | #define rseq_barrier() __asm__ __volatile__("" : : : "memory") | |
85 | ||
86 | #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x)) | |
87 | #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); }) | |
88 | #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x) | |
89 | ||
90 | #define __rseq_str_1(x) #x | |
91 | #define __rseq_str(x) __rseq_str_1(x) | |
92 | ||
93 | #define rseq_log(fmt, args...) \ | |
94 | fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \ | |
95 | ## args, __func__) | |
96 | ||
97 | #define rseq_bug(fmt, args...) \ | |
98 | do { \ | |
99 | rseq_log(fmt, ##args); \ | |
100 | abort(); \ | |
101 | } while (0) | |
102 | ||
103 | #if defined(__x86_64__) || defined(__i386__) | |
104 | #include <rseq/rseq-x86.h> | |
510fdec0 | 105 | #elif defined(__ARMEL__) || defined(__ARMEB__) |
784b0012 | 106 | #include <rseq/rseq-arm.h> |
d78a16c2 MD |
107 | #elif defined (__AARCH64EL__) |
108 | #include <rseq/rseq-arm64.h> | |
784b0012 MD |
109 | #elif defined(__PPC__) |
110 | #include <rseq/rseq-ppc.h> | |
111 | #elif defined(__mips__) | |
112 | #include <rseq/rseq-mips.h> | |
4969e3fa MD |
113 | #elif defined(__s390__) |
114 | #include <rseq/rseq-s390.h> | |
074b1077 MJ |
115 | #elif defined(__riscv) |
116 | #include <rseq/rseq-riscv.h> | |
784b0012 MD |
117 | #else |
118 | #error unsupported target | |
119 | #endif | |
120 | ||
15260018 | 121 | #ifdef __cplusplus |
60a27517 MG |
122 | extern "C" { |
123 | #endif | |
124 | ||
784b0012 MD |
125 | /* |
126 | * Register rseq for the current thread. This needs to be called once | |
127 | * by any thread which uses restartable sequences, before they start | |
128 | * using restartable sequences, to ensure restartable sequences | |
129 | * succeed. A restartable sequence executed from a non-registered | |
130 | * thread will always fail. | |
131 | */ | |
132 | int rseq_register_current_thread(void); | |
133 | ||
134 | /* | |
135 | * Unregister rseq for current thread. | |
136 | */ | |
137 | int rseq_unregister_current_thread(void); | |
138 | ||
139 | /* | |
140 | * Restartable sequence fallback for reading the current CPU number. | |
141 | */ | |
142 | int32_t rseq_fallback_current_cpu(void); | |
143 | ||
8b34114a MD |
144 | enum rseq_available_query { |
145 | RSEQ_AVAILABLE_QUERY_KERNEL = 0, | |
146 | RSEQ_AVAILABLE_QUERY_LIBC = 1, | |
147 | }; | |
148 | ||
149 | /* | |
150 | * Returns true if rseq is supported. | |
151 | */ | |
152 | bool rseq_available(unsigned int query); | |
52e82b87 | 153 | |
784b0012 MD |
154 | /* |
155 | * Values returned can be either the current CPU number, -1 (rseq is | |
156 | * uninitialized), or -2 (rseq initialization has failed). | |
157 | */ | |
158 | static inline int32_t rseq_current_cpu_raw(void) | |
159 | { | |
9698c399 | 160 | return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id); |
784b0012 MD |
161 | } |
162 | ||
163 | /* | |
164 | * Returns a possible CPU number, which is typically the current CPU. | |
165 | * The returned CPU number can be used to prepare for an rseq critical | |
166 | * section, which will confirm whether the cpu number is indeed the | |
167 | * current one, and whether rseq is initialized. | |
168 | * | |
169 | * The CPU number returned by rseq_cpu_start should always be validated | |
170 | * by passing it to a rseq asm sequence, or by comparing it to the | |
171 | * return value of rseq_current_cpu_raw() if the rseq asm sequence | |
172 | * does not need to be invoked. | |
173 | */ | |
174 | static inline uint32_t rseq_cpu_start(void) | |
175 | { | |
9698c399 | 176 | return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id_start); |
784b0012 MD |
177 | } |
178 | ||
179 | static inline uint32_t rseq_current_cpu(void) | |
180 | { | |
181 | int32_t cpu; | |
182 | ||
183 | cpu = rseq_current_cpu_raw(); | |
184 | if (rseq_unlikely(cpu < 0)) | |
185 | cpu = rseq_fallback_current_cpu(); | |
186 | return cpu; | |
187 | } | |
188 | ||
189 | static inline void rseq_clear_rseq_cs(void) | |
190 | { | |
2d533093 | 191 | RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0); |
784b0012 MD |
192 | } |
193 | ||
194 | /* | |
195 | * rseq_prepare_unload() should be invoked by each thread executing a rseq | |
196 | * critical section at least once between their last critical section and | |
197 | * library unload of the library defining the rseq critical section | |
2d533093 | 198 | * (struct rseq_ab_cs). This also applies to use of rseq in code generated by |
784b0012 MD |
199 | * JIT: rseq_prepare_unload() should be invoked at least once by each |
200 | * thread executing a rseq critical section before reclaim of the memory | |
2d533093 | 201 | * holding the struct rseq_abi_cs. |
784b0012 MD |
202 | */ |
203 | static inline void rseq_prepare_unload(void) | |
204 | { | |
205 | rseq_clear_rseq_cs(); | |
206 | } | |
207 | ||
15260018 | 208 | #ifdef __cplusplus |
60a27517 MG |
209 | } |
210 | #endif | |
211 | ||
784b0012 | 212 | #endif /* RSEQ_H_ */ |