rseq.h 7.5 KB

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