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