1 /* SPDX-License-Identifier: LGPL-2.1-only OR MIT */
5 * (C) Copyright 2016-2022 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20 #include <rseq/rseq-abi.h>
21 #include <rseq/compiler.h>
24 * Empty code injection macros, override when testing.
25 * It is important to consider that the ASM injection macros need to be
26 * fully reentrant (e.g. do not modify the stack).
28 #ifndef RSEQ_INJECT_ASM
29 #define RSEQ_INJECT_ASM(n)
33 #define RSEQ_INJECT_C(n)
36 #ifndef RSEQ_INJECT_INPUT
37 #define RSEQ_INJECT_INPUT
40 #ifndef RSEQ_INJECT_CLOBBER
41 #define RSEQ_INJECT_CLOBBER
44 #ifndef RSEQ_INJECT_FAILED
45 #define RSEQ_INJECT_FAILED
50 * User code can define RSEQ_GET_ABI_OVERRIDE to override the
51 * rseq_get_abi() implementation, for instance to use glibc's symbols
54 #ifndef RSEQ_GET_ABI_OVERRIDE
56 # include <rseq/rseq-thread-pointer.h>
62 /* Offset from the thread pointer to the rseq area. */
63 extern int rseq_offset
;
64 /* Size of the registered rseq area. 0 if the registration was
66 extern unsigned int rseq_size
;
67 /* Flags used during rseq registration. */
68 extern unsigned int rseq_flags
;
70 static inline struct rseq_abi
*rseq_get_abi(void)
72 return (struct rseq_abi
*) ((uintptr_t) rseq_thread_pointer() + rseq_offset
);
79 #endif /* RSEQ_GET_ABI_OVERRIDE */
81 #define rseq_likely(x) __builtin_expect(!!(x), 1)
82 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
83 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
85 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
86 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
87 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
89 #define __rseq_str_1(x) #x
90 #define __rseq_str(x) __rseq_str_1(x)
92 #define rseq_log(fmt, args...) \
93 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
96 #define rseq_bug(fmt, args...) \
98 rseq_log(fmt, ##args); \
102 #if defined(__x86_64__) || defined(__i386__)
103 #include <rseq/rseq-x86.h>
104 #elif defined(__ARMEL__) || defined(__ARMEB__)
105 #include <rseq/rseq-arm.h>
106 #elif defined (__AARCH64EL__)
107 #include <rseq/rseq-arm64.h>
108 #elif defined(__PPC__)
109 #include <rseq/rseq-ppc.h>
110 #elif defined(__mips__)
111 #include <rseq/rseq-mips.h>
112 #elif defined(__s390__)
113 #include <rseq/rseq-s390.h>
115 #error unsupported target
123 * Register rseq for the current thread. This needs to be called once
124 * by any thread which uses restartable sequences, before they start
125 * using restartable sequences, to ensure restartable sequences
126 * succeed. A restartable sequence executed from a non-registered
127 * thread will always fail.
129 int rseq_register_current_thread(void);
132 * Unregister rseq for current thread.
134 int rseq_unregister_current_thread(void);
137 * Restartable sequence fallback for reading the current CPU number.
139 int32_t rseq_fallback_current_cpu(void);
141 int rseq_available(void);
144 * Values returned can be either the current CPU number, -1 (rseq is
145 * uninitialized), or -2 (rseq initialization has failed).
147 static inline int32_t rseq_current_cpu_raw(void)
149 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id
);
153 * Returns a possible CPU number, which is typically the current CPU.
154 * The returned CPU number can be used to prepare for an rseq critical
155 * section, which will confirm whether the cpu number is indeed the
156 * current one, and whether rseq is initialized.
158 * The CPU number returned by rseq_cpu_start should always be validated
159 * by passing it to a rseq asm sequence, or by comparing it to the
160 * return value of rseq_current_cpu_raw() if the rseq asm sequence
161 * does not need to be invoked.
163 static inline uint32_t rseq_cpu_start(void)
165 return RSEQ_READ_ONCE(rseq_get_abi()->cpu_id_start
);
168 static inline uint32_t rseq_current_cpu(void)
172 cpu
= rseq_current_cpu_raw();
173 if (rseq_unlikely(cpu
< 0))
174 cpu
= rseq_fallback_current_cpu();
178 static inline void rseq_clear_rseq_cs(void)
180 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs
.arch
.ptr
, 0);
184 * rseq_prepare_unload() should be invoked by each thread executing a rseq
185 * critical section at least once between their last critical section and
186 * library unload of the library defining the rseq critical section
187 * (struct rseq_ab_cs). This also applies to use of rseq in code generated by
188 * JIT: rseq_prepare_unload() should be invoked at least once by each
189 * thread executing a rseq critical section before reclaim of the memory
190 * holding the struct rseq_abi_cs.
192 static inline void rseq_prepare_unload(void)
194 rseq_clear_rseq_cs();
This page took 0.042626 seconds and 5 git commands to generate.