source: mainline/uspace/lib/c/generic/tls.c@ 39330200

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

Prefer to get memory allocation functions through the standard stdlib header.

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