1*4882a593Smuzhiyun /* SPDX-License-Identifier: LGPL-2.1 OR MIT */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * rseq.h
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * (C) Copyright 2016-2018 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef RSEQ_H
9*4882a593Smuzhiyun #define RSEQ_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <stdint.h>
12*4882a593Smuzhiyun #include <stdbool.h>
13*4882a593Smuzhiyun #include <pthread.h>
14*4882a593Smuzhiyun #include <signal.h>
15*4882a593Smuzhiyun #include <sched.h>
16*4882a593Smuzhiyun #include <errno.h>
17*4882a593Smuzhiyun #include <stdio.h>
18*4882a593Smuzhiyun #include <stdlib.h>
19*4882a593Smuzhiyun #include <stddef.h>
20*4882a593Smuzhiyun #include "rseq-abi.h"
21*4882a593Smuzhiyun #include "compiler.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Empty code injection macros, override when testing.
25*4882a593Smuzhiyun * It is important to consider that the ASM injection macros need to be
26*4882a593Smuzhiyun * fully reentrant (e.g. do not modify the stack).
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun #ifndef RSEQ_INJECT_ASM
29*4882a593Smuzhiyun #define RSEQ_INJECT_ASM(n)
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifndef RSEQ_INJECT_C
33*4882a593Smuzhiyun #define RSEQ_INJECT_C(n)
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #ifndef RSEQ_INJECT_INPUT
37*4882a593Smuzhiyun #define RSEQ_INJECT_INPUT
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #ifndef RSEQ_INJECT_CLOBBER
41*4882a593Smuzhiyun #define RSEQ_INJECT_CLOBBER
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #ifndef RSEQ_INJECT_FAILED
45*4882a593Smuzhiyun #define RSEQ_INJECT_FAILED
46*4882a593Smuzhiyun #endif
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun #include "rseq-thread-pointer.h"
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /* Offset from the thread pointer to the rseq area. */
51*4882a593Smuzhiyun extern ptrdiff_t rseq_offset;
52*4882a593Smuzhiyun /* Size of the registered rseq area. 0 if the registration was
53*4882a593Smuzhiyun unsuccessful. */
54*4882a593Smuzhiyun extern unsigned int rseq_size;
55*4882a593Smuzhiyun /* Flags used during rseq registration. */
56*4882a593Smuzhiyun extern unsigned int rseq_flags;
57*4882a593Smuzhiyun
rseq_get_abi(void)58*4882a593Smuzhiyun static inline struct rseq_abi *rseq_get_abi(void)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun #define rseq_likely(x) __builtin_expect(!!(x), 1)
64*4882a593Smuzhiyun #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
65*4882a593Smuzhiyun #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
68*4882a593Smuzhiyun #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
69*4882a593Smuzhiyun #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #define __rseq_str_1(x) #x
72*4882a593Smuzhiyun #define __rseq_str(x) __rseq_str_1(x)
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun #define rseq_log(fmt, args...) \
75*4882a593Smuzhiyun fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
76*4882a593Smuzhiyun ## args, __func__)
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #define rseq_bug(fmt, args...) \
79*4882a593Smuzhiyun do { \
80*4882a593Smuzhiyun rseq_log(fmt, ##args); \
81*4882a593Smuzhiyun abort(); \
82*4882a593Smuzhiyun } while (0)
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun #if defined(__x86_64__) || defined(__i386__)
85*4882a593Smuzhiyun #include <rseq-x86.h>
86*4882a593Smuzhiyun #elif defined(__ARMEL__)
87*4882a593Smuzhiyun #include <rseq-arm.h>
88*4882a593Smuzhiyun #elif defined (__AARCH64EL__)
89*4882a593Smuzhiyun #include <rseq-arm64.h>
90*4882a593Smuzhiyun #elif defined(__PPC__)
91*4882a593Smuzhiyun #include <rseq-ppc.h>
92*4882a593Smuzhiyun #elif defined(__mips__)
93*4882a593Smuzhiyun #include <rseq-mips.h>
94*4882a593Smuzhiyun #elif defined(__s390__)
95*4882a593Smuzhiyun #include <rseq-s390.h>
96*4882a593Smuzhiyun #else
97*4882a593Smuzhiyun #error unsupported target
98*4882a593Smuzhiyun #endif
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun * Register rseq for the current thread. This needs to be called once
102*4882a593Smuzhiyun * by any thread which uses restartable sequences, before they start
103*4882a593Smuzhiyun * using restartable sequences, to ensure restartable sequences
104*4882a593Smuzhiyun * succeed. A restartable sequence executed from a non-registered
105*4882a593Smuzhiyun * thread will always fail.
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun int rseq_register_current_thread(void);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun * Unregister rseq for current thread.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun int rseq_unregister_current_thread(void);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * Restartable sequence fallback for reading the current CPU number.
116*4882a593Smuzhiyun */
117*4882a593Smuzhiyun int32_t rseq_fallback_current_cpu(void);
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * Values returned can be either the current CPU number, -1 (rseq is
121*4882a593Smuzhiyun * uninitialized), or -2 (rseq initialization has failed).
122*4882a593Smuzhiyun */
rseq_current_cpu_raw(void)123*4882a593Smuzhiyun static inline int32_t rseq_current_cpu_raw(void)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * Returns a possible CPU number, which is typically the current CPU.
130*4882a593Smuzhiyun * The returned CPU number can be used to prepare for an rseq critical
131*4882a593Smuzhiyun * section, which will confirm whether the cpu number is indeed the
132*4882a593Smuzhiyun * current one, and whether rseq is initialized.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * The CPU number returned by rseq_cpu_start should always be validated
135*4882a593Smuzhiyun * by passing it to a rseq asm sequence, or by comparing it to the
136*4882a593Smuzhiyun * return value of rseq_current_cpu_raw() if the rseq asm sequence
137*4882a593Smuzhiyun * does not need to be invoked.
138*4882a593Smuzhiyun */
rseq_cpu_start(void)139*4882a593Smuzhiyun static inline uint32_t rseq_cpu_start(void)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
rseq_current_cpu(void)144*4882a593Smuzhiyun static inline uint32_t rseq_current_cpu(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun int32_t cpu;
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun cpu = rseq_current_cpu_raw();
149*4882a593Smuzhiyun if (rseq_unlikely(cpu < 0))
150*4882a593Smuzhiyun cpu = rseq_fallback_current_cpu();
151*4882a593Smuzhiyun return cpu;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
rseq_clear_rseq_cs(void)154*4882a593Smuzhiyun static inline void rseq_clear_rseq_cs(void)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /*
160*4882a593Smuzhiyun * rseq_prepare_unload() should be invoked by each thread executing a rseq
161*4882a593Smuzhiyun * critical section at least once between their last critical section and
162*4882a593Smuzhiyun * library unload of the library defining the rseq critical section (struct
163*4882a593Smuzhiyun * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
164*4882a593Smuzhiyun * post_commit_offset fields. This also applies to use of rseq in code
165*4882a593Smuzhiyun * generated by JIT: rseq_prepare_unload() should be invoked at least once by
166*4882a593Smuzhiyun * each thread executing a rseq critical section before reclaim of the memory
167*4882a593Smuzhiyun * holding the struct rseq_cs or reclaim of the code pointed to by struct
168*4882a593Smuzhiyun * rseq_cs start_ip and post_commit_offset fields.
169*4882a593Smuzhiyun */
rseq_prepare_unload(void)170*4882a593Smuzhiyun static inline void rseq_prepare_unload(void)
171*4882a593Smuzhiyun {
172*4882a593Smuzhiyun rseq_clear_rseq_cs();
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun #endif /* RSEQ_H_ */
176