| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 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 | */ | 
|  | 28 |  | 
|  | 29 | #pragma once | 
|  | 30 |  | 
|  | 31 | /** WARNING WARNING WARNING | 
|  | 32 | ** | 
|  | 33 | ** This header file is *NOT* part of the public Bionic ABI/API and should not | 
|  | 34 | ** be used/included by user-serviceable parts of the system (e.g. | 
|  | 35 | ** applications). | 
|  | 36 | ** | 
|  | 37 | ** It is only provided here for the benefit of Android components that need a | 
|  | 38 | ** pre-allocated slot for performance reasons (including ART, the OpenGL | 
|  | 39 | ** subsystem, and sanitizers). | 
|  | 40 | **/ | 
|  | 41 |  | 
|  | 42 | // Bionic TCB / TLS slots: | 
|  | 43 | // | 
|  | 44 | //  - TLS_SLOT_SELF: On x86-{32,64}, the kernel makes TLS memory available via | 
|  | 45 | //    the gs/fs segments. To get the address of a TLS variable, the first slot | 
|  | 46 | //    of TLS memory (accessed using %gs:0 / %fs:0) holds the address of the | 
|  | 47 | //    gs/fs segment. This slot is used by: | 
|  | 48 | //     - OpenGL and compiler-rt | 
|  | 49 | //     - Accesses of x86 ELF TLS variables | 
|  | 50 | // | 
|  | 51 | //  - TLS_SLOT_OPENGL and TLS_SLOT_OPENGL_API: These two aren't used by bionic | 
|  | 52 | //    itself, but allow the graphics code to access TLS directly rather than | 
|  | 53 | //    using the pthread API. | 
|  | 54 | // | 
|  | 55 | //  - TLS_SLOT_STACK_GUARD: Used for -fstack-protector by: | 
|  | 56 | //     - Clang targeting Android/arm64 | 
|  | 57 | //     - gcc targeting Linux/x86-{32,64} | 
|  | 58 | // | 
|  | 59 | //  - TLS_SLOT_SANITIZER: Lets sanitizers avoid using pthread_getspecific for | 
|  | 60 | //    finding the current thread state. | 
|  | 61 | // | 
|  | 62 | //  - TLS_SLOT_DTV: Pointer to ELF TLS dynamic thread vector. | 
|  | 63 | // | 
|  | 64 | //  - TLS_SLOT_ART_THREAD_SELF: Fast storage for Thread::Current() in ART. | 
|  | 65 | // | 
|  | 66 | //  - TLS_SLOT_BIONIC_TLS: Optimizes accesses to bionic_tls by one load versus | 
|  | 67 | //    finding it using __get_thread(). | 
| Ryan Prichard | a0834d8 | 2019-01-23 18:47:10 -0800 | [diff] [blame] | 68 | // | 
|  | 69 | //  - TLS_SLOT_APP: Available for use by apps in Android Q and later. (This slot | 
|  | 70 | //    was used for errno in P and earlier.) | 
| dimitry | a66a683 | 2023-11-10 00:20:26 +0100 | [diff] [blame] | 71 | // | 
|  | 72 | //  - TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE: Pointer to the guest state for native | 
|  | 73 | //    bridge implementations. It is (to be) used by debuggerd to access this | 
|  | 74 | //    state for guest aware crash reporting of the binary translated code. | 
|  | 75 | //    (Introduced in V) | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 76 |  | 
|  | 77 | #if defined(__arm__) || defined(__aarch64__) | 
|  | 78 |  | 
|  | 79 | // The ARM ELF TLS ABI specifies[1] that the thread pointer points at a 2-word | 
|  | 80 | // TCB followed by the executable's TLS segment. Both the TCB and the | 
|  | 81 | // executable's segment are aligned according to the segment, so Bionic requires | 
|  | 82 | // a minimum segment alignment, which effectively reserves an 8-word TCB. The | 
|  | 83 | // ARM spec allocates the first TCB word to the DTV. | 
|  | 84 | // | 
|  | 85 | // [1] "Addenda to, and Errata in, the ABI for the ARM Architecture". Section 3. | 
|  | 86 | // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045e/IHI0045E_ABI_addenda.pdf | 
|  | 87 |  | 
| dimitry | a66a683 | 2023-11-10 00:20:26 +0100 | [diff] [blame] | 88 | #define MIN_TLS_SLOT (-2)  // update this value when reserving a slot | 
|  | 89 | #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE (-2) | 
| Chih-Hung Hsieh | fa658eb | 2020-03-04 11:14:42 -0800 | [diff] [blame] | 90 | #define TLS_SLOT_BIONIC_TLS     (-1) | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 91 | #define TLS_SLOT_DTV              0 | 
|  | 92 | #define TLS_SLOT_THREAD_ID        1 | 
| Ryan Prichard | a0834d8 | 2019-01-23 18:47:10 -0800 | [diff] [blame] | 93 | #define TLS_SLOT_APP              2 // was historically used for errno | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 94 | #define TLS_SLOT_OPENGL           3 | 
|  | 95 | #define TLS_SLOT_OPENGL_API       4 | 
|  | 96 | #define TLS_SLOT_STACK_GUARD      5 | 
|  | 97 | #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror | 
|  | 98 | #define TLS_SLOT_ART_THREAD_SELF  7 | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 99 |  | 
|  | 100 | // The maximum slot is fixed by the minimum TLS alignment in Bionic executables. | 
| Ryan Prichard | 2e72417 | 2019-01-15 20:33:27 -0800 | [diff] [blame] | 101 | #define MAX_TLS_SLOT              7 | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 102 |  | 
|  | 103 | #elif defined(__i386__) || defined(__x86_64__) | 
|  | 104 |  | 
|  | 105 | // x86 uses variant 2 ELF TLS layout, which places the executable's TLS segment | 
|  | 106 | // immediately before the thread pointer. New slots are allocated at positive | 
|  | 107 | // offsets from the thread pointer. | 
|  | 108 |  | 
|  | 109 | #define MIN_TLS_SLOT              0 | 
|  | 110 |  | 
|  | 111 | #define TLS_SLOT_SELF             0 | 
|  | 112 | #define TLS_SLOT_THREAD_ID        1 | 
| Ryan Prichard | a0834d8 | 2019-01-23 18:47:10 -0800 | [diff] [blame] | 113 | #define TLS_SLOT_APP              2 // was historically used for errno | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 114 | #define TLS_SLOT_OPENGL           3 | 
|  | 115 | #define TLS_SLOT_OPENGL_API       4 | 
|  | 116 | #define TLS_SLOT_STACK_GUARD      5 | 
|  | 117 | #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror | 
|  | 118 | #define TLS_SLOT_ART_THREAD_SELF  7 | 
| Ryan Prichard | 2e72417 | 2019-01-15 20:33:27 -0800 | [diff] [blame] | 119 | #define TLS_SLOT_DTV              8 | 
|  | 120 | #define TLS_SLOT_BIONIC_TLS       9 | 
| dimitry | a66a683 | 2023-11-10 00:20:26 +0100 | [diff] [blame] | 121 | #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE 10 | 
|  | 122 | #define MAX_TLS_SLOT 10  // update this value when reserving a slot | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 123 |  | 
| Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 124 | #elif defined(__riscv) | 
|  | 125 |  | 
|  | 126 | // RISC-V ELF Specification[1] specifies that RISC-V uses Variant I as described | 
|  | 127 | // by the ELF TLS specification, with tp containing the address one past the end | 
|  | 128 | // of the TCB. | 
|  | 129 | // | 
|  | 130 | // [1]: RISC-V ELF Specification. Section: Thread Local Storage | 
|  | 131 | // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc#thread-local-storage | 
|  | 132 |  | 
| dimitry | a66a683 | 2023-11-10 00:20:26 +0100 | [diff] [blame] | 133 | #define MIN_TLS_SLOT (-10)  // update this value when reserving a slot | 
| Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 134 |  | 
| dimitry | a66a683 | 2023-11-10 00:20:26 +0100 | [diff] [blame] | 135 | #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE (-10) | 
| Elliott Hughes | 4346270 | 2022-10-10 19:21:44 +0000 | [diff] [blame] | 136 | #define TLS_SLOT_BIONIC_TLS      (-9) | 
|  | 137 | #define TLS_SLOT_DTV             (-8) | 
|  | 138 | #define TLS_SLOT_THREAD_ID       (-7) | 
|  | 139 | #define TLS_SLOT_APP             (-6) | 
|  | 140 | #define TLS_SLOT_OPENGL          (-5) | 
|  | 141 | #define TLS_SLOT_OPENGL_API      (-4) | 
|  | 142 | #define TLS_SLOT_STACK_GUARD     (-3) | 
|  | 143 | #define TLS_SLOT_SANITIZER       (-2) | 
|  | 144 | #define TLS_SLOT_ART_THREAD_SELF (-1) | 
|  | 145 | #define MAX_TLS_SLOT             (-1) | 
|  | 146 |  | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 147 | #endif | 
|  | 148 |  | 
|  | 149 | #define BIONIC_TLS_SLOTS (MAX_TLS_SLOT - MIN_TLS_SLOT + 1) |