rseq-abi.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
  2. #ifndef _RSEQ_ABI_H
  3. #define _RSEQ_ABI_H
  4. /*
  5. * rseq-abi.h
  6. *
  7. * Restartable sequences system call API
  8. *
  9. * Copyright (c) 2015-2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  10. */
  11. #include <linux/types.h>
  12. #include <asm/byteorder.h>
  13. enum rseq_abi_cpu_id_state {
  14. RSEQ_ABI_CPU_ID_UNINITIALIZED = -1,
  15. RSEQ_ABI_CPU_ID_REGISTRATION_FAILED = -2,
  16. };
  17. enum rseq_abi_flags {
  18. RSEQ_ABI_FLAG_UNREGISTER = (1 << 0),
  19. };
  20. enum rseq_abi_cs_flags_bit {
  21. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT = 0,
  22. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT = 1,
  23. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT = 2,
  24. };
  25. enum rseq_abi_cs_flags {
  26. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_PREEMPT =
  27. (1U << RSEQ_ABI_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT),
  28. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_SIGNAL =
  29. (1U << RSEQ_ABI_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
  30. RSEQ_ABI_CS_FLAG_NO_RESTART_ON_MIGRATE =
  31. (1U << RSEQ_ABI_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
  32. };
  33. /*
  34. * struct rseq_abi_cs is aligned on 4 * 8 bytes to ensure it is always
  35. * contained within a single cache-line. It is usually declared as
  36. * link-time constant data.
  37. */
  38. struct rseq_abi_cs {
  39. /* Version of this structure. */
  40. __u32 version;
  41. /* enum rseq_abi_cs_flags */
  42. __u32 flags;
  43. __u64 start_ip;
  44. /* Offset from start_ip. */
  45. __u64 post_commit_offset;
  46. __u64 abort_ip;
  47. } __attribute__((aligned(4 * sizeof(__u64))));
  48. /**
  49. * rseq_abi_slice_ctrl - Time slice extension control structure
  50. * @all: Compound value
  51. * @request: Request for a time slice extension
  52. * @granted: Granted time slice extension
  53. *
  54. * @request is set by user space and can be cleared by user space or kernel
  55. * space. @granted is set and cleared by the kernel and must only be read
  56. * by user space.
  57. */
  58. struct rseq_abi_slice_ctrl {
  59. union {
  60. __u32 all;
  61. struct {
  62. __u8 request;
  63. __u8 granted;
  64. __u16 __reserved;
  65. };
  66. };
  67. };
  68. /*
  69. * struct rseq_abi is aligned on 4 * 8 bytes to ensure it is always
  70. * contained within a single cache-line.
  71. *
  72. * A single struct rseq_abi per thread is allowed.
  73. */
  74. struct rseq_abi {
  75. /*
  76. * Restartable sequences cpu_id_start field. Updated by the
  77. * kernel. Read by user-space with single-copy atomicity
  78. * semantics. This field should only be read by the thread which
  79. * registered this data structure. Aligned on 32-bit. Always
  80. * contains a value in the range of possible CPUs, although the
  81. * value may not be the actual current CPU (e.g. if rseq is not
  82. * initialized). This CPU number value should always be compared
  83. * against the value of the cpu_id field before performing a rseq
  84. * commit or returning a value read from a data structure indexed
  85. * using the cpu_id_start value.
  86. */
  87. __u32 cpu_id_start;
  88. /*
  89. * Restartable sequences cpu_id field. Updated by the kernel.
  90. * Read by user-space with single-copy atomicity semantics. This
  91. * field should only be read by the thread which registered this
  92. * data structure. Aligned on 32-bit. Values
  93. * RSEQ_CPU_ID_UNINITIALIZED and RSEQ_CPU_ID_REGISTRATION_FAILED
  94. * have a special semantic: the former means "rseq uninitialized",
  95. * and latter means "rseq initialization failed". This value is
  96. * meant to be read within rseq critical sections and compared
  97. * with the cpu_id_start value previously read, before performing
  98. * the commit instruction, or read and compared with the
  99. * cpu_id_start value before returning a value loaded from a data
  100. * structure indexed using the cpu_id_start value.
  101. */
  102. __u32 cpu_id;
  103. /*
  104. * Restartable sequences rseq_cs field.
  105. *
  106. * Contains NULL when no critical section is active for the current
  107. * thread, or holds a pointer to the currently active struct rseq_cs.
  108. *
  109. * Updated by user-space, which sets the address of the currently
  110. * active rseq_cs at the beginning of assembly instruction sequence
  111. * block, and set to NULL by the kernel when it restarts an assembly
  112. * instruction sequence block, as well as when the kernel detects that
  113. * it is preempting or delivering a signal outside of the range
  114. * targeted by the rseq_cs. Also needs to be set to NULL by user-space
  115. * before reclaiming memory that contains the targeted struct rseq_cs.
  116. *
  117. * Read and set by the kernel. Set by user-space with single-copy
  118. * atomicity semantics. This field should only be updated by the
  119. * thread which registered this data structure. Aligned on 64-bit.
  120. */
  121. union {
  122. __u64 ptr64;
  123. /*
  124. * The "arch" field provides architecture accessor for
  125. * the ptr field based on architecture pointer size and
  126. * endianness.
  127. */
  128. struct {
  129. #ifdef __LP64__
  130. __u64 ptr;
  131. #elif defined(__BYTE_ORDER) ? (__BYTE_ORDER == __BIG_ENDIAN) : defined(__BIG_ENDIAN)
  132. __u32 padding; /* Initialized to zero. */
  133. __u32 ptr;
  134. #else
  135. __u32 ptr;
  136. __u32 padding; /* Initialized to zero. */
  137. #endif
  138. } arch;
  139. } rseq_cs;
  140. /*
  141. * Restartable sequences flags field.
  142. *
  143. * This field should only be updated by the thread which
  144. * registered this data structure. Read by the kernel.
  145. * Mainly used for single-stepping through rseq critical sections
  146. * with debuggers.
  147. *
  148. * - RSEQ_ABI_CS_FLAG_NO_RESTART_ON_PREEMPT
  149. * Inhibit instruction sequence block restart on preemption
  150. * for this thread.
  151. * - RSEQ_ABI_CS_FLAG_NO_RESTART_ON_SIGNAL
  152. * Inhibit instruction sequence block restart on signal
  153. * delivery for this thread.
  154. * - RSEQ_ABI_CS_FLAG_NO_RESTART_ON_MIGRATE
  155. * Inhibit instruction sequence block restart on migration for
  156. * this thread.
  157. */
  158. __u32 flags;
  159. /*
  160. * Restartable sequences node_id field. Updated by the kernel. Read by
  161. * user-space with single-copy atomicity semantics. This field should
  162. * only be read by the thread which registered this data structure.
  163. * Aligned on 32-bit. Contains the current NUMA node ID.
  164. */
  165. __u32 node_id;
  166. /*
  167. * Restartable sequences mm_cid field. Updated by the kernel. Read by
  168. * user-space with single-copy atomicity semantics. This field should
  169. * only be read by the thread which registered this data structure.
  170. * Aligned on 32-bit. Contains the current thread's concurrency ID
  171. * (allocated uniquely within a memory map).
  172. */
  173. __u32 mm_cid;
  174. /*
  175. * Time slice extension control structure. CPU local updates from
  176. * kernel and user space.
  177. */
  178. struct rseq_abi_slice_ctrl slice_ctrl;
  179. /*
  180. * Flexible array member at end of structure, after last feature field.
  181. */
  182. char end[];
  183. } __attribute__((aligned(4 * sizeof(__u64))));
  184. #endif /* _RSEQ_ABI_H */