]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - tools/testing/selftests/rseq/rseq.h
1 /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
5 * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
19 #include <linux/rseq.h>
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 extern __thread
volatile struct rseq __rseq_abi
;
48 #define rseq_likely(x) __builtin_expect(!!(x), 1)
49 #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
50 #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
52 #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
53 #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
54 #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
56 #define __rseq_str_1(x) #x
57 #define __rseq_str(x) __rseq_str_1(x)
59 #define rseq_log(fmt, args...) \
60 fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
63 #define rseq_bug(fmt, args...) \
65 rseq_log(fmt, ##args); \
69 #if defined(__x86_64__) || defined(__i386__)
71 #elif defined(__ARMEL__)
73 #elif defined (__AARCH64EL__)
74 #include <rseq-arm64.h>
75 #elif defined(__PPC__)
77 #elif defined(__mips__)
78 #include <rseq-mips.h>
79 #elif defined(__s390__)
80 #include <rseq-s390.h>
82 #error unsupported target
86 * Register rseq for the current thread. This needs to be called once
87 * by any thread which uses restartable sequences, before they start
88 * using restartable sequences, to ensure restartable sequences
89 * succeed. A restartable sequence executed from a non-registered
90 * thread will always fail.
92 int rseq_register_current_thread(void);
95 * Unregister rseq for current thread.
97 int rseq_unregister_current_thread(void);
100 * Restartable sequence fallback for reading the current CPU number.
102 int32_t rseq_fallback_current_cpu(void);
105 * Values returned can be either the current CPU number, -1 (rseq is
106 * uninitialized), or -2 (rseq initialization has failed).
108 static inline int32_t rseq_current_cpu_raw(void)
110 return RSEQ_ACCESS_ONCE(__rseq_abi
.cpu_id
);
114 * Returns a possible CPU number, which is typically the current CPU.
115 * The returned CPU number can be used to prepare for an rseq critical
116 * section, which will confirm whether the cpu number is indeed the
117 * current one, and whether rseq is initialized.
119 * The CPU number returned by rseq_cpu_start should always be validated
120 * by passing it to a rseq asm sequence, or by comparing it to the
121 * return value of rseq_current_cpu_raw() if the rseq asm sequence
122 * does not need to be invoked.
124 static inline uint32_t rseq_cpu_start(void)
126 return RSEQ_ACCESS_ONCE(__rseq_abi
.cpu_id_start
);
129 static inline uint32_t rseq_current_cpu(void)
133 cpu
= rseq_current_cpu_raw();
134 if (rseq_unlikely(cpu
< 0))
135 cpu
= rseq_fallback_current_cpu();
139 static inline void rseq_clear_rseq_cs(void)
142 __rseq_abi
.rseq_cs
.ptr
= 0;
144 __rseq_abi
.rseq_cs
.ptr
.ptr32
= 0;
149 * rseq_prepare_unload() should be invoked by each thread executing a rseq
150 * critical section at least once between their last critical section and
151 * library unload of the library defining the rseq critical section
152 * (struct rseq_cs). This also applies to use of rseq in code generated by
153 * JIT: rseq_prepare_unload() should be invoked at least once by each
154 * thread executing a rseq critical section before reclaim of the memory
155 * holding the struct rseq_cs.
157 static inline void rseq_prepare_unload(void)
159 rseq_clear_rseq_cs();