selftests/rseq: Uplift rseq selftests for compatibility with glibc-2.35
[linux-2.6-microblaze.git] / tools / testing / selftests / rseq / rseq.h
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 "rseq-abi.h"
20
21 /*
22  * Empty code injection macros, override when testing.
23  * It is important to consider that the ASM injection macros need to be
24  * fully reentrant (e.g. do not modify the stack).
25  */
26 #ifndef RSEQ_INJECT_ASM
27 #define RSEQ_INJECT_ASM(n)
28 #endif
29
30 #ifndef RSEQ_INJECT_C
31 #define RSEQ_INJECT_C(n)
32 #endif
33
34 #ifndef RSEQ_INJECT_INPUT
35 #define RSEQ_INJECT_INPUT
36 #endif
37
38 #ifndef RSEQ_INJECT_CLOBBER
39 #define RSEQ_INJECT_CLOBBER
40 #endif
41
42 #ifndef RSEQ_INJECT_FAILED
43 #define RSEQ_INJECT_FAILED
44 #endif
45
46 #include "rseq-thread-pointer.h"
47
48 /* Offset from the thread pointer to the rseq area.  */
49 extern int rseq_offset;
50 /* Size of the registered rseq area.  0 if the registration was
51    unsuccessful.  */
52 extern unsigned int rseq_size;
53 /* Flags used during rseq registration.  */
54 extern unsigned int rseq_flags;
55
56 static inline struct rseq_abi *rseq_get_abi(void)
57 {
58         return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);
59 }
60
61 #define rseq_likely(x)          __builtin_expect(!!(x), 1)
62 #define rseq_unlikely(x)        __builtin_expect(!!(x), 0)
63 #define rseq_barrier()          __asm__ __volatile__("" : : : "memory")
64
65 #define RSEQ_ACCESS_ONCE(x)     (*(__volatile__  __typeof__(x) *)&(x))
66 #define RSEQ_WRITE_ONCE(x, v)   __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
67 #define RSEQ_READ_ONCE(x)       RSEQ_ACCESS_ONCE(x)
68
69 #define __rseq_str_1(x) #x
70 #define __rseq_str(x)           __rseq_str_1(x)
71
72 #define rseq_log(fmt, args...)                                                 \
73         fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
74                 ## args, __func__)
75
76 #define rseq_bug(fmt, args...)          \
77         do {                            \
78                 rseq_log(fmt, ##args);  \
79                 abort();                \
80         } while (0)
81
82 #if defined(__x86_64__) || defined(__i386__)
83 #include <rseq-x86.h>
84 #elif defined(__ARMEL__)
85 #include <rseq-arm.h>
86 #elif defined (__AARCH64EL__)
87 #include <rseq-arm64.h>
88 #elif defined(__PPC__)
89 #include <rseq-ppc.h>
90 #elif defined(__mips__)
91 #include <rseq-mips.h>
92 #elif defined(__s390__)
93 #include <rseq-s390.h>
94 #else
95 #error unsupported target
96 #endif
97
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
117 /*
118  * Values returned can be either the current CPU number, -1 (rseq is
119  * uninitialized), or -2 (rseq initialization has failed).
120  */
121 static inline int32_t rseq_current_cpu_raw(void)
122 {
123         return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
124 }
125
126 /*
127  * Returns a possible CPU number, which is typically the current CPU.
128  * The returned CPU number can be used to prepare for an rseq critical
129  * section, which will confirm whether the cpu number is indeed the
130  * current one, and whether rseq is initialized.
131  *
132  * The CPU number returned by rseq_cpu_start should always be validated
133  * by passing it to a rseq asm sequence, or by comparing it to the
134  * return value of rseq_current_cpu_raw() if the rseq asm sequence
135  * does not need to be invoked.
136  */
137 static inline uint32_t rseq_cpu_start(void)
138 {
139         return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
140 }
141
142 static inline uint32_t rseq_current_cpu(void)
143 {
144         int32_t cpu;
145
146         cpu = rseq_current_cpu_raw();
147         if (rseq_unlikely(cpu < 0))
148                 cpu = rseq_fallback_current_cpu();
149         return cpu;
150 }
151
152 static inline void rseq_clear_rseq_cs(void)
153 {
154         RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
155 }
156
157 /*
158  * rseq_prepare_unload() should be invoked by each thread executing a rseq
159  * critical section at least once between their last critical section and
160  * library unload of the library defining the rseq critical section (struct
161  * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
162  * post_commit_offset fields. This also applies to use of rseq in code
163  * generated by JIT: rseq_prepare_unload() should be invoked at least once by
164  * each thread executing a rseq critical section before reclaim of the memory
165  * holding the struct rseq_cs or reclaim of the code pointed to by struct
166  * rseq_cs start_ip and post_commit_offset fields.
167  */
168 static inline void rseq_prepare_unload(void)
169 {
170         rseq_clear_rseq_cs();
171 }
172
173 #endif  /* RSEQ_H_ */