tls.c revision 1.5 1 /* $NetBSD: tls.c,v 1.5 2011/03/29 20:56:35 joerg 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.5 2011/03/29 20:56:35 joerg Exp $");
33
34 #include <sys/param.h>
35 #include <sys/ucontext.h>
36 #include <lwp.h>
37 #include <string.h>
38 #include "rtld.h"
39
40 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
41
42 static struct tls_tcb *_rtld_tls_allocate_locked(void);
43
44 #ifndef TLS_DTV_OFFSET
45 #define TLS_DTV_OFFSET 0
46 #endif
47
48 static size_t _rtld_tls_static_space; /* Static TLS space allocated */
49 static size_t _rtld_tls_static_offset; /* Next offset for static TLS to use */
50 size_t _rtld_tls_dtv_generation = 1;
51 size_t _rtld_tls_max_index = 1;
52
53 #define DTV_GENERATION(dtv) ((size_t)((dtv)[0]))
54 #define DTV_MAX_INDEX(dtv) ((size_t)((dtv)[-1]))
55 #define SET_DTV_GENERATION(dtv, val) (dtv)[0] = (void *)(size_t)(val)
56 #define SET_DTV_MAX_INDEX(dtv, val) (dtv)[-1] = (void *)(size_t)(val)
57
58 void *
59 _rtld_tls_get_addr(void *tls, size_t idx, size_t offset)
60 {
61 struct tls_tcb *tcb = tls;
62 void **dtv, **new_dtv;
63 sigset_t mask;
64
65 _rtld_exclusive_enter(&mask);
66
67 dtv = tcb->tcb_dtv;
68
69 if (__predict_false(DTV_GENERATION(dtv) != _rtld_tls_dtv_generation)) {
70 size_t to_copy = DTV_MAX_INDEX(dtv);
71
72 new_dtv = xcalloc((2 + _rtld_tls_max_index) * sizeof(*dtv));
73 ++new_dtv;
74 if (to_copy > _rtld_tls_max_index)
75 to_copy = _rtld_tls_max_index;
76 memcpy(new_dtv + 1, dtv + 1, to_copy * sizeof(*dtv));
77 xfree(dtv - 1);
78 dtv = tcb->tcb_dtv = new_dtv;
79 SET_DTV_MAX_INDEX(dtv, _rtld_tls_max_index);
80 SET_DTV_GENERATION(dtv, _rtld_tls_dtv_generation);
81 }
82
83 if (__predict_false(dtv[idx] == NULL))
84 dtv[idx] = _rtld_tls_module_allocate(idx);
85
86 _rtld_exclusive_exit(&mask);
87
88 return (uint8_t *)dtv[idx] + offset;
89 }
90
91 void
92 _rtld_tls_initial_allocation(void)
93 {
94 struct tls_tcb *tcb;
95
96 _rtld_tls_static_space = _rtld_tls_static_offset +
97 RTLD_STATIC_TLS_RESERVATION;
98
99 #ifndef __HAVE_TLS_VARIANT_I
100 _rtld_tls_static_space = roundup2(_rtld_tls_static_space,
101 sizeof(void *));
102 #endif
103
104 tcb = _rtld_tls_allocate_locked();
105 #ifdef __HAVE___LWP_SETTCB
106 __lwp_settcb(tcb);
107 #else
108 _lwp_setprivate(tcb);
109 #endif
110 }
111
112 static struct tls_tcb *
113 _rtld_tls_allocate_locked(void)
114 {
115 Obj_Entry *obj;
116 struct tls_tcb *tcb;
117 uint8_t *p, *q;
118
119 p = xcalloc(_rtld_tls_static_space + sizeof(struct tls_tcb));
120 #ifdef __HAVE_TLS_VARIANT_I
121 tcb = (struct tls_tcb *)p;
122 p += sizeof(struct tls_tcb);
123 #else
124 p += _rtld_tls_static_space;
125 tcb = (struct tls_tcb *)p;
126 tcb->tcb_self = tcb;
127 #endif
128 tcb->tcb_dtv = xcalloc(sizeof(*tcb->tcb_dtv) * (2 + _rtld_tls_max_index));
129 ++tcb->tcb_dtv;
130 SET_DTV_MAX_INDEX(tcb->tcb_dtv, _rtld_tls_max_index);
131 SET_DTV_GENERATION(tcb->tcb_dtv, _rtld_tls_dtv_generation);
132
133 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
134 if (obj->tlssize) {
135 #ifdef __HAVE_TLS_VARIANT_I
136 q = p + obj->tlsoffset;
137 #else
138 q = p - obj->tlsoffset;
139 #endif
140 memcpy(q, obj->tlsinit, obj->tlsinitsize);
141 tcb->tcb_dtv[obj->tlsindex] = q;
142 }
143 }
144
145 return tcb;
146 }
147
148 struct tls_tcb *
149 _rtld_tls_allocate(void)
150 {
151 struct tls_tcb *tcb;
152 sigset_t mask;
153
154 _rtld_exclusive_enter(&mask);
155 tcb = _rtld_tls_allocate_locked();
156 _rtld_exclusive_exit(&mask);
157
158 return tcb;
159 }
160
161 void
162 _rtld_tls_free(struct tls_tcb *tcb)
163 {
164 size_t i, max_index;
165 uint8_t *p;
166 sigset_t mask;
167
168 _rtld_exclusive_enter(&mask);
169
170 max_index = DTV_MAX_INDEX(tcb->tcb_dtv);
171 for (i = 1; i <= max_index; ++i)
172 xfree(tcb->tcb_dtv[i]);
173 xfree(tcb->tcb_dtv - 1);
174
175 #ifdef __HAVE_TLS_VARIANT_I
176 p = (uint8_t *)tcb;
177 #else
178 p = (uint8_t *)tcb - _rtld_tls_static_space;
179 #endif
180 xfree(p);
181
182 _rtld_exclusive_exit(&mask);
183 }
184
185 void *
186 _rtld_tls_module_allocate(size_t idx)
187 {
188 Obj_Entry *obj;
189 uint8_t *p;
190
191 for (obj = _rtld_objlist; obj != NULL; obj = obj->next) {
192 if (obj->tlsindex == idx)
193 break;
194 }
195 if (obj == NULL) {
196 _rtld_error("Module for TLS index %zu missing", idx);
197 _rtld_die();
198 }
199
200 p = xmalloc(obj->tlssize);
201 memcpy(p, obj->tlsinit, obj->tlsinitsize);
202 memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize);
203
204 return p;
205 }
206
207 int
208 _rtld_tls_offset_allocate(Obj_Entry *obj)
209 {
210 size_t offset, next_offset;
211
212 if (obj->tls_done)
213 return 0;
214 if (obj->tlssize == 0) {
215 obj->tlsoffset = 0;
216 obj->tls_done = 1;
217 return 0;
218 }
219
220 #ifdef __HAVE_TLS_VARIANT_I
221 offset = roundup2(_rtld_tls_static_offset, obj->tlsalign);
222 next_offset = offset + obj->tlssize;
223 #else
224 offset = roundup2(_rtld_tls_static_offset + obj->tlssize,
225 obj->tlsalign);
226 next_offset = offset;
227 #endif
228
229 /*
230 * Check if the static allocation was already done.
231 * This happens if dynamically loaded modules want to use
232 * static TLS space.
233 *
234 * XXX Keep an actual free list and callbacks for initialisation.
235 */
236 if (_rtld_tls_static_space) {
237 if (obj->tlsinitsize) {
238 _rtld_error("%s: Use of initialized "
239 "Thread Locale Storage with model initial-exec "
240 "and dlopen is not supported",
241 obj->path);
242 return -1;
243 }
244 if (next_offset > _rtld_tls_static_space) {
245 _rtld_error("%s: No space available "
246 "for static Thread Local Storage",
247 obj->path);
248 return -1;
249 }
250 }
251 obj->tlsoffset = offset;
252 _rtld_tls_static_offset = next_offset;
253 obj->tls_done = 1;
254
255 return 0;
256 }
257
258 void
259 _rtld_tls_offset_free(Obj_Entry *obj)
260 {
261
262 /*
263 * XXX See above.
264 */
265 obj->tls_done = 0;
266 return;
267 }
268
269 #ifdef __HAVE_COMMON___TLS_GET_ADDR
270 /*
271 * The fast path is access to an already allocated DTV entry.
272 * This checks the current limit and the entry without needing any
273 * locking. Entries are only freed on dlclose() and it is an application
274 * bug if code of the module is still running at that point.
275 */
276 void *
277 __tls_get_addr(void *arg_)
278 {
279 size_t *arg = (size_t *)arg_;
280 void **dtv;
281 #ifdef __HAVE___LWP_GETTCB_FAST
282 struct tls_tcb * const tcb = __lwp_gettcb_fast();
283 #else
284 struct tls_tcb * const tcb = __lwp_getprivate_fast();
285 #endif
286 size_t idx = arg[0], offset = arg[1] + TLS_DTV_OFFSET;
287
288 dtv = tcb->tcb_dtv;
289
290 if (__predict_true(idx < DTV_MAX_INDEX(dtv) && dtv[idx] != NULL))
291 return (uint8_t *)dtv[idx] + offset;
292
293 return _rtld_tls_get_addr(tcb, idx, offset);
294 }
295 #endif
296
297 #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */
298