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