source: mainline/uspace/lib/c/generic/tls.c@ 7c4b26c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c4b26c was 6adb775f, checked in by Jiri Svoboda <jiri@…>, 10 years ago

TLS for dynamically linked executables and initially loaded DSOs (but must not call dlopen or there will be trouble).

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 * Copyright (c) 2006 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/** @file
33 *
34 * Support for thread-local storage, as described in:
35 * Drepper U.: ELF Handling For Thread-Local Storage, 2005
36 */
37
38#include <tls.h>
39#include <malloc.h>
40#include <str.h>
41#include <unistd.h>
42
43#ifdef CONFIG_RTLD
44#include <rtld/rtld.h>
45#endif
46
47size_t tls_get_size(void)
48{
49 return &_tbss_end - &_tdata_start;
50}
51
52/** Create TLS (Thread Local Storage) data structures.
53 *
54 * @return Pointer to TCB.
55 */
56tcb_t *tls_make(void)
57{
58 void *data;
59 tcb_t *tcb;
60 size_t tls_size = &_tbss_end - &_tdata_start;
61
62#ifdef CONFIG_RTLD
63 if (runtime_env != NULL)
64 return rtld_tls_make(runtime_env);
65#endif
66 tcb = tls_alloc_arch(&data, tls_size);
67 if (!tcb)
68 return NULL;
69
70 /*
71 * Copy thread local data from the initialization image.
72 */
73 memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
74 /*
75 * Zero out the thread local uninitialized data.
76 */
77 memset(data + (&_tbss_start - &_tdata_start), 0,
78 &_tbss_end - &_tbss_start);
79
80 return tcb;
81}
82
83void tls_free(tcb_t *tcb)
84{
85 size_t tls_size = &_tbss_end - &_tdata_start;
86
87#ifdef CONFIG_RTLD
88 if (runtime_env != NULL)
89 tls_size = runtime_env->tls_size;
90#endif
91 tls_free_arch(tcb, tls_size);
92}
93
94#ifdef CONFIG_TLS_VARIANT_1
95/** Allocate TLS variant 1 data structures.
96 *
97 * @param data Start of TLS section. This is an output argument.
98 * @param size Size of tdata + tbss section.
99 * @return Pointer to tcb_t structure.
100 */
101tcb_t *tls_alloc_variant_1(void **data, size_t size)
102{
103 tcb_t *result;
104
105 result = malloc(sizeof(tcb_t) + size);
106 if (!result)
107 return NULL;
108 *data = ((void *)result) + sizeof(tcb_t);
109
110 return result;
111}
112
113/** Free TLS variant I data structures.
114 *
115 * @param tcb Pointer to TCB structure.
116 * @param size This argument is ignored.
117 */
118void tls_free_variant_1(tcb_t *tcb, size_t size)
119{
120 free(tcb);
121}
122#endif
123
124#ifdef CONFIG_TLS_VARIANT_2
125/** Allocate TLS variant II data structures.
126 *
127 * @param data Pointer to pointer to thread local data. This is
128 * actually an output argument.
129 * @param size Size of thread local data.
130 * @return Pointer to TCB structure.
131 */
132tcb_t * tls_alloc_variant_2(void **data, size_t size)
133{
134 tcb_t *tcb;
135
136 *data = malloc(sizeof(tcb_t) + size);
137 if (*data == NULL)
138 return NULL;
139 tcb = (tcb_t *) (*data + size);
140 tcb->self = tcb;
141
142 return tcb;
143}
144
145/** Free TLS variant II data structures.
146 *
147 * @param tcb Pointer to TCB structure.
148 * @param size Size of thread local data.
149 */
150void tls_free_variant_2(tcb_t *tcb, size_t size)
151{
152 void *start = ((void *) tcb) - size;
153 free(start);
154}
155#endif
156
157/** @}
158 */
Note: See TracBrowser for help on using the repository browser.