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