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 | |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 32 | #include <locale.h> |
| 33 | #include <mntent.h> |
| 34 | #include <stdio.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | #include <sys/cdefs.h> |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 36 | #include <sys/param.h> |
Elliott Hughes | fc69a8a | 2016-03-04 11:53:09 -0800 | [diff] [blame] | 37 | |
Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 38 | #include "bionic_macros.h" |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 39 | #include "__get_tls.h" |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 40 | #include "grp_pwd.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | |
| 42 | __BEGIN_DECLS |
| 43 | |
| 44 | /** WARNING WARNING WARNING |
| 45 | ** |
| 46 | ** This header file is *NOT* part of the public Bionic ABI/API |
| 47 | ** and should not be used/included by user-serviceable parts of |
| 48 | ** the system (e.g. applications). |
| 49 | ** |
| 50 | ** It is only provided here for the benefit of the system dynamic |
| 51 | ** linker and the OpenGL sub-system (which needs to access the |
| 52 | ** pre-allocated slot directly for performance reason). |
| 53 | **/ |
| 54 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 55 | // 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] | 56 | enum { |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 57 | TLS_SLOT_SELF = 0, // The kernel requires this specific slot for x86. |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 58 | TLS_SLOT_THREAD_ID, |
| 59 | TLS_SLOT_ERRNO, |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 60 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 61 | // These two aren't used by bionic itself, but allow the graphics code to |
| 62 | // access TLS directly rather than using the pthread API. |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 63 | TLS_SLOT_OPENGL_API = 3, |
| 64 | TLS_SLOT_OPENGL = 4, |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 65 | |
Elliott Hughes | b30aff4 | 2014-05-28 19:35:33 +0000 | [diff] [blame] | 66 | TLS_SLOT_STACK_GUARD = 5, // GCC requires this specific slot for x86. |
Elliott Hughes | 34583c1 | 2018-11-13 15:30:07 -0800 | [diff] [blame^] | 67 | |
| 68 | // TLS slot 6 was used for dlerror but is now free. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 69 | |
Andreas Gampe | 9bbb5a7 | 2015-07-28 19:53:54 -0700 | [diff] [blame] | 70 | // Fast storage for Thread::Current() in ART. |
Elliott Hughes | 34583c1 | 2018-11-13 15:30:07 -0800 | [diff] [blame^] | 71 | TLS_SLOT_ART_THREAD_SELF = 7, |
Andreas Gampe | 9bbb5a7 | 2015-07-28 19:53:54 -0700 | [diff] [blame] | 72 | |
Dan Albert | aa23b2e | 2015-08-18 15:09:00 -0700 | [diff] [blame] | 73 | // Lets TSAN avoid using pthread_getspecific for finding the current thread |
| 74 | // state. |
Elliott Hughes | 34583c1 | 2018-11-13 15:30:07 -0800 | [diff] [blame^] | 75 | TLS_SLOT_TSAN = 8, |
Dan Albert | aa23b2e | 2015-08-18 15:09:00 -0700 | [diff] [blame] | 76 | |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 77 | BIONIC_TLS_SLOTS // Must come last! |
Elliott Hughes | 44b53ad | 2013-02-11 20:18:47 +0000 | [diff] [blame] | 78 | }; |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 79 | |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 80 | // ~3 pages. |
| 81 | struct bionic_tls { |
| 82 | locale_t locale; |
| 83 | |
| 84 | char basename_buf[MAXPATHLEN]; |
| 85 | char dirname_buf[MAXPATHLEN]; |
| 86 | |
| 87 | mntent mntent_buf; |
| 88 | char mntent_strings[BUFSIZ]; |
| 89 | |
| 90 | char ptsname_buf[32]; |
| 91 | char ttyname_buf[64]; |
| 92 | |
| 93 | char strerror_buf[NL_TEXTMAX]; |
| 94 | char strsignal_buf[NL_TEXTMAX]; |
| 95 | |
| 96 | group_state_t group; |
| 97 | passwd_state_t passwd; |
| 98 | }; |
| 99 | |
Dan Albert | a613d0d | 2017-10-05 16:39:33 -0700 | [diff] [blame] | 100 | #define BIONIC_TLS_SIZE (__BIONIC_ALIGN(sizeof(bionic_tls), PAGE_SIZE)) |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 101 | |
Elliott Hughes | 3e89847 | 2013-02-12 16:40:24 +0000 | [diff] [blame] | 102 | /* |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 103 | * Bionic uses some pthread keys internally. All pthread keys used internally |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 104 | * should be created in constructors, except for keys that may be used in or |
| 105 | * before constructors. |
| 106 | * |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 107 | * We need to manually maintain the count of pthread keys used internally, but |
| 108 | * pthread_test should fail if we forget. |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 109 | * |
| 110 | * These are the pthread keys currently used internally by libc: |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 111 | * _res_key libc (constructor in BSD code) |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 112 | */ |
| 113 | |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 114 | #define LIBC_PTHREAD_KEY_RESERVED_COUNT 1 |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 115 | |
Christopher Ferris | c0f8928 | 2015-04-15 16:34:57 -0700 | [diff] [blame] | 116 | /* Internally, jemalloc uses a single key for per thread data. */ |
| 117 | #define JEMALLOC_PTHREAD_KEY_RESERVED_COUNT 1 |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 118 | #define BIONIC_PTHREAD_KEY_RESERVED_COUNT (LIBC_PTHREAD_KEY_RESERVED_COUNT + JEMALLOC_PTHREAD_KEY_RESERVED_COUNT) |
Christopher Ferris | 72bbd42 | 2014-05-08 11:14:03 -0700 | [diff] [blame] | 119 | |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 120 | /* |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 121 | * Maximum number of pthread keys allocated. |
| 122 | * This includes pthread keys used internally and externally. |
Elliott Hughes | 1887621 | 2013-12-12 11:02:41 -0800 | [diff] [blame] | 123 | */ |
Yabin Cui | 5e2bd71 | 2015-02-20 16:15:33 -0800 | [diff] [blame] | 124 | #define BIONIC_PTHREAD_KEY_COUNT (BIONIC_PTHREAD_KEY_RESERVED_COUNT + PTHREAD_KEYS_MAX) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 125 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 126 | __END_DECLS |
| 127 | |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 128 | #if defined(__cplusplus) |
Bernhard Rosenkraenzer | edad1e1 | 2013-09-18 23:37:00 +0200 | [diff] [blame] | 129 | class KernelArgumentBlock; |
Ryan Prichard | 9cfca86 | 2018-11-22 02:44:09 -0800 | [diff] [blame] | 130 | extern void __libc_init_main_thread_early(KernelArgumentBlock& args); |
Ryan Prichard | 07440a8 | 2018-11-22 03:16:06 -0800 | [diff] [blame] | 131 | extern void __libc_init_main_thread_late(); |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 132 | #endif |
| 133 | |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 134 | #endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */ |