rseq.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. #ifndef RSEQ_H
  8. #define RSEQ_H
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. #include <pthread.h>
  12. #include <signal.h>
  13. #include <sched.h>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stddef.h>
  18. #include "rseq-abi.h"
  19. #include "compiler.h"
  20. #ifndef rseq_sizeof_field
  21. #define rseq_sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
  22. #endif
  23. #ifndef rseq_offsetofend
  24. #define rseq_offsetofend(TYPE, MEMBER) \
  25. (offsetof(TYPE, MEMBER) + rseq_sizeof_field(TYPE, MEMBER))
  26. #endif
  27. /*
  28. * Empty code injection macros, override when testing.
  29. * It is important to consider that the ASM injection macros need to be
  30. * fully reentrant (e.g. do not modify the stack).
  31. */
  32. #ifndef RSEQ_INJECT_ASM
  33. #define RSEQ_INJECT_ASM(n)
  34. #endif
  35. #ifndef RSEQ_INJECT_C
  36. #define RSEQ_INJECT_C(n)
  37. #endif
  38. #ifndef RSEQ_INJECT_INPUT
  39. #define RSEQ_INJECT_INPUT
  40. #endif
  41. #ifndef RSEQ_INJECT_CLOBBER
  42. #define RSEQ_INJECT_CLOBBER
  43. #endif
  44. #ifndef RSEQ_INJECT_FAILED
  45. #define RSEQ_INJECT_FAILED
  46. #endif
  47. #include "rseq-thread-pointer.h"
  48. /* Offset from the thread pointer to the rseq area. */
  49. extern ptrdiff_t rseq_offset;
  50. /*
  51. * The rseq ABI is composed of extensible feature fields. The extensions
  52. * are done by appending additional fields at the end of the structure.
  53. * The rseq_size defines the size of the active feature set which can be
  54. * used by the application for the current rseq registration. Features
  55. * starting at offset >= rseq_size are inactive and should not be used.
  56. *
  57. * The rseq_size is the intersection between the available allocation
  58. * size for the rseq area and the feature size supported by the kernel.
  59. * unsuccessful.
  60. */
  61. extern unsigned int rseq_size;
  62. /* Flags used during rseq registration. */
  63. extern unsigned int rseq_flags;
  64. enum rseq_mo {
  65. RSEQ_MO_RELAXED = 0,
  66. RSEQ_MO_CONSUME = 1, /* Unused */
  67. RSEQ_MO_ACQUIRE = 2, /* Unused */
  68. RSEQ_MO_RELEASE = 3,
  69. RSEQ_MO_ACQ_REL = 4, /* Unused */
  70. RSEQ_MO_SEQ_CST = 5, /* Unused */
  71. };
  72. enum rseq_percpu_mode {
  73. RSEQ_PERCPU_CPU_ID = 0,
  74. RSEQ_PERCPU_MM_CID = 1,
  75. };
  76. static inline struct rseq_abi *rseq_get_abi(void)
  77. {
  78. return (struct rseq_abi *) ((uintptr_t) rseq_thread_pointer() + rseq_offset);
  79. }
  80. #define rseq_likely(x) __builtin_expect(!!(x), 1)
  81. #define rseq_unlikely(x) __builtin_expect(!!(x), 0)
  82. #define rseq_barrier() __asm__ __volatile__("" : : : "memory")
  83. #define RSEQ_ACCESS_ONCE(x) (*(__volatile__ __typeof__(x) *)&(x))
  84. #define RSEQ_WRITE_ONCE(x, v) __extension__ ({ RSEQ_ACCESS_ONCE(x) = (v); })
  85. #define RSEQ_READ_ONCE(x) RSEQ_ACCESS_ONCE(x)
  86. #define __rseq_str_1(x) #x
  87. #define __rseq_str(x) __rseq_str_1(x)
  88. #define rseq_log(fmt, args...) \
  89. fprintf(stderr, fmt "(in %s() at " __FILE__ ":" __rseq_str(__LINE__)"\n", \
  90. ## args, __func__)
  91. #define rseq_bug(fmt, args...) \
  92. do { \
  93. rseq_log(fmt, ##args); \
  94. abort(); \
  95. } while (0)
  96. #if defined(__x86_64__) || defined(__i386__)
  97. #include <rseq-x86.h>
  98. #elif defined(__ARMEL__)
  99. #include <rseq-arm.h>
  100. #elif defined (__AARCH64EL__)
  101. #include <rseq-arm64.h>
  102. #elif defined(__PPC__)
  103. #include <rseq-ppc.h>
  104. #elif defined(__mips__)
  105. #include <rseq-mips.h>
  106. #elif defined(__s390__)
  107. #include <rseq-s390.h>
  108. #elif defined(__riscv)
  109. #include <rseq-riscv.h>
  110. #elif defined(__or1k__)
  111. #include <rseq-or1k.h>
  112. #else
  113. #error unsupported target
  114. #endif
  115. /*
  116. * Register rseq for the current thread. This needs to be called once
  117. * by any thread which uses restartable sequences, before they start
  118. * using restartable sequences, to ensure restartable sequences
  119. * succeed. A restartable sequence executed from a non-registered
  120. * thread will always fail.
  121. */
  122. int rseq_register_current_thread(void);
  123. /*
  124. * Unregister rseq for current thread.
  125. */
  126. int rseq_unregister_current_thread(void);
  127. /*
  128. * Restartable sequence fallback for reading the current CPU number.
  129. */
  130. int32_t rseq_fallback_current_cpu(void);
  131. /*
  132. * Restartable sequence fallback for reading the current node number.
  133. */
  134. int32_t rseq_fallback_current_node(void);
  135. /*
  136. * Returns true if rseq is supported.
  137. */
  138. bool rseq_available(void);
  139. /*
  140. * Values returned can be either the current CPU number, -1 (rseq is
  141. * uninitialized), or -2 (rseq initialization has failed).
  142. */
  143. static inline int32_t rseq_current_cpu_raw(void)
  144. {
  145. return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
  146. }
  147. /*
  148. * Returns a possible CPU number, which is typically the current CPU.
  149. * The returned CPU number can be used to prepare for an rseq critical
  150. * section, which will confirm whether the cpu number is indeed the
  151. * current one, and whether rseq is initialized.
  152. *
  153. * The CPU number returned by rseq_cpu_start should always be validated
  154. * by passing it to a rseq asm sequence, or by comparing it to the
  155. * return value of rseq_current_cpu_raw() if the rseq asm sequence
  156. * does not need to be invoked.
  157. */
  158. static inline uint32_t rseq_cpu_start(void)
  159. {
  160. return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
  161. }
  162. static inline uint32_t rseq_current_cpu(void)
  163. {
  164. int32_t cpu;
  165. cpu = rseq_current_cpu_raw();
  166. if (rseq_unlikely(cpu < 0))
  167. cpu = rseq_fallback_current_cpu();
  168. return cpu;
  169. }
  170. static inline bool rseq_node_id_available(void)
  171. {
  172. return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, node_id);
  173. }
  174. /*
  175. * Current NUMA node number.
  176. */
  177. static inline uint32_t rseq_current_node_id(void)
  178. {
  179. assert(rseq_node_id_available());
  180. return RSEQ_ACCESS_ONCE(rseq_get_abi()->node_id);
  181. }
  182. static inline bool rseq_mm_cid_available(void)
  183. {
  184. return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, mm_cid);
  185. }
  186. static inline uint32_t rseq_current_mm_cid(void)
  187. {
  188. return RSEQ_ACCESS_ONCE(rseq_get_abi()->mm_cid);
  189. }
  190. static inline void rseq_clear_rseq_cs(void)
  191. {
  192. RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
  193. }
  194. /*
  195. * rseq_prepare_unload() should be invoked by each thread executing a rseq
  196. * critical section at least once between their last critical section and
  197. * library unload of the library defining the rseq critical section (struct
  198. * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
  199. * post_commit_offset fields. This also applies to use of rseq in code
  200. * generated by JIT: rseq_prepare_unload() should be invoked at least once by
  201. * each thread executing a rseq critical section before reclaim of the memory
  202. * holding the struct rseq_cs or reclaim of the code pointed to by struct
  203. * rseq_cs start_ip and post_commit_offset fields.
  204. */
  205. static inline void rseq_prepare_unload(void)
  206. {
  207. rseq_clear_rseq_cs();
  208. }
  209. static inline __attribute__((always_inline))
  210. int rseq_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  211. intptr_t *v, intptr_t expect,
  212. intptr_t newv, int cpu)
  213. {
  214. if (rseq_mo != RSEQ_MO_RELAXED)
  215. return -1;
  216. switch (percpu_mode) {
  217. case RSEQ_PERCPU_CPU_ID:
  218. return rseq_cmpeqv_storev_relaxed_cpu_id(v, expect, newv, cpu);
  219. case RSEQ_PERCPU_MM_CID:
  220. return rseq_cmpeqv_storev_relaxed_mm_cid(v, expect, newv, cpu);
  221. }
  222. return -1;
  223. }
  224. /*
  225. * Compare @v against @expectnot. When it does _not_ match, load @v
  226. * into @load, and store the content of *@v + voffp into @v.
  227. */
  228. static inline __attribute__((always_inline))
  229. int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  230. intptr_t *v, intptr_t expectnot, long voffp, intptr_t *load,
  231. int cpu)
  232. {
  233. if (rseq_mo != RSEQ_MO_RELAXED)
  234. return -1;
  235. switch (percpu_mode) {
  236. case RSEQ_PERCPU_CPU_ID:
  237. return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v, expectnot, voffp, load, cpu);
  238. case RSEQ_PERCPU_MM_CID:
  239. return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v, expectnot, voffp, load, cpu);
  240. }
  241. return -1;
  242. }
  243. static inline __attribute__((always_inline))
  244. int rseq_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  245. intptr_t *v, intptr_t count, int cpu)
  246. {
  247. if (rseq_mo != RSEQ_MO_RELAXED)
  248. return -1;
  249. switch (percpu_mode) {
  250. case RSEQ_PERCPU_CPU_ID:
  251. return rseq_addv_relaxed_cpu_id(v, count, cpu);
  252. case RSEQ_PERCPU_MM_CID:
  253. return rseq_addv_relaxed_mm_cid(v, count, cpu);
  254. }
  255. return -1;
  256. }
  257. #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
  258. /*
  259. * pval = *(ptr+off)
  260. * *pval += inc;
  261. */
  262. static inline __attribute__((always_inline))
  263. int rseq_offset_deref_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  264. intptr_t *ptr, long off, intptr_t inc, int cpu)
  265. {
  266. if (rseq_mo != RSEQ_MO_RELAXED)
  267. return -1;
  268. switch (percpu_mode) {
  269. case RSEQ_PERCPU_CPU_ID:
  270. return rseq_offset_deref_addv_relaxed_cpu_id(ptr, off, inc, cpu);
  271. case RSEQ_PERCPU_MM_CID:
  272. return rseq_offset_deref_addv_relaxed_mm_cid(ptr, off, inc, cpu);
  273. }
  274. return -1;
  275. }
  276. #endif
  277. static inline __attribute__((always_inline))
  278. int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  279. intptr_t *v, intptr_t expect,
  280. intptr_t *v2, intptr_t newv2,
  281. intptr_t newv, int cpu)
  282. {
  283. switch (rseq_mo) {
  284. case RSEQ_MO_RELAXED:
  285. switch (percpu_mode) {
  286. case RSEQ_PERCPU_CPU_ID:
  287. return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v, expect, v2, newv2, newv, cpu);
  288. case RSEQ_PERCPU_MM_CID:
  289. return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v, expect, v2, newv2, newv, cpu);
  290. }
  291. return -1;
  292. case RSEQ_MO_RELEASE:
  293. switch (percpu_mode) {
  294. case RSEQ_PERCPU_CPU_ID:
  295. return rseq_cmpeqv_trystorev_storev_release_cpu_id(v, expect, v2, newv2, newv, cpu);
  296. case RSEQ_PERCPU_MM_CID:
  297. return rseq_cmpeqv_trystorev_storev_release_mm_cid(v, expect, v2, newv2, newv, cpu);
  298. }
  299. return -1;
  300. default:
  301. return -1;
  302. }
  303. }
  304. static inline __attribute__((always_inline))
  305. int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  306. intptr_t *v, intptr_t expect,
  307. intptr_t *v2, intptr_t expect2,
  308. intptr_t newv, int cpu)
  309. {
  310. if (rseq_mo != RSEQ_MO_RELAXED)
  311. return -1;
  312. switch (percpu_mode) {
  313. case RSEQ_PERCPU_CPU_ID:
  314. return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v, expect, v2, expect2, newv, cpu);
  315. case RSEQ_PERCPU_MM_CID:
  316. return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v, expect, v2, expect2, newv, cpu);
  317. }
  318. return -1;
  319. }
  320. static inline __attribute__((always_inline))
  321. int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  322. intptr_t *v, intptr_t expect,
  323. void *dst, void *src, size_t len,
  324. intptr_t newv, int cpu)
  325. {
  326. switch (rseq_mo) {
  327. case RSEQ_MO_RELAXED:
  328. switch (percpu_mode) {
  329. case RSEQ_PERCPU_CPU_ID:
  330. return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v, expect, dst, src, len, newv, cpu);
  331. case RSEQ_PERCPU_MM_CID:
  332. return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v, expect, dst, src, len, newv, cpu);
  333. }
  334. return -1;
  335. case RSEQ_MO_RELEASE:
  336. switch (percpu_mode) {
  337. case RSEQ_PERCPU_CPU_ID:
  338. return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v, expect, dst, src, len, newv, cpu);
  339. case RSEQ_PERCPU_MM_CID:
  340. return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v, expect, dst, src, len, newv, cpu);
  341. }
  342. return -1;
  343. default:
  344. return -1;
  345. }
  346. }
  347. #endif /* RSEQ_H_ */