blob: 9ea36474e4fce37d1e80a019afb90a8320431674 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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
Elliott Hughesad88a082012-10-24 18:37:21 -070046/* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */
Elliott Hughes44b53ad2013-02-11 20:18:47 +000047enum {
48 TLS_SLOT_SELF = 0, /* The kernel requires this specific slot for x86. */
49 TLS_SLOT_THREAD_ID,
50 TLS_SLOT_ERRNO,
Elliott Hughes3e898472013-02-12 16:40:24 +000051
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. */
Elliott Hughes44b53ad2013-02-11 20:18:47 +000054 TLS_SLOT_OPENGL_API = 3,
55 TLS_SLOT_OPENGL = 4,
Elliott Hughes3e898472013-02-12 16:40:24 +000056
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
Elliott Hughes44b53ad2013-02-11 20:18:47 +000063 TLS_SLOT_STACK_GUARD = 5, /* GCC requires this specific slot for x86. */
64 TLS_SLOT_DLERROR,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
Elliott Hughes44b53ad2013-02-11 20:18:47 +000066 TLS_SLOT_FIRST_USER_SLOT /* Must come last! */
67};
Elliott Hughes5419b942012-10-16 15:54:46 -070068
Elliott Hughes3e898472013-02-12 16:40:24 +000069/*
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.
David 'Digit' Turneref0bd182009-07-17 17:55:01 +020077 */
Elliott Hughes3e898472013-02-12 16:40:24 +000078#define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 4
Elliott Hughesc03e1e72013-07-29 16:51:45 -070079/*
80 * This is PTHREAD_KEYS_MAX + TLS_SLOT_FIRST_USER_SLOT + GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT
81 * rounded up to maintain stack alignment.
82 */
83#define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))
84#define BIONIC_TLS_SLOTS BIONIC_ALIGN(128 + TLS_SLOT_FIRST_USER_SLOT + GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT, 4)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086/* syscall only, do not call directly */
Elliott Hughes91a99252013-02-12 21:56:42 -080087extern int __set_tls(void* ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
89/* get the TLS */
Elliott Hughes91a99252013-02-12 21:56:42 -080090#if defined(__arm__)
91# define __get_tls() \
Mathias Agopianb6e34002013-06-14 19:00:03 -070092 ({ register unsigned int __val; \
93 asm ("mrc p15, 0, %0, c13, c0, 3" : "=r"(__val)); \
94 (volatile void*) __val; })
Raghu Gandham1c303982012-08-02 17:47:37 -070095#elif defined(__mips__)
Elliott Hughes91a99252013-02-12 21:56:42 -080096# define __get_tls() \
Elliott Hughesa33dc572013-06-18 13:26:22 -070097 /* On mips32r1, this goes via a kernel illegal instruction trap that's optimized for v1. */ \
98 ({ register unsigned int __val asm("v1"); \
Mathias Agopianb6e34002013-06-14 19:00:03 -070099 asm (" .set push\n" \
100 " .set mips32r2\n" \
101 " rdhwr %0,$29\n" \
102 " .set pop\n" : "=r"(__val)); \
103 (volatile void*) __val; })
Elliott Hughes91a99252013-02-12 21:56:42 -0800104#elif defined(__i386__)
105# define __get_tls() \
106 ({ register void* __val; \
107 asm ("movl %%gs:0, %0" : "=r"(__val)); \
108 (volatile void*) __val; })
Raghu Gandham1c303982012-08-02 17:47:37 -0700109#else
Elliott Hughes91a99252013-02-12 21:56:42 -0800110#error unsupported architecture
Raghu Gandham1c303982012-08-02 17:47:37 -0700111#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113__END_DECLS
114
Elliott Hughesd3920b32013-02-07 18:39:34 -0800115#if defined(__cplusplus)
Bernhard Rosenkraenzeredad1e12013-09-18 23:37:00 +0200116class KernelArgumentBlock;
Elliott Hughesce532722013-03-15 16:31:09 -0700117extern __LIBC_HIDDEN__ void __libc_init_tls(KernelArgumentBlock& args);
Elliott Hughesd3920b32013-02-07 18:39:34 -0800118#endif
119
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120#endif /* _SYS_TLS_H */