The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 46 | /* Maximum number of elements in the TLS array. */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 47 | #define BIONIC_TLS_SLOTS 64 |
| 48 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 49 | /* Well-known TLS slots. What data goes in which slot is arbitrary unless otherwise noted. */ |
| 50 | #define TLS_SLOT_SELF 0 /* The kernel requires this specific slot for x86. */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | #define TLS_SLOT_THREAD_ID 1 |
| 52 | #define TLS_SLOT_ERRNO 2 |
| 53 | |
| 54 | #define TLS_SLOT_OPENGL_API 3 |
| 55 | #define TLS_SLOT_OPENGL 4 |
| 56 | |
Elliott Hughes | ad88a08 | 2012-10-24 18:37:21 -0700 | [diff] [blame] | 57 | #define TLS_SLOT_STACK_GUARD 5 /* GCC requires this specific slot for x86. */ |
| 58 | #define TLS_SLOT_DLERROR 6 |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 59 | |
| 60 | #define TLS_SLOT_MAX_WELL_KNOWN TLS_SLOT_DLERROR |
| 61 | |
| 62 | /* This slot is only used to pass information from the dynamic linker to |
David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 63 | * 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, |
| 65 | * we reuse an existing location. |
| 66 | */ |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 67 | #define TLS_SLOT_BIONIC_PREINIT TLS_SLOT_OPENGL_API |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 | |
| 69 | #define TLS_DEFAULT_ALLOC_MAP 0x0000001F |
| 70 | |
| 71 | /* set the Thread Local Storage, must contain at least BIONIC_TLS_SLOTS pointers */ |
| 72 | extern void __init_tls(void** tls, void* thread_info); |
| 73 | |
| 74 | /* syscall only, do not call directly */ |
| 75 | extern int __set_tls(void *ptr); |
| 76 | |
| 77 | /* get the TLS */ |
| 78 | #ifdef __arm__ |
David 'Digit' Turner | 6a51def | 2010-08-27 08:19:19 -0700 | [diff] [blame] | 79 | /* The standard way to get the TLS is to call a kernel helper |
| 80 | * function (i.e. a function provided at a fixed address in a |
| 81 | * "magic page" mapped in all user-space address spaces ), which |
| 82 | * contains the most appropriate code path for the target device. |
| 83 | * |
| 84 | * However, for performance reasons, we're going to use our own |
| 85 | * machine code for the system's C shared library. |
| 86 | * |
| 87 | * We cannot use this optimization in the static version of the |
| 88 | * C library, because we don't know where the corresponding code |
| 89 | * is going to run. |
| 90 | */ |
| 91 | # ifdef LIBC_STATIC |
| 92 | |
| 93 | /* Use the kernel helper in static C library. */ |
| 94 | typedef volatile void* (__kernel_get_tls_t)(void); |
| 95 | # define __get_tls() (*(__kernel_get_tls_t *)0xffff0fe0)() |
| 96 | |
| 97 | # else /* !LIBC_STATIC */ |
| 98 | /* Use optimized code path. |
David 'Digit' Turner | 4a05d12 | 2009-09-18 13:35:05 -0700 | [diff] [blame] | 99 | * Note that HAVE_ARM_TLS_REGISTER is build-specific |
| 100 | * (it must match your kernel configuration) |
| 101 | */ |
David 'Digit' Turner | 6a51def | 2010-08-27 08:19:19 -0700 | [diff] [blame] | 102 | # ifdef HAVE_ARM_TLS_REGISTER |
| 103 | /* We can read the address directly from a coprocessor |
| 104 | * register, which avoids touching the data cache |
| 105 | * completely. |
| 106 | */ |
| 107 | # define __get_tls() \ |
David 'Digit' Turner | 4a05d12 | 2009-09-18 13:35:05 -0700 | [diff] [blame] | 108 | ({ register unsigned int __val asm("r0"); \ |
| 109 | asm ("mrc p15, 0, r0, c13, c0, 3" : "=r"(__val) ); \ |
| 110 | (volatile void*)__val; }) |
David 'Digit' Turner | 6a51def | 2010-08-27 08:19:19 -0700 | [diff] [blame] | 111 | # else /* !HAVE_ARM_TLS_REGISTER */ |
| 112 | /* The kernel provides the address of the TLS at a fixed |
| 113 | * address of the magic page too. |
| 114 | */ |
| 115 | # define __get_tls() ( *((volatile void **) 0xffff0ff0) ) |
| 116 | # endif |
| 117 | # endif /* !LIBC_STATIC */ |
Raghu Gandham | 1c30398 | 2012-08-02 17:47:37 -0700 | [diff] [blame] | 118 | #elif defined(__mips__) |
| 119 | # define __get_tls() \ |
| 120 | ({ register unsigned int __val asm("v1"); \ |
| 121 | asm ( \ |
| 122 | " .set push\n" \ |
| 123 | " .set mips32r2\n" \ |
| 124 | " rdhwr %0,$29\n" \ |
| 125 | " .set pop\n" \ |
| 126 | : "=r"(__val) \ |
| 127 | ); \ |
| 128 | (volatile void*)__val; }) |
| 129 | #else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | extern void* __get_tls( void ); |
Raghu Gandham | 1c30398 | 2012-08-02 17:47:37 -0700 | [diff] [blame] | 131 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | |
| 133 | /* return the stack base and size, used by our malloc debugger */ |
| 134 | extern void* __get_stack_base(int *p_stack_size); |
| 135 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 136 | /* Initialize the TLS. */ |
Nick Kralevich | 36bd371 | 2013-01-16 13:13:22 -0800 | [diff] [blame] | 137 | extern void __libc_init_tls(unsigned** elfdata); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 138 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 139 | __END_DECLS |
| 140 | |
| 141 | #endif /* _SYS_TLS_H */ |