| 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(). | 
|  | 68 |  | 
|  | 69 | #if defined(__arm__) || defined(__aarch64__) | 
|  | 70 |  | 
|  | 71 | // The ARM ELF TLS ABI specifies[1] that the thread pointer points at a 2-word | 
|  | 72 | // TCB followed by the executable's TLS segment. Both the TCB and the | 
|  | 73 | // executable's segment are aligned according to the segment, so Bionic requires | 
|  | 74 | // a minimum segment alignment, which effectively reserves an 8-word TCB. The | 
|  | 75 | // ARM spec allocates the first TCB word to the DTV. | 
|  | 76 | // | 
|  | 77 | // [1] "Addenda to, and Errata in, the ABI for the ARM Architecture". Section 3. | 
|  | 78 | // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045e/IHI0045E_ABI_addenda.pdf | 
|  | 79 |  | 
|  | 80 | #define MIN_TLS_SLOT              -1 // update this value when reserving a slot | 
|  | 81 | #define TLS_SLOT_BIONIC_TLS       -1 | 
|  | 82 | #define TLS_SLOT_DTV              0 | 
|  | 83 | #define TLS_SLOT_THREAD_ID        1 | 
|  | 84 | // Slot 2 is free (was historically used for TLS_SLOT_ERRNO) | 
|  | 85 | #define TLS_SLOT_OPENGL           3 | 
|  | 86 | #define TLS_SLOT_OPENGL_API       4 | 
|  | 87 | #define TLS_SLOT_STACK_GUARD      5 | 
|  | 88 | #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror | 
|  | 89 | #define TLS_SLOT_ART_THREAD_SELF  7 | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 90 |  | 
|  | 91 | // 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] | 92 | #define MAX_TLS_SLOT              7 | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 93 |  | 
|  | 94 | #elif defined(__i386__) || defined(__x86_64__) | 
|  | 95 |  | 
|  | 96 | // x86 uses variant 2 ELF TLS layout, which places the executable's TLS segment | 
|  | 97 | // immediately before the thread pointer. New slots are allocated at positive | 
|  | 98 | // offsets from the thread pointer. | 
|  | 99 |  | 
|  | 100 | #define MIN_TLS_SLOT              0 | 
|  | 101 |  | 
|  | 102 | #define TLS_SLOT_SELF             0 | 
|  | 103 | #define TLS_SLOT_THREAD_ID        1 | 
|  | 104 | // Slot 2 is free (was historically used for TLS_SLOT_ERRNO) | 
|  | 105 | #define TLS_SLOT_OPENGL           3 | 
|  | 106 | #define TLS_SLOT_OPENGL_API       4 | 
|  | 107 | #define TLS_SLOT_STACK_GUARD      5 | 
|  | 108 | #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror | 
|  | 109 | #define TLS_SLOT_ART_THREAD_SELF  7 | 
| Ryan Prichard | 2e72417 | 2019-01-15 20:33:27 -0800 | [diff] [blame] | 110 | #define TLS_SLOT_DTV              8 | 
|  | 111 | #define TLS_SLOT_BIONIC_TLS       9 | 
|  | 112 | #define MAX_TLS_SLOT              9 // update this value when reserving a slot | 
| Ryan Prichard | 45d1349 | 2019-01-03 02:51:30 -0800 | [diff] [blame] | 113 |  | 
|  | 114 | #endif | 
|  | 115 |  | 
|  | 116 | #define BIONIC_TLS_SLOTS (MAX_TLS_SLOT - MIN_TLS_SLOT + 1) |