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.
31 #include "../kselftest.h"
34 static const int *libc_rseq_offset_p;
35 static const unsigned int *libc_rseq_size_p;
36 static const unsigned int *libc_rseq_flags_p;
38 /* Offset from the thread pointer to the rseq area. */
41 /* Size of the registered rseq area. 0 if the registration was
43 unsigned int rseq_size = -1U;
45 /* Flags used during rseq registration. */
46 unsigned int rseq_flags;
48 static int rseq_ownership;
51 __thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"))) = {
52 .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED,
55 static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len,
56 int flags, uint32_t sig)
58 return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig);
61 int rseq_available(void)
65 rc = sys_rseq(NULL, 0, 0, 0);
78 int rseq_register_current_thread(void)
82 if (!rseq_ownership) {
83 /* Treat libc's ownership as a successful registration. */
86 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), 0, RSEQ_SIG);
89 assert(rseq_current_cpu_raw() >= 0);
93 int rseq_unregister_current_thread(void)
97 if (!rseq_ownership) {
98 /* Treat libc's ownership as a successful unregistration. */
101 rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG);
107 static __attribute__((constructor))
110 libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset");
111 libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size");
112 libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags");
113 if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p) {
114 /* rseq registration owned by glibc */
115 rseq_offset = *libc_rseq_offset_p;
116 rseq_size = *libc_rseq_size_p;
117 rseq_flags = *libc_rseq_flags_p;
120 if (!rseq_available())
123 rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer();
124 rseq_size = sizeof(struct rseq_abi);
128 static __attribute__((destructor))
138 int32_t rseq_fallback_current_cpu(void)
142 cpu = sched_getcpu();
144 perror("sched_getcpu()");