1 // SPDX-License-Identifier: LGPL-2.1
5 * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
32 #include <linux/auxvec.h>
34 #include <linux/compiler.h>
36 #include "../kselftest.h"
40 * Define weak versions to play nice with binaries that are statically linked
41 * against a libc that doesn't support registering its own rseq.
43 __weak ptrdiff_t __rseq_offset;
44 __weak unsigned int __rseq_size;
45 __weak unsigned int __rseq_flags;
47 static const ptrdiff_t *libc_rseq_offset_p = &__rseq_offset;
48 static const unsigned int *libc_rseq_size_p = &__rseq_size;
49 static const unsigned int *libc_rseq_flags_p = &__rseq_flags;
51 /* Offset from the thread pointer to the rseq area. */
52 ptrdiff_t rseq_offset;
55 * Size of the registered rseq area. 0 if the registration was
58 unsigned int rseq_size = -1U;
60 /* Flags used during rseq registration. */
61 unsigned int rseq_flags;
64 * rseq feature size supported by the kernel. 0 if the registration was
67 unsigned int rseq_feature_size = -1U;
69 static int rseq_ownership;
70 static int rseq_reg_success; /* At least one rseq registration has succeded. */
72 /* Allocate a large area for the TLS. */
73 #define RSEQ_THREAD_AREA_ALLOC_SIZE 1024
75 /* Original struct rseq feature size is 20 bytes. */
76 #define ORIG_RSEQ_FEATURE_SIZE 20
78 /* Original struct rseq allocation size is 32 bytes. */
79 #define ORIG_RSEQ_ALLOC_SIZE 32
82 __thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"), aligned(RSEQ_THREAD_AREA_ALLOC_SIZE))) = {
83 .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED,
86 static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len,
87 int flags, uint32_t sig)
89 return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
92 static int sys_getcpu(unsigned *cpu, unsigned *node)
94 return syscall(__NR_getcpu, cpu, node, NULL);
97 int rseq_available(void)
101 rc = sys_rseq(NULL, 0, 0, 0);
114 int rseq_register_current_thread(void)
118 if (!rseq_ownership) {
119 /* Treat libc's ownership as a successful registration. */
122 rc = sys_rseq(&__rseq_abi, rseq_size, 0, RSEQ_SIG);
124 if (RSEQ_READ_ONCE(rseq_reg_success)) {
125 /* Incoherent success/failure within process. */
130 assert(rseq_current_cpu_raw() >= 0);
131 RSEQ_WRITE_ONCE(rseq_reg_success, 1);
135 int rseq_unregister_current_thread(void)
139 if (!rseq_ownership) {
140 /* Treat libc's ownership as a successful unregistration. */
143 rc = sys_rseq(&__rseq_abi, rseq_size, RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG);
150 unsigned int get_rseq_feature_size(void)
152 unsigned long auxv_rseq_feature_size, auxv_rseq_align;
154 auxv_rseq_align = getauxval(AT_RSEQ_ALIGN);
155 assert(!auxv_rseq_align || auxv_rseq_align <= RSEQ_THREAD_AREA_ALLOC_SIZE);
157 auxv_rseq_feature_size = getauxval(AT_RSEQ_FEATURE_SIZE);
158 assert(!auxv_rseq_feature_size || auxv_rseq_feature_size <= RSEQ_THREAD_AREA_ALLOC_SIZE);
159 if (auxv_rseq_feature_size)
160 return auxv_rseq_feature_size;
162 return ORIG_RSEQ_FEATURE_SIZE;
165 static __attribute__((constructor))
169 * If the libc's registered rseq size isn't already valid, it may be
170 * because the binary is dynamically linked and not necessarily due to
171 * libc not having registered a restartable sequence. Try to find the
172 * symbols if that's the case.
174 if (!*libc_rseq_size_p) {
175 libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
176 libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
177 libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
179 if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p &&
180 *libc_rseq_size_p != 0) {
181 /* rseq registration owned by glibc */
182 rseq_offset = *libc_rseq_offset_p;
183 rseq_size = *libc_rseq_size_p;
184 rseq_flags = *libc_rseq_flags_p;
185 rseq_feature_size = get_rseq_feature_size();
186 if (rseq_feature_size > rseq_size)
187 rseq_feature_size = rseq_size;
191 if (!rseq_available()) {
193 rseq_feature_size = 0;
196 rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer();
198 rseq_feature_size = get_rseq_feature_size();
199 if (rseq_feature_size == ORIG_RSEQ_FEATURE_SIZE)
200 rseq_size = ORIG_RSEQ_ALLOC_SIZE;
202 rseq_size = RSEQ_THREAD_AREA_ALLOC_SIZE;
205 static __attribute__((destructor))
212 rseq_feature_size = -1U;
216 int32_t rseq_fallback_current_cpu(void)
220 cpu = sched_getcpu();
222 perror("sched_getcpu()");
228 int32_t rseq_fallback_current_node(void)
230 uint32_t cpu_id, node_id;
233 ret = sys_getcpu(&cpu_id, &node_id);
235 perror("sys_getcpu()");
238 return (int32_t) node_id;