rseq.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. #else
  111. #error unsupported target
  112. #endif
  113. /*
  114. * Register rseq for the current thread. This needs to be called once
  115. * by any thread which uses restartable sequences, before they start
  116. * using restartable sequences, to ensure restartable sequences
  117. * succeed. A restartable sequence executed from a non-registered
  118. * thread will always fail.
  119. */
  120. int rseq_register_current_thread(void);
  121. /*
  122. * Unregister rseq for current thread.
  123. */
  124. int rseq_unregister_current_thread(void);
  125. /*
  126. * Restartable sequence fallback for reading the current CPU number.
  127. */
  128. int32_t rseq_fallback_current_cpu(void);
  129. /*
  130. * Restartable sequence fallback for reading the current node number.
  131. */
  132. int32_t rseq_fallback_current_node(void);
  133. /*
  134. * Values returned can be either the current CPU number, -1 (rseq is
  135. * uninitialized), or -2 (rseq initialization has failed).
  136. */
  137. static inline int32_t rseq_current_cpu_raw(void)
  138. {
  139. return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id);
  140. }
  141. /*
  142. * Returns a possible CPU number, which is typically the current CPU.
  143. * The returned CPU number can be used to prepare for an rseq critical
  144. * section, which will confirm whether the cpu number is indeed the
  145. * current one, and whether rseq is initialized.
  146. *
  147. * The CPU number returned by rseq_cpu_start should always be validated
  148. * by passing it to a rseq asm sequence, or by comparing it to the
  149. * return value of rseq_current_cpu_raw() if the rseq asm sequence
  150. * does not need to be invoked.
  151. */
  152. static inline uint32_t rseq_cpu_start(void)
  153. {
  154. return RSEQ_ACCESS_ONCE(rseq_get_abi()->cpu_id_start);
  155. }
  156. static inline uint32_t rseq_current_cpu(void)
  157. {
  158. int32_t cpu;
  159. cpu = rseq_current_cpu_raw();
  160. if (rseq_unlikely(cpu < 0))
  161. cpu = rseq_fallback_current_cpu();
  162. return cpu;
  163. }
  164. static inline bool rseq_node_id_available(void)
  165. {
  166. return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, node_id);
  167. }
  168. /*
  169. * Current NUMA node number.
  170. */
  171. static inline uint32_t rseq_current_node_id(void)
  172. {
  173. assert(rseq_node_id_available());
  174. return RSEQ_ACCESS_ONCE(rseq_get_abi()->node_id);
  175. }
  176. static inline bool rseq_mm_cid_available(void)
  177. {
  178. return (int) rseq_size >= rseq_offsetofend(struct rseq_abi, mm_cid);
  179. }
  180. static inline uint32_t rseq_current_mm_cid(void)
  181. {
  182. return RSEQ_ACCESS_ONCE(rseq_get_abi()->mm_cid);
  183. }
  184. static inline void rseq_clear_rseq_cs(void)
  185. {
  186. RSEQ_WRITE_ONCE(rseq_get_abi()->rseq_cs.arch.ptr, 0);
  187. }
  188. /*
  189. * rseq_prepare_unload() should be invoked by each thread executing a rseq
  190. * critical section at least once between their last critical section and
  191. * library unload of the library defining the rseq critical section (struct
  192. * rseq_cs) or the code referred to by the struct rseq_cs start_ip and
  193. * post_commit_offset fields. This also applies to use of rseq in code
  194. * generated by JIT: rseq_prepare_unload() should be invoked at least once by
  195. * each thread executing a rseq critical section before reclaim of the memory
  196. * holding the struct rseq_cs or reclaim of the code pointed to by struct
  197. * rseq_cs start_ip and post_commit_offset fields.
  198. */
  199. static inline void rseq_prepare_unload(void)
  200. {
  201. rseq_clear_rseq_cs();
  202. }
  203. static inline __attribute__((always_inline))
  204. int rseq_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  205. intptr_t *v, intptr_t expect,
  206. intptr_t newv, int cpu)
  207. {
  208. if (rseq_mo != RSEQ_MO_RELAXED)
  209. return -1;
  210. switch (percpu_mode) {
  211. case RSEQ_PERCPU_CPU_ID:
  212. return rseq_cmpeqv_storev_relaxed_cpu_id(v, expect, newv, cpu);
  213. case RSEQ_PERCPU_MM_CID:
  214. return rseq_cmpeqv_storev_relaxed_mm_cid(v, expect, newv, cpu);
  215. }
  216. return -1;
  217. }
  218. /*
  219. * Compare @v against @expectnot. When it does _not_ match, load @v
  220. * into @load, and store the content of *@v + voffp into @v.
  221. */
  222. static inline __attribute__((always_inline))
  223. int rseq_cmpnev_storeoffp_load(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  224. intptr_t *v, intptr_t expectnot, long voffp, intptr_t *load,
  225. int cpu)
  226. {
  227. if (rseq_mo != RSEQ_MO_RELAXED)
  228. return -1;
  229. switch (percpu_mode) {
  230. case RSEQ_PERCPU_CPU_ID:
  231. return rseq_cmpnev_storeoffp_load_relaxed_cpu_id(v, expectnot, voffp, load, cpu);
  232. case RSEQ_PERCPU_MM_CID:
  233. return rseq_cmpnev_storeoffp_load_relaxed_mm_cid(v, expectnot, voffp, load, cpu);
  234. }
  235. return -1;
  236. }
  237. static inline __attribute__((always_inline))
  238. int rseq_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  239. intptr_t *v, intptr_t count, int cpu)
  240. {
  241. if (rseq_mo != RSEQ_MO_RELAXED)
  242. return -1;
  243. switch (percpu_mode) {
  244. case RSEQ_PERCPU_CPU_ID:
  245. return rseq_addv_relaxed_cpu_id(v, count, cpu);
  246. case RSEQ_PERCPU_MM_CID:
  247. return rseq_addv_relaxed_mm_cid(v, count, cpu);
  248. }
  249. return -1;
  250. }
  251. #ifdef RSEQ_ARCH_HAS_OFFSET_DEREF_ADDV
  252. /*
  253. * pval = *(ptr+off)
  254. * *pval += inc;
  255. */
  256. static inline __attribute__((always_inline))
  257. int rseq_offset_deref_addv(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  258. intptr_t *ptr, long off, intptr_t inc, int cpu)
  259. {
  260. if (rseq_mo != RSEQ_MO_RELAXED)
  261. return -1;
  262. switch (percpu_mode) {
  263. case RSEQ_PERCPU_CPU_ID:
  264. return rseq_offset_deref_addv_relaxed_cpu_id(ptr, off, inc, cpu);
  265. case RSEQ_PERCPU_MM_CID:
  266. return rseq_offset_deref_addv_relaxed_mm_cid(ptr, off, inc, cpu);
  267. }
  268. return -1;
  269. }
  270. #endif
  271. static inline __attribute__((always_inline))
  272. int rseq_cmpeqv_trystorev_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  273. intptr_t *v, intptr_t expect,
  274. intptr_t *v2, intptr_t newv2,
  275. intptr_t newv, int cpu)
  276. {
  277. switch (rseq_mo) {
  278. case RSEQ_MO_RELAXED:
  279. switch (percpu_mode) {
  280. case RSEQ_PERCPU_CPU_ID:
  281. return rseq_cmpeqv_trystorev_storev_relaxed_cpu_id(v, expect, v2, newv2, newv, cpu);
  282. case RSEQ_PERCPU_MM_CID:
  283. return rseq_cmpeqv_trystorev_storev_relaxed_mm_cid(v, expect, v2, newv2, newv, cpu);
  284. }
  285. return -1;
  286. case RSEQ_MO_RELEASE:
  287. switch (percpu_mode) {
  288. case RSEQ_PERCPU_CPU_ID:
  289. return rseq_cmpeqv_trystorev_storev_release_cpu_id(v, expect, v2, newv2, newv, cpu);
  290. case RSEQ_PERCPU_MM_CID:
  291. return rseq_cmpeqv_trystorev_storev_release_mm_cid(v, expect, v2, newv2, newv, cpu);
  292. }
  293. return -1;
  294. default:
  295. return -1;
  296. }
  297. }
  298. static inline __attribute__((always_inline))
  299. int rseq_cmpeqv_cmpeqv_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  300. intptr_t *v, intptr_t expect,
  301. intptr_t *v2, intptr_t expect2,
  302. intptr_t newv, int cpu)
  303. {
  304. if (rseq_mo != RSEQ_MO_RELAXED)
  305. return -1;
  306. switch (percpu_mode) {
  307. case RSEQ_PERCPU_CPU_ID:
  308. return rseq_cmpeqv_cmpeqv_storev_relaxed_cpu_id(v, expect, v2, expect2, newv, cpu);
  309. case RSEQ_PERCPU_MM_CID:
  310. return rseq_cmpeqv_cmpeqv_storev_relaxed_mm_cid(v, expect, v2, expect2, newv, cpu);
  311. }
  312. return -1;
  313. }
  314. static inline __attribute__((always_inline))
  315. int rseq_cmpeqv_trymemcpy_storev(enum rseq_mo rseq_mo, enum rseq_percpu_mode percpu_mode,
  316. intptr_t *v, intptr_t expect,
  317. void *dst, void *src, size_t len,
  318. intptr_t newv, int cpu)
  319. {
  320. switch (rseq_mo) {
  321. case RSEQ_MO_RELAXED:
  322. switch (percpu_mode) {
  323. case RSEQ_PERCPU_CPU_ID:
  324. return rseq_cmpeqv_trymemcpy_storev_relaxed_cpu_id(v, expect, dst, src, len, newv, cpu);
  325. case RSEQ_PERCPU_MM_CID:
  326. return rseq_cmpeqv_trymemcpy_storev_relaxed_mm_cid(v, expect, dst, src, len, newv, cpu);
  327. }
  328. return -1;
  329. case RSEQ_MO_RELEASE:
  330. switch (percpu_mode) {
  331. case RSEQ_PERCPU_CPU_ID:
  332. return rseq_cmpeqv_trymemcpy_storev_release_cpu_id(v, expect, dst, src, len, newv, cpu);
  333. case RSEQ_PERCPU_MM_CID:
  334. return rseq_cmpeqv_trymemcpy_storev_release_mm_cid(v, expect, dst, src, len, newv, cpu);
  335. }
  336. return -1;
  337. default:
  338. return -1;
  339. }
  340. }
  341. #endif /* RSEQ_H_ */