xref: /linux-6.15/include/uapi/linux/rseq.h (revision 34962fb8)
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 	LINUX_FIELD_u32_u64(start_ip);
56 	/* Offset from start_ip. */
57 	LINUX_FIELD_u32_u64(post_commit_offset);
58 	LINUX_FIELD_u32_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, and read by user-space with single-copy atomicity
71 	 * semantics. Aligned on 32-bit. Always contains a value in the
72 	 * range of possible CPUs, although the value may not be the
73 	 * actual current CPU (e.g. if rseq is not initialized). This
74 	 * CPU number value should always be compared against the value
75 	 * of the cpu_id field before performing a rseq commit or
76 	 * returning a value read from a data structure indexed using
77 	 * the cpu_id_start value.
78 	 */
79 	__u32 cpu_id_start;
80 	/*
81 	 * Restartable sequences cpu_id field. Updated by the kernel,
82 	 * and read by user-space with single-copy atomicity semantics.
83 	 * Aligned on 32-bit. Values RSEQ_CPU_ID_UNINITIALIZED and
84 	 * RSEQ_CPU_ID_REGISTRATION_FAILED have a special semantic: the
85 	 * former means "rseq uninitialized", and latter means "rseq
86 	 * initialization failed". This value is meant to be read within
87 	 * rseq critical sections and compared with the cpu_id_start
88 	 * value previously read, before performing the commit instruction,
89 	 * or read and compared with the cpu_id_start value before returning
90 	 * a value loaded from a data structure indexed using the
91 	 * cpu_id_start value.
92 	 */
93 	__u32 cpu_id;
94 	/*
95 	 * Restartable sequences rseq_cs field.
96 	 *
97 	 * Contains NULL when no critical section is active for the current
98 	 * thread, or holds a pointer to the currently active struct rseq_cs.
99 	 *
100 	 * Updated by user-space, which sets the address of the currently
101 	 * active rseq_cs at the beginning of assembly instruction sequence
102 	 * block, and set to NULL by the kernel when it restarts an assembly
103 	 * instruction sequence block, as well as when the kernel detects that
104 	 * it is preempting or delivering a signal outside of the range
105 	 * targeted by the rseq_cs. Also needs to be set to NULL by user-space
106 	 * before reclaiming memory that contains the targeted struct rseq_cs.
107 	 *
108 	 * Read and set by the kernel with single-copy atomicity semantics.
109 	 * Set by user-space with single-copy atomicity semantics. Aligned
110 	 * on 64-bit.
111 	 */
112 	LINUX_FIELD_u32_u64(rseq_cs);
113 	/*
114 	 * - RSEQ_DISABLE flag:
115 	 *
116 	 * Fallback fast-track flag for single-stepping.
117 	 * Set by user-space if lack of progress is detected.
118 	 * Cleared by user-space after rseq finish.
119 	 * Read by the kernel.
120 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_PREEMPT
121 	 *     Inhibit instruction sequence block restart and event
122 	 *     counter increment on preemption for this thread.
123 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_SIGNAL
124 	 *     Inhibit instruction sequence block restart and event
125 	 *     counter increment on signal delivery for this thread.
126 	 * - RSEQ_CS_FLAG_NO_RESTART_ON_MIGRATE
127 	 *     Inhibit instruction sequence block restart and event
128 	 *     counter increment on migration for this thread.
129 	 */
130 	__u32 flags;
131 } __attribute__((aligned(4 * sizeof(__u64))));
132 
133 #endif /* _UAPI_LINUX_RSEQ_H */
134