[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 |
|
---|
[4f205248] | 38 | #include <assert.h>
|
---|
[582a0b8] | 39 | #include <stddef.h>
|
---|
[118a872] | 40 | #include <align.h>
|
---|
[fa23560] | 41 | #include <tls.h>
|
---|
[38d150e] | 42 | #include <stdlib.h>
|
---|
[19f857a] | 43 | #include <str.h>
|
---|
[4f205248] | 44 | #include <macros.h>
|
---|
[2c4e1cc] | 45 | #include <elf/elf.h>
|
---|
[40abf56] | 46 | #include <as.h>
|
---|
[fa23560] | 47 |
|
---|
[40abf56] | 48 | #include <libarch/config.h>
|
---|
[2eadda9] | 49 |
|
---|
[6adb775f] | 50 | #ifdef CONFIG_RTLD
|
---|
| 51 | #include <rtld/rtld.h>
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
[40abf56] | 54 | #include "private/libc.h"
|
---|
| 55 |
|
---|
[4f205248] | 56 | #if !defined(CONFIG_TLS_VARIANT_1) && !defined(CONFIG_TLS_VARIANT_2)
|
---|
| 57 | #error Unknown TLS variant.
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
[40abf56] | 60 | static ptrdiff_t _tcb_data_offset(void)
|
---|
| 61 | {
|
---|
| 62 | const elf_segment_header_t *tls =
|
---|
| 63 | elf_get_phdr(__progsymbols.elfstart, PT_TLS);
|
---|
| 64 |
|
---|
| 65 | size_t tls_align = tls ? tls->p_align : 1;
|
---|
| 66 |
|
---|
| 67 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
| 68 | return ALIGN_UP((ptrdiff_t) sizeof(tcb_t), tls_align);
|
---|
| 69 | #else
|
---|
| 70 | size_t tls_size = tls ? tls->p_memsz : 0;
|
---|
| 71 | return -ALIGN_UP((ptrdiff_t) tls_size, max(tls_align, _Alignof(tcb_t)));
|
---|
| 72 | #endif
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[4f205248] | 75 | /** Get address of static TLS block */
|
---|
| 76 | void *tls_get(void)
|
---|
[6adb775f] | 77 | {
|
---|
[91e4567] | 78 | #ifdef CONFIG_RTLD
|
---|
[4f205248] | 79 | assert(runtime_env == NULL);
|
---|
[91e4567] | 80 | #endif
|
---|
[40abf56] | 81 | return (uint8_t *)__tcb_get() + _tcb_data_offset();
|
---|
| 82 | }
|
---|
[2c4e1cc] | 83 |
|
---|
[40abf56] | 84 | static tcb_t *tls_make_generic(const void *elf, void *(*alloc)(size_t, size_t))
|
---|
| 85 | {
|
---|
| 86 | assert(!elf_get_phdr(elf, PT_DYNAMIC));
|
---|
| 87 | #ifdef CONFIG_RTLD
|
---|
| 88 | assert(runtime_env == NULL);
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | const elf_segment_header_t *tls = elf_get_phdr(elf, PT_TLS);
|
---|
| 92 | size_t tls_size = tls ? tls->p_memsz : 0;
|
---|
| 93 | size_t tls_align = tls ? tls->p_align : 1;
|
---|
| 94 |
|
---|
| 95 | /*
|
---|
| 96 | * We don't currently support alignment this big,
|
---|
| 97 | * and neither should we need to.
|
---|
| 98 | */
|
---|
| 99 | assert(tls_align <= PAGE_SIZE);
|
---|
[6adb775f] | 100 |
|
---|
[40abf56] | 101 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
| 102 | size_t alloc_size =
|
---|
| 103 | ALIGN_UP(sizeof(tcb_t), tls_align) + tls_size;
|
---|
| 104 | #else
|
---|
| 105 | size_t alloc_size =
|
---|
| 106 | ALIGN_UP(tls_size, max(tls_align, _Alignof(tcb_t))) + sizeof(tcb_t);
|
---|
| 107 | #endif
|
---|
| 108 |
|
---|
| 109 | void *area = alloc(max(tls_align, _Alignof(tcb_t)), alloc_size);
|
---|
| 110 | if (!area)
|
---|
[4f205248] | 111 | return NULL;
|
---|
| 112 |
|
---|
[e2f26002] | 113 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
[40abf56] | 114 | tcb_t *tcb = area;
|
---|
| 115 | uint8_t *data = (uint8_t *)tcb + _tcb_data_offset();
|
---|
| 116 | memset(tcb, 0, sizeof(*tcb));
|
---|
| 117 | #else
|
---|
| 118 | uint8_t *data = area;
|
---|
| 119 | tcb_t *tcb = (tcb_t *) (data - _tcb_data_offset());
|
---|
| 120 | memset(tcb, 0, sizeof(tcb_t));
|
---|
| 121 | tcb->self = tcb;
|
---|
[e2f26002] | 122 | #endif
|
---|
[40abf56] | 123 |
|
---|
| 124 | if (!tls)
|
---|
| 125 | return tcb;
|
---|
| 126 |
|
---|
| 127 | uintptr_t bias = elf_get_bias(elf);
|
---|
| 128 |
|
---|
| 129 | /* Copy thread local data from the initialization image. */
|
---|
| 130 | memcpy(data, (void *)(tls->p_vaddr + bias), tls->p_filesz);
|
---|
| 131 | /* Zero out the thread local uninitialized data. */
|
---|
| 132 | memset(data + tls->p_filesz, 0, tls->p_memsz - tls->p_filesz);
|
---|
| 133 |
|
---|
| 134 | return tcb;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | static void *early_alloc(size_t align, size_t alloc_size)
|
---|
| 138 | {
|
---|
| 139 | assert(align <= PAGE_SIZE);
|
---|
| 140 | alloc_size = ALIGN_UP(alloc_size, PAGE_SIZE);
|
---|
| 141 |
|
---|
| 142 | void *area = as_area_create(AS_AREA_ANY, alloc_size,
|
---|
| 143 | AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
|
---|
| 144 | if (area == AS_MAP_FAILED)
|
---|
| 145 | return NULL;
|
---|
| 146 | return area;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /** Same as tls_make(), but uses as_area_create() instead of memalign().
|
---|
| 150 | * Only used in __libc_main() if the program was created by the kernel.
|
---|
| 151 | */
|
---|
| 152 | tcb_t *tls_make_initial(const void *elf)
|
---|
| 153 | {
|
---|
| 154 | return tls_make_generic(elf, early_alloc);
|
---|
[e2f26002] | 155 | }
|
---|
| 156 |
|
---|
[fa23560] | 157 | /** Create TLS (Thread Local Storage) data structures.
|
---|
| 158 | *
|
---|
| 159 | * @return Pointer to TCB.
|
---|
| 160 | */
|
---|
[40abf56] | 161 | tcb_t *tls_make(const void *elf)
|
---|
[fa23560] | 162 | {
|
---|
[40abf56] | 163 | // TODO: Always use rtld.
|
---|
[a35b458] | 164 |
|
---|
[6adb775f] | 165 | #ifdef CONFIG_RTLD
|
---|
| 166 | if (runtime_env != NULL)
|
---|
| 167 | return rtld_tls_make(runtime_env);
|
---|
| 168 | #endif
|
---|
[a35b458] | 169 |
|
---|
[40abf56] | 170 | return tls_make_generic(elf, memalign);
|
---|
[fa23560] | 171 | }
|
---|
| 172 |
|
---|
[31399f3] | 173 | void tls_free(tcb_t *tcb)
|
---|
[fa23560] | 174 | {
|
---|
[3a9414e] | 175 | #ifdef CONFIG_RTLD
|
---|
[d2bb25e7] | 176 | free(tcb->dtv);
|
---|
[4f205248] | 177 |
|
---|
| 178 | if (runtime_env != NULL) {
|
---|
| 179 | tls_free_arch(tcb, runtime_env->tls_size, runtime_env->tls_align);
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
[3a9414e] | 182 | #endif
|
---|
[4f205248] | 183 | const elf_segment_header_t *tls =
|
---|
[2eadda9] | 184 | elf_get_phdr(__progsymbols.elfstart, PT_TLS);
|
---|
[4f205248] | 185 |
|
---|
| 186 | assert(tls != NULL);
|
---|
| 187 | tls_free_arch(tcb,
|
---|
| 188 | ALIGN_UP(tls->p_memsz, tls->p_align) + sizeof(tcb_t),
|
---|
| 189 | max(tls->p_align, _Alignof(tcb_t)));
|
---|
[fa23560] | 190 | }
|
---|
| 191 |
|
---|
| 192 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
| 193 | /** Allocate TLS variant 1 data structures.
|
---|
| 194 | *
|
---|
| 195 | * @param data Start of TLS section. This is an output argument.
|
---|
| 196 | * @param size Size of tdata + tbss section.
|
---|
| 197 | * @return Pointer to tcb_t structure.
|
---|
| 198 | */
|
---|
[4f205248] | 199 | tcb_t *tls_alloc_variant_1(size_t size, size_t align)
|
---|
[fa23560] | 200 | {
|
---|
[4f205248] | 201 | tcb_t *tcb = memalign(align, size);
|
---|
[d2bb25e7] | 202 | if (!tcb)
|
---|
[0d57c3e] | 203 | return NULL;
|
---|
[4f205248] | 204 | memset(tcb, 0, sizeof(tcb_t));
|
---|
[d2bb25e7] | 205 | return tcb;
|
---|
[fa23560] | 206 | }
|
---|
| 207 |
|
---|
| 208 | /** Free TLS variant I data structures.
|
---|
| 209 | *
|
---|
| 210 | * @param tcb Pointer to TCB structure.
|
---|
| 211 | * @param size This argument is ignored.
|
---|
| 212 | */
|
---|
[4f205248] | 213 | void tls_free_variant_1(tcb_t *tcb, size_t size, size_t align)
|
---|
[fa23560] | 214 | {
|
---|
| 215 | free(tcb);
|
---|
| 216 | }
|
---|
| 217 | #endif
|
---|
| 218 |
|
---|
| 219 | #ifdef CONFIG_TLS_VARIANT_2
|
---|
| 220 | /** Allocate TLS variant II data structures.
|
---|
| 221 | *
|
---|
| 222 | * @param data Pointer to pointer to thread local data. This is
|
---|
| 223 | * actually an output argument.
|
---|
| 224 | * @param size Size of thread local data.
|
---|
[4f205248] | 225 | * @param align Alignment of thread local data.
|
---|
[fa23560] | 226 | * @return Pointer to TCB structure.
|
---|
| 227 | */
|
---|
[4f205248] | 228 | tcb_t *tls_alloc_variant_2(size_t size, size_t align)
|
---|
[fa23560] | 229 | {
|
---|
[4f205248] | 230 | void *data = memalign(align, size);
|
---|
| 231 | if (data == NULL)
|
---|
[0d57c3e] | 232 | return NULL;
|
---|
[fa23560] | 233 |
|
---|
[4f205248] | 234 | tcb_t *tcb = (tcb_t *) (data + size - sizeof(tcb_t));
|
---|
| 235 | memset(tcb, 0, sizeof(tcb_t));
|
---|
| 236 | tcb->self = tcb;
|
---|
[fa23560] | 237 | return tcb;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /** Free TLS variant II data structures.
|
---|
| 241 | *
|
---|
| 242 | * @param tcb Pointer to TCB structure.
|
---|
| 243 | * @param size Size of thread local data.
|
---|
[4f205248] | 244 | * @param align Alignment of thread local data.
|
---|
[fa23560] | 245 | */
|
---|
[4f205248] | 246 | void tls_free_variant_2(tcb_t *tcb, size_t size, size_t align)
|
---|
[fa23560] | 247 | {
|
---|
[4f205248] | 248 | if (tcb != NULL) {
|
---|
| 249 | void *start = ((void *) tcb) + sizeof(tcb_t) - size;
|
---|
| 250 | free(start);
|
---|
| 251 | }
|
---|
[fa23560] | 252 | }
|
---|
| 253 | #endif
|
---|
| 254 |
|
---|
| 255 | /** @}
|
---|
| 256 | */
|
---|