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