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