tls.c revision 1.17 1 /* $NetBSD: tls.c,v 1.17 2023/06/01 08:20:10 riastradh Exp $ */
2 /*-
3 * Copyright (c) 2011 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Joerg Sonnenberger.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: tls.c,v 1.17 2023/06/01 08:20:10 riastradh Exp $");
33
34 #include <sys/param.h>
35 #include <sys/ucontext.h>
36 #include <lwp.h>
37 #include <stdalign.h>
38 #include <stddef.h>
39 #include <string.h>
40 #include "debug.h"
41 #include "rtld.h"
42
43 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
44
45 static struct tls_tcb *_rtld_tls_allocate_locked(void);
46
47 #ifndef TLS_DTV_OFFSET
48 #define TLS_DTV_OFFSET 0
49 #endif
50
51 static size_t _rtld_tls_static_space; /* Static TLS space allocated */
52 static size_t _rtld_tls_static_offset; /* Next offset for static TLS to use */
53 size_t _rtld_tls_dtv_generation = 1;
54 size_t _rtld_tls_max_index = 1;
55
56 #define DTV_GENERATION(dtv) ((size_t)((dtv)[0]))
57 #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
58 #define SET_DTV_GENERATION(dtv, val) (dtv)[0] = (void *)(size_t)(val)
59 #define SET_DTV_MAX_INDEX(dtv, val) (dtv)[-1] = (void *)(size_t)(val)
60
61 void *
62 _rtld_tls_get_addr(void *tls, size_t idx, size_t offset)
63 {
64 struct tls_tcb *tcb = tls;
65 void **dtv, **new_dtv;
66 sigset_t mask;
67
68 _rtld_exclusive_enter(&mask);
69
70 dtv = tcb->tcb_dtv;
71
72 if (__predict_false(DTV_GENERATION(dtv) != _rtld_tls_dtv_generation)) {
73 size_t to_copy = DTV_MAX_INDEX(dtv);
74
75 new_dtv = xcalloc((2 + _rtld_tls_max_index) * sizeof(*dtv));
76 ++new_dtv;
77 if (to_copy > _rtld_tls_max_index)
78 to_copy = _rtld_tls_max_index;
79 memcpy(new_dtv + 1, dtv + 1, to_copy * sizeof(*dtv));
80 xfree(dtv - 1);
81 dtv = tcb->tcb_dtv = new_dtv;
82 SET_DTV_MAX_INDEX(dtv, _rtld_tls_max_index);
83 SET_DTV_GENERATION(dtv, _rtld_tls_dtv_generation);
84 }
85
86 if (__predict_false(dtv[idx] == NULL))
87 dtv[idx] = _rtld_tls_module_allocate(idx);
88
89 _rtld_exclusive_exit(&mask);
90
91 return (uint8_t *)dtv[idx] + offset;
92 }
93
94 void
95 _rtld_tls_initial_allocation(void)
96 {
97 struct tls_tcb *tcb;
98
99 _rtld_tls_static_space = _rtld_tls_static_offset +
100 RTLD_STATIC_TLS_RESERVATION;
101
102 #ifndef __HAVE_TLS_VARIANT_I
103 _rtld_tls_static_space = roundup2(_rtld_tls_static_space,
104 alignof(max_align_t));
105 #endif
106 dbg(("_rtld_tls_static_space %zu", _rtld_tls_static_space));
107
108 tcb = _rtld_tls_allocate_locked();
109 #ifdef __HAVE___LWP_SETTCB
110 __lwp_settcb(tcb);
111 #else
112 _lwp_setprivate(tcb);
113 #endif
114 }
115
116 static struct tls_tcb *
117 _rtld_tls_allocate_locked(void)
118 {
119 Obj_Entry *obj;
120 struct tls_tcb *tcb;
121 uint8_t *p, *q;
122
123 p = xcalloc(_rtld_tls_static_space + sizeof(struct tls_tcb));
124 #ifdef __HAVE_TLS_VARIANT_I
125 tcb = (struct tls_tcb *)p;
126 p += sizeof(struct tls_tcb);
127 #else
128 p += _rtld_tls_static_space;
129 tcb = (struct tls_tcb *)p;
130 tcb->tcb_self = tcb;
131 #endif
132 dbg(("lwp %d tls tcb %p", _lwp_self(), tcb));
133 tcb->tcb_dtv = xcalloc(sizeof(*tcb->tcb_dtv) * (2 + _rtld_tls_max_index));
134 ++tcb->tcb_dtv;
135 SET_DTV_MAX_INDEX(tcb->tcb_dtv, _rtld_tls_max_index);
136 SET_DTV_GENERATION(tcb->tcb_dtv, _rtld_tls_dtv_generation);
137
138 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
139 if (obj->tls_done) {
140 #ifdef __HAVE_TLS_VARIANT_I
141 q = p + obj->tlsoffset;
142 #else
143 q = p - obj->tlsoffset;
144 #endif
145 dbg(("%s: [lwp %d] tls dtv %p index %zu offset %zu",
146 obj->path, _lwp_self(),
147 q, obj->tlsindex, obj->tlsoffset));
148 if (obj->tlsinitsize)
149 memcpy(q, obj->tlsinit, obj->tlsinitsize);
150 tcb->tcb_dtv[obj->tlsindex] = q;
151 }
152 }
153
154 return tcb;
155 }
156
157 struct tls_tcb *
158 _rtld_tls_allocate(void)
159 {
160 struct tls_tcb *tcb;
161 sigset_t mask;
162
163 _rtld_exclusive_enter(&mask);
164 tcb = _rtld_tls_allocate_locked();
165 _rtld_exclusive_exit(&mask);
166
167 return tcb;
168 }
169
170 void
171 _rtld_tls_free(struct tls_tcb *tcb)
172 {
173 size_t i, max_index;
174 uint8_t *p, *p_end;
175 sigset_t mask;
176
177 _rtld_exclusive_enter(&mask);
178
179 #ifdef __HAVE_TLS_VARIANT_I
180 p = (uint8_t *)tcb;
181 #else
182 p = (uint8_t *)tcb - _rtld_tls_static_space;
183 #endif
184 p_end = p + _rtld_tls_static_space;
185
186 max_index = DTV_MAX_INDEX(tcb->tcb_dtv);
187 for (i = 1; i <= max_index; ++i) {
188 if ((uint8_t *)tcb->tcb_dtv[i] < p ||
189 (uint8_t *)tcb->tcb_dtv[i] >= p_end)
190 xfree(tcb->tcb_dtv[i]);
191 }
192 xfree(tcb->tcb_dtv - 1);
193 xfree(p);
194
195 _rtld_exclusive_exit(&mask);
196 }
197
198 void *
199 _rtld_tls_module_allocate(size_t idx)
200 {
201 Obj_Entry *obj;
202 uint8_t *p;
203
204 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
205 if (obj->tlsindex == idx)
206 break;
207 }
208 if (obj == NULL) {
209 _rtld_error("Module for TLS index %zu missing", idx);
210 _rtld_die();
211 }
212
213 p = xmalloc(obj->tlssize);
214 memcpy(p, obj->tlsinit, obj->tlsinitsize);
215 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
216
217 return p;
218 }
219
220 int
221 _rtld_tls_offset_allocate(Obj_Entry *obj)
222 {
223 size_t offset, next_offset;
224
225 if (obj->tls_done)
226 return 0;
227 if (obj->tlssize == 0) {
228 obj->tlsoffset = 0;
229 obj->tls_done = 1;
230 return 0;
231 }
232
233 #ifdef __HAVE_TLS_VARIANT_I
234 offset = roundup2(_rtld_tls_static_offset, obj->tlsalign);
235 next_offset = offset + obj->tlssize;
236 #else
237 offset = roundup2(_rtld_tls_static_offset + obj->tlssize,
238 obj->tlsalign);
239 next_offset = offset;
240 #endif
241
242 /*
243 * Check if the static allocation was already done.
244 * This happens if dynamically loaded modules want to use
245 * static TLS space.
246 *
247 * XXX Keep an actual free list and callbacks for initialisation.
248 */
249 if (_rtld_tls_static_space) {
250 if (obj->tlsinitsize) {
251 _rtld_error("%s: Use of initialized "
252 "Thread Local Storage with model initial-exec "
253 "and dlopen is not supported",
254 obj->path);
255 return -1;
256 }
257 if (next_offset > _rtld_tls_static_space) {
258 _rtld_error("%s: No space available "
259 "for static Thread Local Storage",
260 obj->path);
261 return -1;
262 }
263 }
264 obj->tlsoffset = offset;
265 dbg(("%s: static tls offset 0x%zx size %zu\n",
266 obj->path, obj->tlsoffset, obj->tlssize));
267 _rtld_tls_static_offset = next_offset;
268 obj->tls_done = 1;
269
270 return 0;
271 }
272
273 void
274 _rtld_tls_offset_free(Obj_Entry *obj)
275 {
276
277 /*
278 * XXX See above.
279 */
280 obj->tls_done = 0;
281 return;
282 }
283
284 #if defined(__HAVE_COMMON___TLS_GET_ADDR) && defined(RTLD_LOADER)
285 /*
286 * The fast path is access to an already allocated DTV entry.
287 * This checks the current limit and the entry without needing any
288 * locking. Entries are only freed on dlclose() and it is an application
289 * bug if code of the module is still running at that point.
290 */
291 void *
292 __tls_get_addr(void *arg_)
293 {
294 size_t *arg = (size_t *)arg_;
295 void **dtv;
296 #ifdef __HAVE___LWP_GETTCB_FAST
297 struct tls_tcb * const tcb = __lwp_gettcb_fast();
298 #else
299 struct tls_tcb * const tcb = __lwp_getprivate_fast();
300 #endif
301 size_t idx = arg[0], offset = arg[1] + TLS_DTV_OFFSET;
302
303 dtv = tcb->tcb_dtv;
304
305 if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
306 return (uint8_t *)dtv[idx] + offset;
307
308 return _rtld_tls_get_addr(tcb, idx, offset);
309 }
310 #endif
311
312 #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */
313