1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
5 * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
20 #include <linux/rseq.h>
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).
27 #ifndef RSEQ_INJECT_ASM
28 #define RSEQ_INJECT_ASM(n)
32 #define RSEQ_INJECT_C(n)
35 #ifndef RSEQ_INJECT_INPUT
36 #define RSEQ_INJECT_INPUT
39 #ifndef RSEQ_INJECT_CLOBBER
40 #define RSEQ_INJECT_CLOBBER
43 #ifndef RSEQ_INJECT_FAILED
44 #define RSEQ_INJECT_FAILED
47 extern __thread volatile struct rseq __rseq_abi;
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")
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)
57 #define __rseq_str_1(x) #x
58 #define __rseq_str(x) __rseq_str_1(x)
60 #define rseq_log(fmt, args...) \
61 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
64 #define rseq_bug(fmt, args...) \
66 rseq_log(fmt, ##args); \
70 #if defined(__x86_64__) || defined(__i386__)
72 #elif defined(__ARMEL__)
74 #elif defined(__PPC__)
76 #elif defined(__mips__)
77 #include <rseq-mips.h>
79 #error unsupported target
83 * Register rseq for the current thread. This needs to be called once
84 * by any thread which uses restartable sequences, before they start
85 * using restartable sequences, to ensure restartable sequences
86 * succeed. A restartable sequence executed from a non-registered
87 * thread will always fail.
89 int rseq_register_current_thread(void);
92 * Unregister rseq for current thread.
94 int rseq_unregister_current_thread(void);
97 * Restartable sequence fallback for reading the current CPU number.
99 int32_t rseq_fallback_current_cpu(void);
102 * Values returned can be either the current CPU number, -1 (rseq is
103 * uninitialized), or -2 (rseq initialization has failed).
105 static inline int32_t rseq_current_cpu_raw(void)
107 return RSEQ_ACCESS_ONCE(__rseq_abi.cpu_id);
111 * Returns a possible CPU number, which is typically the current CPU.
112 * The returned CPU number can be used to prepare for an rseq critical
113 * section, which will confirm whether the cpu number is indeed the
114 * current one, and whether rseq is initialized.
116 * The CPU number returned by rseq_cpu_start should always be validated
117 * by passing it to a rseq asm sequence, or by comparing it to the
118 * return value of rseq_current_cpu_raw() if the rseq asm sequence
119 * does not need to be invoked.
121 static inline uint32_t rseq_cpu_start(void)
123 return RSEQ_ACCESS_ONCE(__rseq_abi.cpu_id_start);
126 static inline uint32_t rseq_current_cpu(void)
130 cpu = rseq_current_cpu_raw();
131 if (rseq_unlikely(cpu < 0))
132 cpu = rseq_fallback_current_cpu();
136 static inline void rseq_clear_rseq_cs(void)
139 __rseq_abi.rseq_cs.ptr = 0;
141 __rseq_abi.rseq_cs.ptr.ptr32 = 0;
146 * rseq_prepare_unload() should be invoked by each thread using rseq_finish*()
147 * at least once between their last rseq_finish*() and library unload of the
148 * library defining the rseq critical section (struct rseq_cs). This also
149 * applies to use of rseq in code generated by JIT: rseq_prepare_unload()
150 * should be invoked at least once by each thread using rseq_finish*() before
151 * reclaim of the memory holding the struct rseq_cs.
153 static inline void rseq_prepare_unload(void)
155 rseq_clear_rseq_cs();