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