source: mainline/uspace/lib/c/generic/tls.c@ 118a872

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

Fix amd64 not working properly. Clearly there is something to this alignment.

  • Property mode set to 100644
File size: 4.3 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 <align.h>
39#include <tls.h>
40#include <malloc.h>
41#include <str.h>
42#include <unistd.h>
43
44#ifdef CONFIG_RTLD
45#include <rtld/rtld.h>
46#endif
47
48size_t tls_get_size(void)
49{
50#ifdef CONFIG_RTLD
51 if (runtime_env != NULL)
52 return runtime_env->tls_size;
53#endif
54 return &_tbss_end - &_tdata_start;
55}
56
57/** Get address of static TLS block */
58void *tls_get(void)
59{
60#ifdef CONFIG_TLS_VARIANT_1
61 return (uint8_t *)__tcb_get() + sizeof(tcb_t);
62#else /* CONFIG_TLS_VARIANT_2 */
63 return (uint8_t *)__tcb_get() - tls_get_size();
64#endif
65}
66
67/** Create TLS (Thread Local Storage) data structures.
68 *
69 * @return Pointer to TCB.
70 */
71tcb_t *tls_make(void)
72{
73 void *data;
74 tcb_t *tcb;
75 size_t tls_size = &_tbss_end - &_tdata_start;
76
77#ifdef CONFIG_RTLD
78 if (runtime_env != NULL)
79 return rtld_tls_make(runtime_env);
80#endif
81 tcb = tls_alloc_arch(&data, tls_size);
82 if (!tcb)
83 return NULL;
84
85 /*
86 * Copy thread local data from the initialization image.
87 */
88 memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
89 /*
90 * Zero out the thread local uninitialized data.
91 */
92 memset(data + (&_tbss_start - &_tdata_start), 0,
93 &_tbss_end - &_tbss_start);
94
95 return tcb;
96}
97
98void tls_free(tcb_t *tcb)
99{
100#ifdef CONFIG_RTLD
101 free(tcb->dtv);
102#endif
103 tls_free_arch(tcb, tls_get_size());
104}
105
106#ifdef CONFIG_TLS_VARIANT_1
107/** Allocate TLS variant 1 data structures.
108 *
109 * @param data Start of TLS section. This is an output argument.
110 * @param size Size of tdata + tbss section.
111 * @return Pointer to tcb_t structure.
112 */
113tcb_t *tls_alloc_variant_1(void **data, size_t size)
114{
115 tcb_t *tcb;
116
117 tcb = malloc(sizeof(tcb_t) + size);
118 if (!tcb)
119 return NULL;
120 *data = ((void *)tcb) + sizeof(tcb_t);
121#ifdef CONFIG_RTLD
122 tcb->dtv = NULL;
123#endif
124
125 return tcb;
126}
127
128/** Free TLS variant I data structures.
129 *
130 * @param tcb Pointer to TCB structure.
131 * @param size This argument is ignored.
132 */
133void tls_free_variant_1(tcb_t *tcb, size_t size)
134{
135 free(tcb);
136}
137#endif
138
139#ifdef CONFIG_TLS_VARIANT_2
140/** Allocate TLS variant II data structures.
141 *
142 * @param data Pointer to pointer to thread local data. This is
143 * actually an output argument.
144 * @param size Size of thread local data.
145 * @return Pointer to TCB structure.
146 */
147tcb_t * tls_alloc_variant_2(void **data, size_t size)
148{
149 tcb_t *tcb;
150
151 size = ALIGN_UP(size, &_tls_alignment);
152 *data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size);
153 if (*data == NULL)
154 return NULL;
155 tcb = (tcb_t *) (*data + size);
156 tcb->self = tcb;
157#ifdef CONFIG_RTLD
158 tcb->dtv = NULL;
159#endif
160
161 return tcb;
162}
163
164/** Free TLS variant II data structures.
165 *
166 * @param tcb Pointer to TCB structure.
167 * @param size Size of thread local data.
168 */
169void tls_free_variant_2(tcb_t *tcb, size_t size)
170{
171 size = ALIGN_UP(size, &_tls_alignment);
172 void *start = ((void *) tcb) - size;
173 free(start);
174}
175#endif
176
177/** @}
178 */
Note: See TracBrowser for help on using the repository browser.