• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //  - 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)
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 
88 #define MIN_TLS_SLOT (-2)  // update this value when reserving a slot
89 #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE (-2)
90 #define TLS_SLOT_BIONIC_TLS     (-1)
91 #define TLS_SLOT_DTV              0
92 #define TLS_SLOT_THREAD_ID        1
93 #define TLS_SLOT_APP              2 // was historically used for errno
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
99 
100 // The maximum slot is fixed by the minimum TLS alignment in Bionic executables.
101 #define MAX_TLS_SLOT              7
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
113 #define TLS_SLOT_APP              2 // was historically used for errno
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
119 #define TLS_SLOT_DTV              8
120 #define TLS_SLOT_BIONIC_TLS       9
121 #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE 10
122 #define MAX_TLS_SLOT 10  // update this value when reserving a slot
123 
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 
133 #define MIN_TLS_SLOT (-10)  // update this value when reserving a slot
134 
135 #define TLS_SLOT_NATIVE_BRIDGE_GUEST_STATE (-10)
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 
147 #endif
148 
149 #define BIONIC_TLS_SLOTS (MAX_TLS_SLOT - MIN_TLS_SLOT + 1)
150