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