Commit | Line | Data |
---|---|---|
744d0b8b | 1 | /* SPDX-License-Identifier: LGPL-2.1-only OR MIT */ |
784b0012 MD |
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 | ||
15260018 | 47 | #ifdef __cplusplus |
60a27517 MG |
48 | extern "C" { |
49 | #endif | |
50 | ||
78fd8175 | 51 | extern __thread struct rseq __rseq_abi; |
784b0012 | 52 | |
15260018 | 53 | #ifdef __cplusplus |
60a27517 MG |
54 | } |
55 | #endif | |
56 | ||
784b0012 MD |
57 | #define rseq_likely(x) __builtin_expect(!!(x), 1) |
58 | #define rseq_unlikely(x) __builtin_expect(!!(x), 0) | |
59 | #define rseq_barrier() __asm__ __volatile__("" : : : "memory") | |
60 | ||
61 | #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x)) | |
62 | #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); }) | |
63 | #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x) | |
64 | ||
65 | #define __rseq_str_1(x) #x | |
66 | #define __rseq_str(x) __rseq_str_1(x) | |
67 | ||
68 | #define rseq_log(fmt, args...) \ | |
69 | fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \ | |
70 | ## args, __func__) | |
71 | ||
72 | #define rseq_bug(fmt, args...) \ | |
73 | do { \ | |
74 | rseq_log(fmt, ##args); \ | |
75 | abort(); \ | |
76 | } while (0) | |
77 | ||
78 | #if defined(__x86_64__) || defined(__i386__) | |
79 | #include <rseq/rseq-x86.h> | |
510fdec0 | 80 | #elif defined(__ARMEL__) || defined(__ARMEB__) |
784b0012 | 81 | #include <rseq/rseq-arm.h> |
d78a16c2 MD |
82 | #elif defined (__AARCH64EL__) |
83 | #include <rseq/rseq-arm64.h> | |
784b0012 MD |
84 | #elif defined(__PPC__) |
85 | #include <rseq/rseq-ppc.h> | |
86 | #elif defined(__mips__) | |
87 | #include <rseq/rseq-mips.h> | |
4969e3fa MD |
88 | #elif defined(__s390__) |
89 | #include <rseq/rseq-s390.h> | |
784b0012 MD |
90 | #else |
91 | #error unsupported target | |
92 | #endif | |
93 | ||
15260018 | 94 | #ifdef __cplusplus |
60a27517 MG |
95 | extern "C" { |
96 | #endif | |
97 | ||
784b0012 MD |
98 | /* |
99 | * Register rseq for the current thread. This needs to be called once | |
100 | * by any thread which uses restartable sequences, before they start | |
101 | * using restartable sequences, to ensure restartable sequences | |
102 | * succeed. A restartable sequence executed from a non-registered | |
103 | * thread will always fail. | |
104 | */ | |
105 | int rseq_register_current_thread(void); | |
106 | ||
107 | /* | |
108 | * Unregister rseq for current thread. | |
109 | */ | |
110 | int rseq_unregister_current_thread(void); | |
111 | ||
112 | /* | |
113 | * Restartable sequence fallback for reading the current CPU number. | |
114 | */ | |
115 | int32_t rseq_fallback_current_cpu(void); | |
116 | ||
52e82b87 MD |
117 | int rseq_available(void); |
118 | ||
784b0012 MD |
119 | /* |
120 | * Values returned can be either the current CPU number, -1 (rseq is | |
121 | * uninitialized), or -2 (rseq initialization has failed). | |
122 | */ | |
123 | static inline int32_t rseq_current_cpu_raw(void) | |
124 | { | |
78fd8175 | 125 | return RSEQ_READ_ONCE(__rseq_abi.cpu_id); |
784b0012 MD |
126 | } |
127 | ||
128 | /* | |
129 | * Returns a possible CPU number, which is typically the current CPU. | |
130 | * The returned CPU number can be used to prepare for an rseq critical | |
131 | * section, which will confirm whether the cpu number is indeed the | |
132 | * current one, and whether rseq is initialized. | |
133 | * | |
134 | * The CPU number returned by rseq_cpu_start should always be validated | |
135 | * by passing it to a rseq asm sequence, or by comparing it to the | |
136 | * return value of rseq_current_cpu_raw() if the rseq asm sequence | |
137 | * does not need to be invoked. | |
138 | */ | |
139 | static inline uint32_t rseq_cpu_start(void) | |
140 | { | |
78fd8175 | 141 | return RSEQ_READ_ONCE(__rseq_abi.cpu_id_start); |
784b0012 MD |
142 | } |
143 | ||
144 | static inline uint32_t rseq_current_cpu(void) | |
145 | { | |
146 | int32_t cpu; | |
147 | ||
148 | cpu = rseq_current_cpu_raw(); | |
149 | if (rseq_unlikely(cpu < 0)) | |
150 | cpu = rseq_fallback_current_cpu(); | |
151 | return cpu; | |
152 | } | |
153 | ||
154 | static inline void rseq_clear_rseq_cs(void) | |
155 | { | |
156 | #ifdef __LP64__ | |
78fd8175 | 157 | RSEQ_WRITE_ONCE(__rseq_abi.rseq_cs.ptr, 0); |
784b0012 | 158 | #else |
78fd8175 | 159 | RSEQ_WRITE_ONCE(__rseq_abi.rseq_cs.ptr.ptr32, 0); |
784b0012 MD |
160 | #endif |
161 | } | |
162 | ||
163 | /* | |
164 | * rseq_prepare_unload() should be invoked by each thread executing a rseq | |
165 | * critical section at least once between their last critical section and | |
166 | * library unload of the library defining the rseq critical section | |
167 | * (struct rseq_cs). This also applies to use of rseq in code generated by | |
168 | * JIT: rseq_prepare_unload() should be invoked at least once by each | |
169 | * thread executing a rseq critical section before reclaim of the memory | |
170 | * holding the struct rseq_cs. | |
171 | */ | |
172 | static inline void rseq_prepare_unload(void) | |
173 | { | |
174 | rseq_clear_rseq_cs(); | |
175 | } | |
176 | ||
15260018 | 177 | #ifdef __cplusplus |
60a27517 MG |
178 | } |
179 | #endif | |
180 | ||
784b0012 | 181 | #endif /* RSEQ_H_ */ |