xref: /linux-6.15/include/uapi/linux/rseq.h (revision 0fb9a1ab)
1 /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
2 #ifndef _UAPI_LINUX_RSEQ_H
3 #define _UAPI_LINUX_RSEQ_H
4 
5 /*
6  * linux/rseq.h
7  *
8  * Restartable sequences system call API
9  *
10  * Copyright (c) 2015-2018 Mathieu Desnoyers <[email protected]>
11  */
12 
13 #ifdef __KERNEL__
14 # include <linux/types.h>
15 #else
16 # include <stdint.h>
17 #endif
18 
19 #include <linux/types_32_64.h>
20 
21 enum rseq_cpu_id_state {
22 	RSEQ_CPU_ID_UNINITIALIZED		= -1,
23 	RSEQ_CPU_ID_REGISTRATION_FAILED		= -2,
24 };
25 
26 enum rseq_flags {
27 	RSEQ_FLAG_UNREGISTER = (1 << 0),
28 };
29 
30 enum rseq_cs_flags_bit {
31 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT	= 0,
32 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT	= 1,
33 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT	= 2,
34 };
35 
36 enum rseq_cs_flags {
37 	RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT	=
38 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT_BIT),
39 	RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL	=
40 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL_BIT),
41 	RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE	=
42 		(1U << RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE_BIT),
43 };
44 
45 /*
46  * struct rseq_cs is aligned on 4 * 8 bytes to ensure it is always
47  * contained within a single cache-line. It is usually declared as
48  * link-time constant data.
49  */
50 struct rseq_cs {
51 	/* Version of this structure. */
52 	__u32 version;
53 	/* enum rseq_cs_flags */
54 	__u32 flags;
55 	__u64 start_ip;
56 	/* Offset from start_ip. */
57 	__u64 post_commit_offset;
58 	__u64 abort_ip;
59 } __attribute__((aligned(4 * sizeof(__u64))));
60 
61 /*
62  * struct rseq is aligned on 4 * 8 bytes to ensure it is always
63  * contained within a single cache-line.
64  *
65  * A single struct rseq per thread is allowed.
66  */
67 struct rseq {
68 	/*
69 	 * Restartable sequences cpu_id_start field. Updated by the
70 	 * kernel. Read by user-space with single-copy atomicity
71 	 * semantics. This field should only be read by the thread which
72 	 * registered this data structure. Aligned on 32-bit. Always
73 	 * contains a value in the range of possible CPUs, although the
74 	 * value may not be the actual current CPU (e.g. if rseq is not
75 	 * initialized). This CPU number value should always be compared
76 	 * against the value of the cpu_id field before performing a rseq
77 	 * commit or returning a value read from a data structure indexed
78 	 * using the cpu_id_start value.
79 	 */
80 	__u32 cpu_id_start;
81 	/*
82 	 * Restartable sequences cpu_id field. Updated by the kernel.
83 	 * Read by user-space with single-copy atomicity semantics. This
84 	 * field should only be read by the thread which registered this
85 	 * data structure. Aligned on 32-bit. Values
86 	 * RSEQ_CPU_ID_UNINITIALIZED and RSEQ_CPU_ID_REGISTRATION_FAILED
87 	 * have a special semantic: the former means "rseq uninitialized",
88 	 * and latter means "rseq initialization failed". This value is
89 	 * meant to be read within rseq critical sections and compared
90 	 * with the cpu_id_start value previously read, before performing
91 	 * the commit instruction, or read and compared with the
92 	 * cpu_id_start value before returning a value loaded from a data
93 	 * structure indexed using the cpu_id_start value.
94 	 */
95 	__u32 cpu_id;
96 	/*
97 	 * Restartable sequences rseq_cs field.
98 	 *
99 	 * Contains NULL when no critical section is active for the current
100 	 * thread, or holds a pointer to the currently active struct rseq_cs.
101 	 *
102 	 * Updated by user-space, which sets the address of the currently
103 	 * active rseq_cs at the beginning of assembly instruction sequence
104 	 * block, and set to NULL by the kernel when it restarts an assembly
105 	 * instruction sequence block, as well as when the kernel detects that
106 	 * it is preempting or delivering a signal outside of the range
107 	 * targeted by the rseq_cs. Also needs to be set to NULL by user-space
108 	 * before reclaiming memory that contains the targeted struct rseq_cs.
109 	 *
110 	 * Read and set by the kernel. Set by user-space with single-copy
111 	 * atomicity semantics. This field should only be updated by the
112 	 * thread which registered this data structure. Aligned on 64-bit.
113 	 */
114 	LINUX_FIELD_u32_u64(rseq_cs);
115 	/*
116 	 * Restartable sequences flags field.
117 	 *
118 	 * This field should only be updated by the thread which
119 	 * registered this data structure. Read by the kernel.
120 	 * Mainly used for single-stepping through rseq critical sections
121 	 * with debuggers.
122 	 *
123 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT
124 	 *     Inhibit instruction sequence block restart on preemption
125 	 *     for this thread.
126 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL
127 	 *     Inhibit instruction sequence block restart on signal
128 	 *     delivery for this thread.
129 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE
130 	 *     Inhibit instruction sequence block restart on migration for
131 	 *     this thread.
132 	 */
133 	__u32 flags;
134 } __attribute__((aligned(4 * sizeof(__u64))));
135 
136 #endif /* _UAPI_LINUX_RSEQ_H */
137