bionic_tls.h revision b30aff405a220495941f1673b0a5e66c4fa8b84c
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 <sys/cdefs.h>
33#include <sys/limits.h>
34#include "__get_tls.h"
35
36__BEGIN_DECLS
37
38/** WARNING WARNING WARNING
39 **
40 ** This header file is *NOT* part of the public Bionic ABI/API
41 ** and should not be used/included by user-serviceable parts of
42 ** the system (e.g. applications).
43 **
44 ** It is only provided here for the benefit of the system dynamic
45 ** linker and the OpenGL sub-system (which needs to access the
46 ** pre-allocated slot directly for performance reason).
47 **/
48
49// Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted.
50enum {
51  TLS_SLOT_SELF = 0, // The kernel requires this specific slot for x86.
52  TLS_SLOT_THREAD_ID,
53  TLS_SLOT_ERRNO,
54
55  // These two aren't used by bionic itself, but allow the graphics code to
56  // access TLS directly rather than using the pthread API.
57  TLS_SLOT_OPENGL_API = 3,
58  TLS_SLOT_OPENGL = 4,
59
60  // This slot is only used to pass information from the dynamic linker to
61  // libc.so when the C library is loaded in to memory. The C runtime init
62  // function will then clear it. Since its use is extremely temporary,
63  // we reuse an existing location that isn't needed during libc startup.
64  TLS_SLOT_BIONIC_PREINIT = TLS_SLOT_OPENGL_API,
65
66  TLS_SLOT_STACK_GUARD = 5, // GCC requires this specific slot for x86.
67  TLS_SLOT_DLERROR,
68
69  TLS_SLOT_FIRST_USER_SLOT // Must come last!
70};
71
72/*
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 5
79
80#if defined(USE_JEMALLOC)
81/* jemalloc uses 5 keys for itself. */
82#define BIONIC_TLS_RESERVED_SLOTS (GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT + 5)
83#else
84#define BIONIC_TLS_RESERVED_SLOTS GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT
85#endif
86
87#define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
88
89/*
90 * Maximum number of elements in the TLS array.
91 * This includes space for pthread keys and our own internal slots.
92 * We need to round up to maintain stack alignment.
93 */
94#define BIONIC_TLS_SLOTS BIONIC_ALIGN(PTHREAD_KEYS_MAX + TLS_SLOT_FIRST_USER_SLOT + BIONIC_TLS_RESERVED_SLOTS, 4)
95
96__END_DECLS
97
98#if defined(__cplusplus)
99class KernelArgumentBlock;
100extern __LIBC_HIDDEN__ void __libc_init_tls(KernelArgumentBlock& args);
101#endif
102
103#endif /* __BIONIC_PRIVATE_BIONIC_TLS_H_ */
104