blob: edf878fb755a2ea0005f30cb34ea22330b731689 [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/* Maximum number of elements in the TLS array. */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047#define BIONIC_TLS_SLOTS 64
48
Elliott Hughesad88a082012-10-24 18:37:21 -070049/* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */
Elliott Hughes44b53ad2013-02-11 20:18:47 +000050enum {
51 TLS_SLOT_SELF = 0, /* The kernel requires this specific slot for x86. */
52 TLS_SLOT_THREAD_ID,
53 TLS_SLOT_ERRNO,
54 TLS_SLOT_OPENGL_API = 3,
55 TLS_SLOT_OPENGL = 4,
56 TLS_SLOT_STACK_GUARD = 5, /* GCC requires this specific slot for x86. */
57 TLS_SLOT_DLERROR,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058
Elliott Hughes44b53ad2013-02-11 20:18:47 +000059 TLS_SLOT_FIRST_USER_SLOT /* Must come last! */
60};
Elliott Hughes5419b942012-10-16 15:54:46 -070061
62/* This slot is only used to pass information from the dynamic linker to
David 'Digit' Turneref0bd182009-07-17 17:55:01 +020063 * libc.so when the C library is loaded in to memory. The C runtime init
64 * function will then clear it. Since its use is extremely temporary,
Elliott Hughes44b53ad2013-02-11 20:18:47 +000065 * we reuse an existing location that isn't needed during libc startup.
David 'Digit' Turneref0bd182009-07-17 17:55:01 +020066 */
Elliott Hughes5419b942012-10-16 15:54:46 -070067#define TLS_SLOT_BIONIC_PREINIT TLS_SLOT_OPENGL_API
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069/* set the Thread Local Storage, must contain at least BIONIC_TLS_SLOTS pointers */
70extern void __init_tls(void** tls, void* thread_info);
71
72/* syscall only, do not call directly */
73extern int __set_tls(void *ptr);
74
75/* get the TLS */
76#ifdef __arm__
David 'Digit' Turner6a51def2010-08-27 08:19:19 -070077/* The standard way to get the TLS is to call a kernel helper
78 * function (i.e. a function provided at a fixed address in a
79 * "magic page" mapped in all user-space address spaces ), which
80 * contains the most appropriate code path for the target device.
81 *
82 * However, for performance reasons, we're going to use our own
83 * machine code for the system's C shared library.
84 *
85 * We cannot use this optimization in the static version of the
86 * C library, because we don't know where the corresponding code
87 * is going to run.
88 */
89# ifdef LIBC_STATIC
90
91/* Use the kernel helper in static C library. */
92 typedef volatile void* (__kernel_get_tls_t)(void);
93# define __get_tls() (*(__kernel_get_tls_t *)0xffff0fe0)()
94
95# else /* !LIBC_STATIC */
96/* Use optimized code path.
David 'Digit' Turner4a05d122009-09-18 13:35:05 -070097 * Note that HAVE_ARM_TLS_REGISTER is build-specific
98 * (it must match your kernel configuration)
99 */
David 'Digit' Turner6a51def2010-08-27 08:19:19 -0700100# ifdef HAVE_ARM_TLS_REGISTER
101 /* We can read the address directly from a coprocessor
102 * register, which avoids touching the data cache
103 * completely.
104 */
105# define __get_tls() \
David 'Digit' Turner4a05d122009-09-18 13:35:05 -0700106 ({ register unsigned int __val asm("r0"); \
107 asm ("mrc p15, 0, r0, c13, c0, 3" : "=r"(__val) ); \
108 (volatile void*)__val; })
David 'Digit' Turner6a51def2010-08-27 08:19:19 -0700109# else /* !HAVE_ARM_TLS_REGISTER */
110 /* The kernel provides the address of the TLS at a fixed
111 * address of the magic page too.
112 */
113# define __get_tls() ( *((volatile void **) 0xffff0ff0) )
114# endif
115# endif /* !LIBC_STATIC */
Raghu Gandham1c303982012-08-02 17:47:37 -0700116#elif defined(__mips__)
117# define __get_tls() \
118 ({ register unsigned int __val asm("v1"); \
119 asm ( \
120 " .set push\n" \
121 " .set mips32r2\n" \
122 " rdhwr %0,$29\n" \
123 " .set pop\n" \
124 : "=r"(__val) \
125 ); \
126 (volatile void*)__val; })
127#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128extern void* __get_tls( void );
Raghu Gandham1c303982012-08-02 17:47:37 -0700129#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
131/* return the stack base and size, used by our malloc debugger */
132extern void* __get_stack_base(int *p_stack_size);
133
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134__END_DECLS
135
Elliott Hughesd3920b32013-02-07 18:39:34 -0800136#if defined(__cplusplus)
137struct KernelArgumentBlock;
138extern void __libc_init_tls(KernelArgumentBlock& args);
139#endif
140
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141#endif /* _SYS_TLS_H */