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