Commit | Line | Data |
---|---|---|
744d0b8b | 1 | // SPDX-License-Identifier: LGPL-2.1-only |
784b0012 MD |
2 | /* |
3 | * rseq.c | |
4 | * | |
5 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | */ | |
2cbca301 | 17 | #ifndef _GNU_SOURCE |
784b0012 | 18 | #define _GNU_SOURCE |
2cbca301 | 19 | #endif |
784b0012 MD |
20 | #include <errno.h> |
21 | #include <sched.h> | |
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <unistd.h> | |
26 | #include <syscall.h> | |
27 | #include <assert.h> | |
28 | #include <signal.h> | |
0ceae74a | 29 | #include <limits.h> |
9698c399 | 30 | #include <dlfcn.h> |
784b0012 MD |
31 | |
32 | #include <rseq/rseq.h> | |
33 | ||
34 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | |
35 | ||
9698c399 MD |
36 | static const int *libc_rseq_offset_p; |
37 | static const unsigned int *libc_rseq_size_p; | |
38 | static const unsigned int *libc_rseq_flags_p; | |
39 | ||
40 | /* Offset from the thread pointer to the rseq area. */ | |
41 | int rseq_offset; | |
42 | ||
43 | /* Size of the registered rseq area. 0 if the registration was | |
44 | unsuccessful. */ | |
45 | unsigned int rseq_size = -1U; | |
46 | ||
47 | /* Flags used during rseq registration. */ | |
48 | unsigned int rseq_flags; | |
49 | ||
50 | static int rseq_ownership; | |
51 | ||
52 | static | |
2d533093 MD |
53 | __thread struct rseq_abi __rseq_abi __attribute__((tls_model("initial-exec"))) = { |
54 | .cpu_id = RSEQ_ABI_CPU_ID_UNINITIALIZED, | |
784b0012 MD |
55 | }; |
56 | ||
2d533093 | 57 | static int sys_rseq(struct rseq_abi *rseq_abi, uint32_t rseq_len, |
52e82b87 MD |
58 | int flags, uint32_t sig) |
59 | { | |
60 | return syscall(__NR_rseq, rseq_abi, rseq_len, flags, sig); | |
61 | } | |
62 | ||
63 | int rseq_available(void) | |
64 | { | |
65 | int rc; | |
66 | ||
a5fd0584 | 67 | rc = sys_rseq(NULL, 0, 0, 0); |
52e82b87 MD |
68 | if (rc != -1) |
69 | abort(); | |
70 | switch (errno) { | |
71 | case ENOSYS: | |
72 | return 0; | |
73 | case EINVAL: | |
74 | return 1; | |
75 | default: | |
76 | abort(); | |
77 | } | |
78 | } | |
79 | ||
9698c399 | 80 | int rseq_register_current_thread(void) |
784b0012 | 81 | { |
9698c399 | 82 | int rc; |
784b0012 | 83 | |
9698c399 MD |
84 | if (!rseq_ownership) { |
85 | /* Treat libc's ownership as a successful registration. */ | |
86 | return 0; | |
87 | } | |
2d533093 | 88 | rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), 0, RSEQ_SIG); |
9698c399 MD |
89 | if (rc) |
90 | return -1; | |
91 | assert(rseq_current_cpu_raw() >= 0); | |
92 | return 0; | |
784b0012 MD |
93 | } |
94 | ||
9698c399 | 95 | int rseq_unregister_current_thread(void) |
784b0012 | 96 | { |
9698c399 | 97 | int rc; |
784b0012 | 98 | |
9698c399 MD |
99 | if (!rseq_ownership) { |
100 | /* Treat libc's ownership as a successful unregistration. */ | |
101 | return 0; | |
102 | } | |
2d533093 | 103 | rc = sys_rseq(&__rseq_abi, sizeof(struct rseq_abi), RSEQ_ABI_FLAG_UNREGISTER, RSEQ_SIG); |
9698c399 MD |
104 | if (rc) |
105 | return -1; | |
106 | return 0; | |
784b0012 MD |
107 | } |
108 | ||
9698c399 MD |
109 | static __attribute__((constructor)) |
110 | void rseq_init(void) | |
784b0012 | 111 | { |
9698c399 MD |
112 | libc_rseq_offset_p = dlsym(RTLD_NEXT, "__rseq_offset"); |
113 | libc_rseq_size_p = dlsym(RTLD_NEXT, "__rseq_size"); | |
114 | libc_rseq_flags_p = dlsym(RTLD_NEXT, "__rseq_flags"); | |
115 | if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p) { | |
116 | /* rseq registration owned by glibc */ | |
117 | rseq_offset = *libc_rseq_offset_p; | |
118 | rseq_size = *libc_rseq_size_p; | |
119 | rseq_flags = *libc_rseq_flags_p; | |
120 | return; | |
0ceae74a | 121 | } |
9698c399 MD |
122 | if (!rseq_available()) |
123 | return; | |
124 | rseq_ownership = 1; | |
125 | rseq_offset = (void *)&__rseq_abi - rseq_thread_pointer(); | |
2d533093 | 126 | rseq_size = sizeof(struct rseq_abi); |
9698c399 | 127 | rseq_flags = 0; |
784b0012 MD |
128 | } |
129 | ||
9698c399 MD |
130 | static __attribute__((destructor)) |
131 | void rseq_exit(void) | |
784b0012 | 132 | { |
9698c399 MD |
133 | if (!rseq_ownership) |
134 | return; | |
135 | rseq_offset = 0; | |
136 | rseq_size = -1U; | |
137 | rseq_ownership = 0; | |
784b0012 MD |
138 | } |
139 | ||
140 | int32_t rseq_fallback_current_cpu(void) | |
141 | { | |
142 | int32_t cpu; | |
143 | ||
144 | cpu = sched_getcpu(); | |
145 | if (cpu < 0) { | |
146 | perror("sched_getcpu()"); | |
147 | abort(); | |
148 | } | |
149 | return cpu; | |
150 | } |