xref: /minix3/lib/libc/tls/tls.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: tls.c,v 1.8 2014/12/14 23:49:17 chs Exp $	*/
2f14fb602SLionel Sambuc 
3f14fb602SLionel Sambuc /*-
4f14fb602SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5f14fb602SLionel Sambuc  * All rights reserved.
6f14fb602SLionel Sambuc  *
7f14fb602SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8f14fb602SLionel Sambuc  * by Joerg Sonnenberger.
9f14fb602SLionel Sambuc  *
10f14fb602SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11f14fb602SLionel Sambuc  * modification, are permitted provided that the following conditions
12f14fb602SLionel Sambuc  * are met:
13f14fb602SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14f14fb602SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15f14fb602SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16f14fb602SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17f14fb602SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18f14fb602SLionel Sambuc  *
19f14fb602SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f14fb602SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f14fb602SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f14fb602SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f14fb602SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f14fb602SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f14fb602SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f14fb602SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f14fb602SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f14fb602SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f14fb602SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30f14fb602SLionel Sambuc  */
31f14fb602SLionel Sambuc 
32f14fb602SLionel Sambuc #include <sys/cdefs.h>
33*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: tls.c,v 1.8 2014/12/14 23:49:17 chs Exp $");
34f14fb602SLionel Sambuc 
35f14fb602SLionel Sambuc #include "namespace.h"
36f14fb602SLionel Sambuc 
37f14fb602SLionel Sambuc #define	_rtld_tls_allocate	__libc_rtld_tls_allocate
38f14fb602SLionel Sambuc #define	_rtld_tls_free		__libc_rtld_tls_free
39f14fb602SLionel Sambuc 
40f14fb602SLionel Sambuc #include <sys/tls.h>
41f14fb602SLionel Sambuc 
42f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
43f14fb602SLionel Sambuc 
44f14fb602SLionel Sambuc #include <sys/param.h>
45f14fb602SLionel Sambuc #include <sys/mman.h>
46f14fb602SLionel Sambuc #include <link_elf.h>
47f14fb602SLionel Sambuc #include <lwp.h>
48f14fb602SLionel Sambuc #include <stddef.h>
49f14fb602SLionel Sambuc #include <stdlib.h>
50f14fb602SLionel Sambuc #include <string.h>
51f14fb602SLionel Sambuc #include <unistd.h>
52f14fb602SLionel Sambuc 
53f14fb602SLionel Sambuc __dso_hidden void	__libc_static_tls_setup(void);
54f14fb602SLionel Sambuc 
55f14fb602SLionel Sambuc static const void *tls_initaddr;
56f14fb602SLionel Sambuc static size_t tls_initsize;
57f14fb602SLionel Sambuc static size_t tls_size;
58f14fb602SLionel Sambuc static size_t tls_allocation;
59f14fb602SLionel Sambuc static void *initial_thread_tcb;
60f14fb602SLionel Sambuc 
61f14fb602SLionel Sambuc void * __libc_tls_get_addr(void);
62f14fb602SLionel Sambuc 
__weak_alias(__tls_get_addr,__libc_tls_get_addr)63f14fb602SLionel Sambuc __weak_alias(__tls_get_addr, __libc_tls_get_addr)
64f14fb602SLionel Sambuc #ifdef __i386__
65f14fb602SLionel Sambuc __weak_alias(___tls_get_addr, __libc_tls_get_addr)
66f14fb602SLionel Sambuc #endif
67f14fb602SLionel Sambuc 
68f14fb602SLionel Sambuc void *
69f14fb602SLionel Sambuc __libc_tls_get_addr(void)
70f14fb602SLionel Sambuc {
71f14fb602SLionel Sambuc 
72f14fb602SLionel Sambuc 	abort();
73f14fb602SLionel Sambuc 	/* NOTREACHED */
74f14fb602SLionel Sambuc }
75f14fb602SLionel Sambuc 
__weak_alias(_rtld_tls_allocate,__libc_rtld_tls_allocate)76f14fb602SLionel Sambuc __weak_alias(_rtld_tls_allocate, __libc_rtld_tls_allocate)
77f14fb602SLionel Sambuc 
78f14fb602SLionel Sambuc struct tls_tcb *
79f14fb602SLionel Sambuc _rtld_tls_allocate(void)
80f14fb602SLionel Sambuc {
81f14fb602SLionel Sambuc 	struct tls_tcb *tcb;
82f14fb602SLionel Sambuc 	uint8_t *p;
83f14fb602SLionel Sambuc 
84f14fb602SLionel Sambuc 	if (initial_thread_tcb == NULL) {
85f14fb602SLionel Sambuc #ifdef __HAVE_TLS_VARIANT_II
86f14fb602SLionel Sambuc 		tls_size = roundup2(tls_size, sizeof(void *));
87f14fb602SLionel Sambuc #endif
88f14fb602SLionel Sambuc 		tls_allocation = tls_size + sizeof(*tcb);
89f14fb602SLionel Sambuc 
90f14fb602SLionel Sambuc 		initial_thread_tcb = p = mmap(NULL, tls_allocation,
91f14fb602SLionel Sambuc 		    PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
92f14fb602SLionel Sambuc 	} else {
93f14fb602SLionel Sambuc 		p = calloc(1, tls_allocation);
94f14fb602SLionel Sambuc 	}
95f14fb602SLionel Sambuc 	if (p == NULL) {
96f14fb602SLionel Sambuc 		static const char msg[] =  "TLS allocation failed, terminating\n";
97f14fb602SLionel Sambuc 		write(STDERR_FILENO, msg, sizeof(msg));
98f14fb602SLionel Sambuc 		_exit(127);
99f14fb602SLionel Sambuc 	}
100f14fb602SLionel Sambuc #ifdef __HAVE_TLS_VARIANT_I
101f14fb602SLionel Sambuc 	/* LINTED */
102f14fb602SLionel Sambuc 	tcb = (struct tls_tcb *)p;
103f14fb602SLionel Sambuc 	p += sizeof(struct tls_tcb);
104f14fb602SLionel Sambuc #else
105f14fb602SLionel Sambuc 	/* LINTED tls_size is rounded above */
106f14fb602SLionel Sambuc 	tcb = (struct tls_tcb *)(p + tls_size);
107f14fb602SLionel Sambuc 	tcb->tcb_self = tcb;
108f14fb602SLionel Sambuc #endif
109f14fb602SLionel Sambuc 	memcpy(p, tls_initaddr, tls_initsize);
110f14fb602SLionel Sambuc 
111f14fb602SLionel Sambuc 	return tcb;
112f14fb602SLionel Sambuc }
113f14fb602SLionel Sambuc 
__weak_alias(_rtld_tls_free,__libc_rtld_tls_free)114f14fb602SLionel Sambuc __weak_alias(_rtld_tls_free, __libc_rtld_tls_free)
115f14fb602SLionel Sambuc 
116f14fb602SLionel Sambuc void
117f14fb602SLionel Sambuc _rtld_tls_free(struct tls_tcb *tcb)
118f14fb602SLionel Sambuc {
119f14fb602SLionel Sambuc 	uint8_t *p;
120f14fb602SLionel Sambuc 
121f14fb602SLionel Sambuc #ifdef __HAVE_TLS_VARIANT_I
122f14fb602SLionel Sambuc 	/* LINTED */
123f14fb602SLionel Sambuc 	p = (uint8_t *)tcb;
124f14fb602SLionel Sambuc #else
125f14fb602SLionel Sambuc 	/* LINTED */
126f14fb602SLionel Sambuc 	p = (uint8_t *)tcb - tls_size;
127f14fb602SLionel Sambuc #endif
128f14fb602SLionel Sambuc 	if (p == initial_thread_tcb)
129f14fb602SLionel Sambuc 		munmap(p, tls_allocation);
130f14fb602SLionel Sambuc 	else
131f14fb602SLionel Sambuc 		free(p);
132f14fb602SLionel Sambuc }
133f14fb602SLionel Sambuc 
134f14fb602SLionel Sambuc __weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC);
135f14fb602SLionel Sambuc 
13684d9c625SLionel Sambuc static int __section(".text.startup")
__libc_static_tls_setup_cb(struct dl_phdr_info * data,size_t len,void * cookie)137f14fb602SLionel Sambuc __libc_static_tls_setup_cb(struct dl_phdr_info *data, size_t len, void *cookie)
138f14fb602SLionel Sambuc {
139f14fb602SLionel Sambuc 	const Elf_Phdr *phdr = data->dlpi_phdr;
140f14fb602SLionel Sambuc 	const Elf_Phdr *phlimit = data->dlpi_phdr + data->dlpi_phnum;
141f14fb602SLionel Sambuc 
142f14fb602SLionel Sambuc 	for (; phdr < phlimit; ++phdr) {
143f14fb602SLionel Sambuc 		if (phdr->p_type != PT_TLS)
144f14fb602SLionel Sambuc 			continue;
145f14fb602SLionel Sambuc 		tls_initaddr = (void *)(phdr->p_vaddr + data->dlpi_addr);
146f14fb602SLionel Sambuc 		tls_initsize = phdr->p_filesz;
147f14fb602SLionel Sambuc 		tls_size = phdr->p_memsz;
148f14fb602SLionel Sambuc 	}
149f14fb602SLionel Sambuc 	return 0;
150f14fb602SLionel Sambuc }
151f14fb602SLionel Sambuc 
152f14fb602SLionel Sambuc void
__libc_static_tls_setup(void)153f14fb602SLionel Sambuc __libc_static_tls_setup(void)
154f14fb602SLionel Sambuc {
155f14fb602SLionel Sambuc 	struct tls_tcb *tcb;
156f14fb602SLionel Sambuc 
157f14fb602SLionel Sambuc 	if (&rtld_DYNAMIC != NULL) {
158f14fb602SLionel Sambuc 		return;
159f14fb602SLionel Sambuc 	}
160f14fb602SLionel Sambuc 
161f14fb602SLionel Sambuc 	dl_iterate_phdr(__libc_static_tls_setup_cb, NULL);
162f14fb602SLionel Sambuc 
163f14fb602SLionel Sambuc 	tcb = _rtld_tls_allocate();
164f14fb602SLionel Sambuc #ifdef __HAVE___LWP_SETTCB
165f14fb602SLionel Sambuc 	__lwp_settcb(tcb);
166f14fb602SLionel Sambuc #else
167f14fb602SLionel Sambuc 	_lwp_setprivate(tcb);
168f14fb602SLionel Sambuc #endif
169f14fb602SLionel Sambuc }
170f14fb602SLionel Sambuc 
171f14fb602SLionel Sambuc #endif /* __HAVE_TLS_VARIANT_I || __HAVE_TLS_VARIANT_II */
172