Home | History | Annotate | Download | only in private
      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 //  - TLS_SLOT_APP: Available for use by apps in Android Q and later. (This slot
     70 //    was used for errno in P and earlier.)
     71 
     72 #if defined(__arm__) || defined(__aarch64__)
     73 
     74 // The ARM ELF TLS ABI specifies[1] that the thread pointer points at a 2-word
     75 // TCB followed by the executable's TLS segment. Both the TCB and the
     76 // executable's segment are aligned according to the segment, so Bionic requires
     77 // a minimum segment alignment, which effectively reserves an 8-word TCB. The
     78 // ARM spec allocates the first TCB word to the DTV.
     79 //
     80 // [1] "Addenda to, and Errata in, the ABI for the ARM Architecture". Section 3.
     81 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0045e/IHI0045E_ABI_addenda.pdf
     82 
     83 #define MIN_TLS_SLOT              -1 // update this value when reserving a slot
     84 #define TLS_SLOT_BIONIC_TLS       -1
     85 #define TLS_SLOT_DTV              0
     86 #define TLS_SLOT_THREAD_ID        1
     87 #define TLS_SLOT_APP              2 // was historically used for errno
     88 #define TLS_SLOT_OPENGL           3
     89 #define TLS_SLOT_OPENGL_API       4
     90 #define TLS_SLOT_STACK_GUARD      5
     91 #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror
     92 #define TLS_SLOT_ART_THREAD_SELF  7
     93 
     94 // The maximum slot is fixed by the minimum TLS alignment in Bionic executables.
     95 #define MAX_TLS_SLOT              7
     96 
     97 #elif defined(__i386__) || defined(__x86_64__)
     98 
     99 // x86 uses variant 2 ELF TLS layout, which places the executable's TLS segment
    100 // immediately before the thread pointer. New slots are allocated at positive
    101 // offsets from the thread pointer.
    102 
    103 #define MIN_TLS_SLOT              0
    104 
    105 #define TLS_SLOT_SELF             0
    106 #define TLS_SLOT_THREAD_ID        1
    107 #define TLS_SLOT_APP              2 // was historically used for errno
    108 #define TLS_SLOT_OPENGL           3
    109 #define TLS_SLOT_OPENGL_API       4
    110 #define TLS_SLOT_STACK_GUARD      5
    111 #define TLS_SLOT_SANITIZER        6 // was historically used for dlerror
    112 #define TLS_SLOT_ART_THREAD_SELF  7
    113 #define TLS_SLOT_DTV              8
    114 #define TLS_SLOT_BIONIC_TLS       9
    115 #define MAX_TLS_SLOT              9 // update this value when reserving a slot
    116 
    117 #endif
    118 
    119 #define BIONIC_TLS_SLOTS (MAX_TLS_SLOT - MIN_TLS_SLOT + 1)
    120