1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
5 * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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).
26 #ifndef RSEQ_INJECT_ASM
27 #define RSEQ_INJECT_ASM(n)
31 #define RSEQ_INJECT_C(n)
34 #ifndef RSEQ_INJECT_INPUT
35 #define RSEQ_INJECT_INPUT
38 #ifndef RSEQ_INJECT_CLOBBER
39 #define RSEQ_INJECT_CLOBBER
42 #ifndef RSEQ_INJECT_FAILED
43 #define RSEQ_INJECT_FAILED
46 #include "rseq-thread-pointer.h"
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
52 extern unsigned int rseq_size;
53 /* Flags used during rseq registration. */
54 extern unsigned int rseq_flags;
56 static inline struct rseq_abi *rseq_get_abi(void)
58 return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);
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")
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)
69 #define __rseq_str_1(x) #x
70 #define __rseq_str(x) __rseq_str_1(x)
72 #define rseq_log(fmt, args...) \
73 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
76 #define rseq_bug(fmt, args...) \
78 rseq_log(fmt, ##args); \
82 #if defined(__x86_64__) || defined(__i386__)
84 #elif defined(__ARMEL__)
86 #elif defined (__AARCH64EL__)
87 #include <rseq-arm64.h>
88 #elif defined(__PPC__)
90 #elif defined(__mips__)
91 #include <rseq-mips.h>
92 #elif defined(__s390__)
93 #include <rseq-s390.h>
95 #error unsupported target
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.
105 int rseq_register_current_thread(void);
108 * Unregister rseq for current thread.
110 int rseq_unregister_current_thread(void);
113 * Restartable sequence fallback for reading the current CPU number.
115 int32_t rseq_fallback_current_cpu(void);
118 * Values returned can be either the current CPU number, -1 (rseq is
119 * uninitialized), or -2 (rseq initialization has failed).
121 static inline int32_t rseq_current_cpu_raw(void)
123 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
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.
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.
137 static inline uint32_t rseq_cpu_start(void)
139 return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
142 static inline uint32_t rseq_current_cpu(void)
146 cpu = rseq_current_cpu_raw();
147 if (rseq_unlikely(cpu < 0))
148 cpu = rseq_fallback_current_cpu();
152 static inline void rseq_clear_rseq_cs(void)
154 RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
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.
168 static inline void rseq_prepare_unload(void)
170 rseq_clear_rseq_cs();