The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 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 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 28 | |
| 29 | #ifndef __BIONIC_PRIVATE_BIONIC_TLS_H_ |
| 30 | #define __BIONIC_PRIVATE_BIONIC_TLS_H_ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | |
| 32 | #include <sys/cdefs.h> |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 33 | #include <sys/limits.h> |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 34 | #include "__get_tls.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
| 36 | __BEGIN_DECLS |
| 37 | |
| 38 | /** WARNING WARNING WARNING |
| 39 | ** |
| 40 | ** This header file is *NOT* part of the public Bionic ABI/API |
| 41 | ** and should not be used/included by user-serviceable parts of |
| 42 | ** the system (e.g. applications). |
| 43 | ** |
| 44 | ** It is only provided here for the benefit of the system dynamic |
| 45 | ** linker and the OpenGL sub-system (which needs to access the |
| 46 | ** pre-allocated slot directly for performance reason). |
| 47 | **/ |
| 48 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 49 | /* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */ |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 50 | enum { |
| 51 | TLS_SLOT_SELF = 0, /* The kernel requires this specific slot for x86. */ |
| 52 | TLS_SLOT_THREAD_ID, |
| 53 | TLS_SLOT_ERRNO, |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 54 | |
Elliott Hughes | 877ec6d | 2013-11-15 17:40:18 -0800 | [diff] [blame] | 55 | /* This slot in the child's TLS is used to synchronize the parent and child |
| 56 | * during thread initialization. The child finishes with this mutex before |
| 57 | * running any code that can set errno, so we can reuse the errno slot. */ |
Elliott Hughes | 70b24b1 | 2013-11-15 11:51:07 -0800 | [diff] [blame] | 58 | TLS_SLOT_START_MUTEX = TLS_SLOT_ERRNO, |
| 59 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 60 | /* These two aren't used by bionic itself, but allow the graphics code to |
| 61 | * access TLS directly rather than using the pthread API. */ |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 62 | TLS_SLOT_OPENGL_API = 3, |
| 63 | TLS_SLOT_OPENGL = 4, |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 64 | |
| 65 | /* This slot is only used to pass information from the dynamic linker to |
| 66 | * libc.so when the C library is loaded in to memory. The C runtime init |
| 67 | * function will then clear it. Since its use is extremely temporary, |
| 68 | * we reuse an existing location that isn't needed during libc startup. */ |
| 69 | TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API, |
| 70 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 71 | TLS_SLOT_STACK_GUARD = 5, /* GCC requires this specific slot for x86. */ |
| 72 | TLS_SLOT_DLERROR, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 74 | TLS_SLOT_FIRST_USER_SLOT /* Must come last! */ |
| 75 | }; |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 76 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 77 | /* |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 78 | * There are two kinds of slot used internally by bionic --- there are the well-known slots |
| 79 | * enumerated above, and then there are those that are allocated during startup by calls to |
| 80 | * pthread_key_create; grep for GLOBAL_INIT_THREAD_LOCAL_BUFFER to find those. We need to manually |
| 81 | * maintain that second number, but pthread_test will fail if we forget. |
David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 82 | */ |
Elliott Hughes | 2f68866 | 2014-04-18 13:34:26 -0700 | [diff] [blame] | 83 | #define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 5 |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 84 | |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame^] | 85 | #if defined(USE_JEMALLOC) |
| 86 | /* jemalloc uses 5 keys for itself. */ |
| 87 | #define BIONIC_TLS_RESERVED_SLOTS (GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT + 5) |
| 88 | #else |
| 89 | #define BIONIC_TLS_RESERVED_SLOTS GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT |
| 90 | #endif |
| 91 | |
Elliott Hughes | c03e1e7 | 2013-07-29 16:51:45 -0700 | [diff] [blame] | 92 | #define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1)) |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Maximum number of elements in the TLS array. |
| 96 | * This includes space for pthread keys and our own internal slots. |
| 97 | * We need to round up to maintain stack alignment. |
| 98 | */ |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame^] | 99 | #define BIONIC_TLS_SLOTS BIONIC_ALIGN(PTHREAD_KEYS_MAX + TLS_SLOT_FIRST_USER_SLOT + BIONIC_TLS_RESERVED_SLOTS, 4) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 101 | __END_DECLS |
| 102 | |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 103 | #if defined(__cplusplus) |
Bernhard Rosenkraenzer | edad1e1 | 2013-09-18 23:37:00 +0200 | [diff] [blame] | 104 | class KernelArgumentBlock; |
Elliott Hughes | ce53272 | 2013-03-15 16:31:09 -0700 | [diff] [blame] | 105 | extern __LIBC_HIDDEN__ void __libc_init_tls(KernelArgumentBlock& args); |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 106 | #endif |
| 107 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 108 | #endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */ |