bionic_tls.h revision 3e898476c7230b60a0f76968e64ff25f475b48c0
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#ifndef _SYS_TLS_H
29#define _SYS_TLS_H
30
31#include <sys/cdefs.h>
32
33__BEGIN_DECLS
34
35/** WARNING WARNING WARNING
36 **
37 ** This header file is *NOT* part of the public Bionic ABI/API
38 ** and should not be used/included by user-serviceable parts of
39 ** the system (e.g. applications).
40 **
41 ** It is only provided here for the benefit of the system dynamic
42 ** linker and the OpenGL sub-system (which needs to access the
43 ** pre-allocated slot directly for performance reason).
44 **/
45
46/* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */
47enum {
48  TLS_SLOT_SELF = 0, /* The kernel requires this specific slot for x86. */
49  TLS_SLOT_THREAD_ID,
50  TLS_SLOT_ERRNO,
51
52  /* These two aren't used by bionic itself, but allow the graphics code to
53   * access TLS directly rather than using the pthread API. */
54  TLS_SLOT_OPENGL_API = 3,
55  TLS_SLOT_OPENGL = 4,
56
57  /* This slot is only used to pass information from the dynamic linker to
58   * libc.so when the C library is loaded in to memory. The C runtime init
59   * function will then clear it. Since its use is extremely temporary,
60   * we reuse an existing location that isn't needed during libc startup. */
61  TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API,
62
63  TLS_SLOT_STACK_GUARD = 5, /* GCC requires this specific slot for x86. */
64  TLS_SLOT_DLERROR,
65
66  TLS_SLOT_FIRST_USER_SLOT /* Must come last! */
67};
68
69/*
70 * Maximum number of elements in the TLS array.
71 * POSIX says this must be at least 128, but Android has traditionally had only 64, minus those
72 * ones used internally by bionic itself.
73 * There are two kinds of slot used internally by bionic --- there are the well-known slots
74 * enumerated above, and then there are those that are allocated during startup by calls to
75 * pthread_key_create; grep for GLOBAL_INIT_THREAD_LOCAL_BUFFER to find those. We need to manually
76 * maintain that second number, but pthread_test will fail if we forget.
77 */
78#define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 4
79#define BIONIC_TLS_SLOTS 64
80
81/* set the Thread Local Storage, must contain at least BIONIC_TLS_SLOTS pointers */
82extern void __init_tls(void**  tls, void*  thread_info);
83
84/* syscall only, do not call directly */
85extern int __set_tls(void *ptr);
86
87/* get the TLS */
88#ifdef __arm__
89/* The standard way to get the TLS is to call a kernel helper
90 * function (i.e. a function provided at a fixed address in a
91 * "magic page" mapped in all user-space address spaces ), which
92 * contains the most appropriate code path for the target device.
93 *
94 * However, for performance reasons, we're going to use our own
95 * machine code for the system's C shared library.
96 *
97 * We cannot use this optimization in the static version of the
98 * C library, because we don't know where the corresponding code
99 * is going to run.
100 */
101#  ifdef LIBC_STATIC
102
103/* Use the kernel helper in static C library. */
104  typedef volatile void* (__kernel_get_tls_t)(void);
105#    define __get_tls() (*(__kernel_get_tls_t *)0xffff0fe0)()
106
107#  else /* !LIBC_STATIC */
108/* Use optimized code path.
109 * Note that HAVE_ARM_TLS_REGISTER is build-specific
110 * (it must match your kernel configuration)
111 */
112#    ifdef HAVE_ARM_TLS_REGISTER
113 /* We can read the address directly from a coprocessor
114  * register, which avoids touching the data cache
115  * completely.
116  */
117#      define __get_tls() \
118    ({ register unsigned int __val asm("r0"); \
119       asm ("mrc p15, 0, r0, c13, c0, 3" : "=r"(__val) ); \
120       (volatile void*)__val; })
121#    else /* !HAVE_ARM_TLS_REGISTER */
122 /* The kernel provides the address of the TLS at a fixed
123  * address of the magic page too.
124  */
125#      define __get_tls() ( *((volatile void **) 0xffff0ff0) )
126#    endif
127#  endif /* !LIBC_STATIC */
128#elif defined(__mips__)
129#    define __get_tls() \
130    ({ register unsigned int __val asm("v1");   \
131        asm (                                   \
132            "   .set    push\n"                 \
133            "   .set    mips32r2\n"             \
134            "   rdhwr   %0,$29\n"               \
135            "   .set    pop\n"                  \
136            : "=r"(__val)                       \
137            );                                  \
138        (volatile void*)__val; })
139#else
140extern void*  __get_tls( void );
141#endif
142
143/* return the stack base and size, used by our malloc debugger */
144extern void*  __get_stack_base(int  *p_stack_size);
145
146__END_DECLS
147
148#if defined(__cplusplus)
149struct KernelArgumentBlock;
150extern void __libc_init_tls(KernelArgumentBlock& args);
151#endif
152
153#endif /* _SYS_TLS_H */
154