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