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  */
28 
29 #ifndef __BIONIC_PRIVATE_BIONIC_TLS_H_
30 #define __BIONIC_PRIVATE_BIONIC_TLS_H_
31 
32 #include <locale.h>
33 #include <mntent.h>
34 #include <stdio.h>
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 
38 #include "bionic_macros.h"
39 #include "__get_tls.h"
40 #include "grp_pwd.h"
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 
55 // Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted.
56 enum {
57   TLS_SLOT_SELF = 0, // The kernel requires this specific slot for x86.
58   TLS_SLOT_THREAD_ID,
59   TLS_SLOT_ERRNO,
60 
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.
63   TLS_SLOT_OPENGL_API = 3,
64   TLS_SLOT_OPENGL = 4,
65 
66   // This slot is only used to pass information from the dynamic linker to
67   // libc.so when the C library is loaded in to memory. The C runtime init
68   // function will then clear it. Since its use is extremely temporary,
69   // we reuse an existing location that isn't needed during libc startup.
70   TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API,
71 
72   TLS_SLOT_STACK_GUARD = 5, // GCC requires this specific slot for x86.
73   TLS_SLOT_DLERROR,
74 
75   // Fast storage for Thread::Current() in ART.
76   TLS_SLOT_ART_THREAD_SELF,
77 
78   // Lets TSAN avoid using pthread_getspecific for finding the current thread
79   // state.
80   TLS_SLOT_TSAN,
81 
82   BIONIC_TLS_SLOTS // Must come last!
83 };
84 
85 // ~3 pages.
86 struct bionic_tls {
87   locale_t locale;
88 
89   char basename_buf[MAXPATHLEN];
90   char dirname_buf[MAXPATHLEN];
91 
92   mntent mntent_buf;
93   char mntent_strings[BUFSIZ];
94 
95   char ptsname_buf[32];
96   char ttyname_buf[64];
97 
98   char strerror_buf[NL_TEXTMAX];
99   char strsignal_buf[NL_TEXTMAX];
100 
101   group_state_t group;
102   passwd_state_t passwd;
103 };
104 
105 #define BIONIC_TLS_SIZE (BIONIC_ALIGN(sizeof(bionic_tls), PAGE_SIZE))
106 
107 /*
108  * Bionic uses some pthread keys internally. All pthread keys used internally
109  * should be created in constructors, except for keys that may be used in or
110  * before constructors.
111  *
112  * We need to manually maintain the count of pthread keys used internally, but
113  * pthread_test should fail if we forget.
114  *
115  * These are the pthread keys currently used internally by libc:
116  *  _res_key               libc (constructor in BSD code)
117  */
118 
119 #define LIBC_PTHREAD_KEY_RESERVED_COUNT 1
120 
121 /* Internally, jemalloc uses a single key for per thread data. */
122 #define JEMALLOC_PTHREAD_KEY_RESERVED_COUNT 1
123 #define BIONIC_PTHREAD_KEY_RESERVED_COUNT (LIBC_PTHREAD_KEY_RESERVED_COUNT + JEMALLOC_PTHREAD_KEY_RESERVED_COUNT)
124 
125 /*
126  * Maximum number of pthread keys allocated.
127  * This includes pthread keys used internally and externally.
128  */
129 #define BIONIC_PTHREAD_KEY_COUNT (BIONIC_PTHREAD_KEY_RESERVED_COUNT + PTHREAD_KEYS_MAX)
130 
131 __END_DECLS
132 
133 #if defined(__cplusplus)
134 class KernelArgumentBlock;
135 extern void __libc_init_main_thread(KernelArgumentBlock&);
136 #endif
137 
138 #endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */
139