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