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