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 | * Only static model is supported.
|
---|
38 | */
|
---|
39 |
|
---|
40 | #include <tls.h>
|
---|
41 | #include <malloc.h>
|
---|
42 | #include <str.h>
|
---|
43 | #include <align.h>
|
---|
44 | #include <unistd.h>
|
---|
45 |
|
---|
46 | /** Create TLS (Thread Local Storage) data structures.
|
---|
47 | *
|
---|
48 | * The code requires, that sections .tdata and .tbss are adjacent. It may be
|
---|
49 | * changed in the future.
|
---|
50 | *
|
---|
51 | * @return Pointer to TCB.
|
---|
52 | */
|
---|
53 | tcb_t *__make_tls(void)
|
---|
54 | {
|
---|
55 | void *data;
|
---|
56 | tcb_t *tcb;
|
---|
57 | size_t tls_size = &_tbss_end - &_tdata_start;
|
---|
58 |
|
---|
59 | tcb = __alloc_tls(&data, tls_size);
|
---|
60 | if (!tcb)
|
---|
61 | return NULL;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Copy thread local data from the initialization image.
|
---|
65 | */
|
---|
66 | memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start);
|
---|
67 | /*
|
---|
68 | * Zero out the thread local uninitialized data.
|
---|
69 | */
|
---|
70 | memset(data + (&_tbss_start - &_tdata_start), 0,
|
---|
71 | &_tbss_end - &_tbss_start);
|
---|
72 |
|
---|
73 | return tcb;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void __free_tls(tcb_t *tcb)
|
---|
77 | {
|
---|
78 | size_t tls_size = &_tbss_end - &_tdata_start;
|
---|
79 | __free_tls_arch(tcb, tls_size);
|
---|
80 | }
|
---|
81 |
|
---|
82 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
83 | /** Allocate TLS variant 1 data structures.
|
---|
84 | *
|
---|
85 | * @param data Start of TLS section. This is an output argument.
|
---|
86 | * @param size Size of tdata + tbss section.
|
---|
87 | * @return Pointer to tcb_t structure.
|
---|
88 | */
|
---|
89 | tcb_t *tls_alloc_variant_1(void **data, size_t size)
|
---|
90 | {
|
---|
91 | tcb_t *result;
|
---|
92 |
|
---|
93 | result = malloc(sizeof(tcb_t) + size);
|
---|
94 | if (!result)
|
---|
95 | return NULL;
|
---|
96 | *data = ((void *)result) + sizeof(tcb_t);
|
---|
97 |
|
---|
98 | return result;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** Free TLS variant I data structures.
|
---|
102 | *
|
---|
103 | * @param tcb Pointer to TCB structure.
|
---|
104 | * @param size This argument is ignored.
|
---|
105 | */
|
---|
106 | void tls_free_variant_1(tcb_t *tcb, size_t size)
|
---|
107 | {
|
---|
108 | free(tcb);
|
---|
109 | }
|
---|
110 | #endif
|
---|
111 |
|
---|
112 | #ifdef CONFIG_TLS_VARIANT_2
|
---|
113 | /** Allocate TLS variant II data structures.
|
---|
114 | *
|
---|
115 | * @param data Pointer to pointer to thread local data. This is
|
---|
116 | * actually an output argument.
|
---|
117 | * @param size Size of thread local data.
|
---|
118 | * @return Pointer to TCB structure.
|
---|
119 | */
|
---|
120 | tcb_t * tls_alloc_variant_2(void **data, size_t size)
|
---|
121 | {
|
---|
122 | tcb_t *tcb;
|
---|
123 |
|
---|
124 | size = ALIGN_UP(size, &_tls_alignment);
|
---|
125 | *data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size);
|
---|
126 | if (!*data)
|
---|
127 | return NULL;
|
---|
128 | tcb = (tcb_t *) (*data + size);
|
---|
129 | tcb->self = tcb;
|
---|
130 |
|
---|
131 | return tcb;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** Free TLS variant II data structures.
|
---|
135 | *
|
---|
136 | * @param tcb Pointer to TCB structure.
|
---|
137 | * @param size Size of thread local data.
|
---|
138 | */
|
---|
139 | void tls_free_variant_2(tcb_t *tcb, size_t size)
|
---|
140 | {
|
---|
141 | size = ALIGN_UP(size, &_tls_alignment);
|
---|
142 | void *start = ((void *) tcb) - size;
|
---|
143 | free(start);
|
---|
144 | }
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | /** @}
|
---|
148 | */
|
---|