2
* Copyright (c) 2006 Jakub Jermar
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
34
* Support for thread-local storage, as described in:
35
* Drepper U.: ELF Handling For Thread-Local Storage, 2005
37
* Only static model is supported.
45
/** Create TLS (Thread Local Storage) data structures.
47
* The code requires, that sections .tdata and .tbss are adjacent. It may be
48
* changed in the future.
50
* @return Pointer to TCB.
52
tcb_t *__make_tls(void)
56
size_t tls_size = &_tbss_end - &_tdata_start;
58
tcb = __alloc_tls(&data, tls_size);
61
* Copy thread local data from the initialization image.
63
memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
65
* Zero out the thread local uninitialized data.
67
memset(data + (&_tbss_start - &_tdata_start), 0,
68
&_tbss_end - &_tbss_start);
73
void __free_tls(tcb_t *tcb)
75
size_t tls_size = &_tbss_end - &_tdata_start;
76
__free_tls_arch(tcb, tls_size);
79
#ifdef CONFIG_TLS_VARIANT_1
80
/** Allocate TLS variant 1 data structures.
82
* @param data Start of TLS section. This is an output argument.
83
* @param size Size of tdata + tbss section.
84
* @return Pointer to tcb_t structure.
86
tcb_t *tls_alloc_variant_1(void **data, size_t size)
90
result = malloc(sizeof(tcb_t) + size);
91
*data = ((void *)result) + sizeof(tcb_t);
95
/** Free TLS variant I data structures.
97
* @param tcb Pointer to TCB structure.
98
* @param size This argument is ignored.
100
void tls_free_variant_1(tcb_t *tcb, size_t size)
106
#ifdef CONFIG_TLS_VARIANT_2
107
/** Allocate TLS variant II data structures.
109
* @param data Pointer to pointer to thread local data. This is
110
* actually an output argument.
111
* @param size Size of thread local data.
112
* @return Pointer to TCB structure.
114
tcb_t * tls_alloc_variant_2(void **data, size_t size)
118
size = ALIGN_UP(size, &_tls_alignment);
119
*data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size);
121
tcb = (tcb_t *) (*data + size);
127
/** Free TLS variant II data structures.
129
* @param tcb Pointer to TCB structure.
130
* @param size Size of thread local data.
132
void tls_free_variant_2(tcb_t *tcb, size_t size)
134
size = ALIGN_UP(size, &_tls_alignment);
135
void *start = ((void *) tcb) - size;