Rev 3386 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3386 | Rev 4153 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Jakub Jermar |
2 | * Copyright (c) 2006 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | * |
33 | * |
34 | * Support for thread-local storage, as described in: |
34 | * Support for thread-local storage, as described in: |
35 | * Drepper U.: ELF Handling For Thread-Local Storage, 2005 |
35 | * Drepper U.: ELF Handling For Thread-Local Storage, 2005 |
36 | * |
36 | * |
37 | * Only static model is supported. |
37 | * Only static model is supported. |
38 | */ |
38 | */ |
39 | 39 | ||
40 | #include <tls.h> |
40 | #include <tls.h> |
41 | #include <malloc.h> |
41 | #include <malloc.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | #include <align.h> |
43 | #include <align.h> |
44 | 44 | ||
45 | /** Create TLS (Thread Local Storage) data structures. |
45 | /** Create TLS (Thread Local Storage) data structures. |
46 | * |
46 | * |
47 | * The code requires, that sections .tdata and .tbss are adjacent. It may be |
47 | * The code requires, that sections .tdata and .tbss are adjacent. It may be |
48 | * changed in the future. |
48 | * changed in the future. |
49 | * |
49 | * |
50 | * @return Pointer to TCB. |
50 | * @return Pointer to TCB. |
51 | */ |
51 | */ |
52 | tcb_t *__make_tls(void) |
52 | tcb_t *__make_tls(void) |
53 | { |
53 | { |
54 | void *data; |
54 | void *data; |
55 | tcb_t *tcb; |
55 | tcb_t *tcb; |
56 | size_t tls_size = &_tbss_end - &_tdata_start; |
56 | size_t tls_size = &_tbss_end - &_tdata_start; |
57 | 57 | ||
58 | tcb = __alloc_tls(&data, tls_size); |
58 | tcb = __alloc_tls(&data, tls_size); |
59 | 59 | ||
60 | /* |
60 | /* |
61 | * Copy thread local data from the initialization image. |
61 | * Copy thread local data from the initialization image. |
62 | */ |
62 | */ |
63 | memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start); |
63 | memcpy(data, &_tdata_start, &_tdata_end - &_tdata_start); |
64 | /* |
64 | /* |
65 | * Zero out the thread local uninitialized data. |
65 | * Zero out the thread local uninitialized data. |
66 | */ |
66 | */ |
67 | memset(data + (&_tbss_start - &_tdata_start), 0, |
67 | memset(data + (&_tbss_start - &_tdata_start), 0, |
68 | &_tbss_end - &_tbss_start); |
68 | &_tbss_end - &_tbss_start); |
69 | 69 | ||
70 | return tcb; |
70 | return tcb; |
71 | } |
71 | } |
72 | 72 | ||
73 | void __free_tls(tcb_t *tcb) |
73 | void __free_tls(tcb_t *tcb) |
74 | { |
74 | { |
75 | size_t tls_size = &_tbss_end - &_tdata_start; |
75 | size_t tls_size = &_tbss_end - &_tdata_start; |
76 | __free_tls_arch(tcb, tls_size); |
76 | __free_tls_arch(tcb, tls_size); |
77 | } |
77 | } |
78 | 78 | ||
79 | #ifdef CONFIG_TLS_VARIANT_1 |
79 | #ifdef CONFIG_TLS_VARIANT_1 |
80 | /** Allocate TLS variant 1 data structures. |
80 | /** Allocate TLS variant 1 data structures. |
81 | * |
81 | * |
82 | * @param data Start of TLS section. This is an output argument. |
82 | * @param data Start of TLS section. This is an output argument. |
83 | * @param size Size of tdata + tbss section. |
83 | * @param size Size of tdata + tbss section. |
84 | * @return Pointer to tcb_t structure. |
84 | * @return Pointer to tcb_t structure. |
85 | */ |
85 | */ |
86 | tcb_t *tls_alloc_variant_1(void **data, size_t size) |
86 | tcb_t *tls_alloc_variant_1(void **data, size_t size) |
87 | { |
87 | { |
88 | tcb_t *result; |
88 | tcb_t *result; |
89 | 89 | ||
90 | result = malloc(sizeof(tcb_t) + size); |
90 | result = malloc(sizeof(tcb_t) + size); |
91 | *data = ((void *)result) + sizeof(tcb_t); |
91 | *data = ((void *)result) + sizeof(tcb_t); |
92 | return result; |
92 | return result; |
93 | } |
93 | } |
94 | 94 | ||
95 | /** Free TLS variant I data structures. |
95 | /** Free TLS variant I data structures. |
96 | * |
96 | * |
97 | * @param tcb Pointer to TCB structure. |
97 | * @param tcb Pointer to TCB structure. |
98 | * @param size This argument is ignored. |
98 | * @param size This argument is ignored. |
99 | */ |
99 | */ |
100 | void tls_free_variant_1(tcb_t *tcb, size_t size) |
100 | void tls_free_variant_1(tcb_t *tcb, size_t size) |
101 | { |
101 | { |
102 | free(tcb); |
102 | free(tcb); |
103 | } |
103 | } |
104 | #endif |
104 | #endif |
105 | 105 | ||
106 | #ifdef CONFIG_TLS_VARIANT_2 |
106 | #ifdef CONFIG_TLS_VARIANT_2 |
107 | /** Allocate TLS variant II data structures. |
107 | /** Allocate TLS variant II data structures. |
108 | * |
108 | * |
109 | * @param data Pointer to pointer to thread local data. This is |
109 | * @param data Pointer to pointer to thread local data. This is |
110 | * actually an output argument. |
110 | * actually an output argument. |
111 | * @param size Size of thread local data. |
111 | * @param size Size of thread local data. |
112 | * @return Pointer to TCB structure. |
112 | * @return Pointer to TCB structure. |
113 | */ |
113 | */ |
114 | tcb_t * tls_alloc_variant_2(void **data, size_t size) |
114 | tcb_t * tls_alloc_variant_2(void **data, size_t size) |
115 | { |
115 | { |
116 | tcb_t *tcb; |
116 | tcb_t *tcb; |
117 | 117 | ||
118 | size = ALIGN_UP(size, &_tls_alignment); |
118 | size = ALIGN_UP(size, &_tls_alignment); |
119 | *data = memalign(&_tls_alignment, sizeof(tcb_t) + size); |
119 | *data = memalign((uintptr_t) &_tls_alignment, sizeof(tcb_t) + size); |
120 | 120 | ||
121 | tcb = (tcb_t *) (*data + size); |
121 | tcb = (tcb_t *) (*data + size); |
122 | tcb->self = tcb; |
122 | tcb->self = tcb; |
123 | 123 | ||
124 | return tcb; |
124 | return tcb; |
125 | } |
125 | } |
126 | 126 | ||
127 | /** Free TLS variant II data structures. |
127 | /** Free TLS variant II data structures. |
128 | * |
128 | * |
129 | * @param tcb Pointer to TCB structure. |
129 | * @param tcb Pointer to TCB structure. |
130 | * @param size Size of thread local data. |
130 | * @param size Size of thread local data. |
131 | */ |
131 | */ |
132 | void tls_free_variant_2(tcb_t *tcb, size_t size) |
132 | void tls_free_variant_2(tcb_t *tcb, size_t size) |
133 | { |
133 | { |
134 | size = ALIGN_UP(size, &_tls_alignment); |
134 | size = ALIGN_UP(size, &_tls_alignment); |
135 | void *start = ((void *) tcb) - size; |
135 | void *start = ((void *) tcb) - size; |
136 | free(start); |
136 | free(start); |
137 | } |
137 | } |
138 | #endif |
138 | #endif |
139 | 139 | ||
140 | /** @} |
140 | /** @} |
141 | */ |
141 | */ |
142 | 142 |