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