rseq.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* Restartable Sequences exported symbols. Linux header.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _SYS_RSEQ_H
  15. #define _SYS_RSEQ_H 1
  16. /* Architecture-specific rseq signature. */
  17. #include <bits/rseq.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <sys/cdefs.h>
  21. #ifdef __has_include
  22. # if __has_include ("linux/rseq.h")
  23. # define __GLIBC_HAVE_KERNEL_RSEQ
  24. # endif
  25. #else
  26. # include <linux/version.h>
  27. # if LINUX_VERSION_CODE >= KERNEL_VERSION (4, 18, 0)
  28. # define __GLIBC_HAVE_KERNEL_RSEQ
  29. # endif
  30. #endif
  31. #ifdef __GLIBC_HAVE_KERNEL_RSEQ
  32. /* We use the structures declarations from the kernel headers. */
  33. # include <linux/rseq.h>
  34. #else /* __GLIBC_HAVE_KERNEL_RSEQ */
  35. /* We use a copy of the include/uapi/linux/rseq.h kernel header. */
  36. enum rseq_cpu_id_state
  37. {
  38. RSEQ_CPU_ID_UNINITIALIZED = -1,
  39. RSEQ_CPU_ID_REGISTRATION_FAILED = -2,
  40. };
  41. enum rseq_flags
  42. {
  43. RSEQ_FLAG_UNREGISTER = (1 << 0),
  44. };
  45. enum rseq_cs_flags_bit
  46. {
  47. RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT = 0,
  48. RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT = 1,
  49. RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT = 2,
  50. };
  51. enum rseq_cs_flags
  52. {
  53. RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT =
  54. (1U << RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT),
  55. RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL =
  56. (1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
  57. RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE =
  58. (1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
  59. };
  60. /* struct rseq_cs is aligned on 32 bytes to ensure it is always
  61. contained within a single cache-line. It is usually declared as
  62. link-time constant data. */
  63. struct rseq_cs
  64. {
  65. /* Version of this structure. */
  66. uint32_t version;
  67. /* enum rseq_cs_flags. */
  68. uint32_t flags;
  69. uint64_t start_ip;
  70. /* Offset from start_ip. */
  71. uint64_t post_commit_offset;
  72. uint64_t abort_ip;
  73. } __attribute__ ((__aligned__ (32)));
  74. /* struct rseq is aligned on 32 bytes to ensure it is always
  75. contained within a single cache-line.
  76. A single struct rseq per thread is allowed. */
  77. struct rseq
  78. {
  79. /* Restartable sequences cpu_id_start field. Updated by the
  80. kernel. Read by user-space with single-copy atomicity
  81. semantics. This field should only be read by the thread which
  82. registered this data structure. Aligned on 32-bit. Always
  83. contains a value in the range of possible CPUs, although the
  84. value may not be the actual current CPU (e.g. if rseq is not
  85. initialized). This CPU number value should always be compared
  86. against the value of the cpu_id field before performing a rseq
  87. commit or returning a value read from a data structure indexed
  88. using the cpu_id_start value. */
  89. uint32_t cpu_id_start;
  90. /* Restartable sequences cpu_id field. Updated by the kernel.
  91. Read by user-space with single-copy atomicity semantics. This
  92. field should only be read by the thread which registered this
  93. data structure. Aligned on 32-bit. Values
  94. RSEQ_CPU_ID_UNINITIALIZED and RSEQ_CPU_ID_REGISTRATION_FAILED
  95. have a special semantic: the former means "rseq uninitialized",
  96. and latter means "rseq initialization failed". This value is
  97. meant to be read within rseq critical sections and compared
  98. with the cpu_id_start value previously read, before performing
  99. the commit instruction, or read and compared with the
  100. cpu_id_start value before returning a value loaded from a data
  101. structure indexed using the cpu_id_start value. */
  102. uint32_t cpu_id;
  103. /* Restartable sequences rseq_cs field.
  104. Contains NULL when no critical section is active for the current
  105. thread, or holds a pointer to the currently active struct rseq_cs.
  106. Updated by user-space, which sets the address of the currently
  107. active rseq_cs at the beginning of assembly instruction sequence
  108. block, and set to NULL by the kernel when it restarts an assembly
  109. instruction sequence block, as well as when the kernel detects that
  110. it is preempting or delivering a signal outside of the range
  111. targeted by the rseq_cs. Also needs to be set to NULL by user-space
  112. before reclaiming memory that contains the targeted struct rseq_cs.
  113. Read and set by the kernel. Set by user-space with single-copy
  114. atomicity semantics. This field should only be updated by the
  115. thread which registered this data structure. Aligned on 64-bit.
  116. 32-bit architectures should update the low order bits of the
  117. rseq_cs field, leaving the high order bits initialized to 0. */
  118. uint64_t rseq_cs;
  119. /* Restartable sequences flags field.
  120. This field should only be updated by the thread which
  121. registered this data structure. Read by the kernel.
  122. Mainly used for single-stepping through rseq critical sections
  123. with debuggers.
  124. - RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT
  125. Inhibit instruction sequence block restart on preemption
  126. for this thread.
  127. - RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL
  128. Inhibit instruction sequence block restart on signal
  129. delivery for this thread.
  130. - RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE
  131. Inhibit instruction sequence block restart on migration for
  132. this thread. */
  133. uint32_t flags;
  134. /* Restartable sequences node_id field. Updated by the kernel. Read by
  135. user-space with single-copy atomicity semantics. This field should only
  136. be read by the thread which registered this data structure. Aligned on
  137. 32-bit. Contains the current NUMA node ID. */
  138. uint32_t node_id;
  139. /* Restartable sequences mm_cid field. Updated by the kernel. Read by
  140. user-space with single-copy atomicity semantics. This field should only
  141. be read by the thread which registered this data structure. Aligned on
  142. 32-bit. Contains the current thread's concurrency ID (allocated
  143. uniquely within a memory map). */
  144. uint32_t mm_cid;
  145. } __attribute__ ((__aligned__ (32)));
  146. #endif /* __GLIBC_HAVE_KERNEL_RSEQ */
  147. /* Offset from the thread pointer to the rseq area. */
  148. extern const ptrdiff_t __rseq_offset;
  149. /* Size of the registered rseq area. 0 if the registration was
  150. unsuccessful. */
  151. extern const unsigned int __rseq_size;
  152. /* Flags used during rseq registration. */
  153. extern const unsigned int __rseq_flags;
  154. #endif /* sys/rseq.h */