xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Definitions for thread-local data handling. linuxthreads/ARM version. |
| 2 | Copyright (C) 2004 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, write to the Free |
| 17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 18 | 02111-1307 USA. */ |
| 19 | |
| 20 | #ifndef _TLS_H |
| 21 | #define _TLS_H |
| 22 | |
| 23 | #ifndef __ASSEMBLER__ |
| 24 | |
| 25 | # include <stdbool.h> |
| 26 | # include <pt-machine.h> |
| 27 | # include <stddef.h> |
| 28 | |
| 29 | /* Type for the dtv. */ |
| 30 | typedef union dtv |
| 31 | { |
| 32 | size_t counter; |
| 33 | struct |
| 34 | { |
| 35 | void *val; |
| 36 | bool is_static; |
| 37 | } pointer; |
| 38 | } dtv_t; |
| 39 | |
| 40 | typedef struct |
| 41 | { |
| 42 | dtv_t *dtv; |
| 43 | |
| 44 | /* Reserved for the thread implementation. Unused in LinuxThreads. */ |
| 45 | void *private; |
| 46 | } tcbhead_t; |
| 47 | #endif |
| 48 | |
| 49 | |
| 50 | /* We can support TLS only if the floating-stack support is available. |
| 51 | However, we want to compile in the support and test at runtime whether |
| 52 | the running kernel can support it or not. To avoid bothering with the |
| 53 | TLS support code at all, use configure --without-tls. |
| 54 | |
| 55 | We need USE_TLS to be consistently defined, for ldsodefs.h conditionals. |
| 56 | But some of the code below can cause problems in building libpthread |
| 57 | (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't). */ |
| 58 | |
| 59 | /* LinuxThreads can only support TLS if both floating stacks and support |
| 60 | from the tools are available. |
| 61 | |
| 62 | We have to define USE_TLS consistently, or ldsodefs.h will lay out types |
| 63 | differently between an NPTL build and a LinuxThreads build. It can be set |
| 64 | for libc.so and not libpthread.so, but only if we provide appropriate padding |
| 65 | in the _pthread_descr_struct. |
| 66 | |
| 67 | Currently nothing defines FLOATING_STACKS. We could assume this based on |
| 68 | kernel version once the TLS patches are available in kernel.org. |
| 69 | |
| 70 | To avoid bothering with the TLS support code at all, use configure |
| 71 | --without-tls. */ |
| 72 | |
| 73 | #if defined HAVE_TLS_SUPPORT \ |
| 74 | && (defined FLOATING_STACKS || !defined IS_IN_libpthread) |
| 75 | |
| 76 | /* Signal that TLS support is available. */ |
| 77 | # define USE_TLS 1 |
| 78 | |
| 79 | /* Include padding in _pthread_descr_struct so that libc can find p_errno, |
| 80 | if libpthread will only include the padding because of the !IS_IN_libpthread |
| 81 | check. */ |
| 82 | #ifndef FLOATING_STACKS |
| 83 | # define INCLUDE_TLS_PADDING 1 |
| 84 | #endif |
| 85 | |
| 86 | # ifndef __ASSEMBLER__ |
| 87 | /* Get system call information. */ |
| 88 | # include <sysdep.h> |
| 89 | |
| 90 | /* This is the size of the initial TCB. */ |
| 91 | # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t) |
| 92 | |
| 93 | /* Alignment requirements for the initial TCB. */ |
| 94 | # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t) |
| 95 | |
| 96 | /* This is the size of the TCB. */ |
| 97 | # define TLS_TCB_SIZE sizeof (tcbhead_t) |
| 98 | |
| 99 | /* Alignment requirements for the TCB. */ |
| 100 | # define TLS_TCB_ALIGN __alignof__ (tcbhead_t) |
| 101 | |
| 102 | /* This is the size we need before TCB. */ |
| 103 | # define TLS_PRE_TCB_SIZE sizeof (struct _pthread_descr_struct) |
| 104 | |
| 105 | /* The DTV is allocated at the TP; the TCB is placed elsewhere. */ |
| 106 | # define TLS_DTV_AT_TP 1 |
| 107 | |
| 108 | /* Install the dtv pointer. The pointer passed is to the element with |
| 109 | index -1 which contain the length. */ |
| 110 | # define INSTALL_DTV(TCBP, DTVP) \ |
| 111 | (((tcbhead_t *) (TCBP))->dtv = (DTVP) + 1) |
| 112 | |
| 113 | /* Install new dtv for current thread. */ |
| 114 | # define INSTALL_NEW_DTV(DTV) \ |
| 115 | (((tcbhead_t *)__builtin_thread_pointer ())->dtv = (DTV)) |
| 116 | |
| 117 | /* Return dtv of given thread descriptor. */ |
| 118 | # define GET_DTV(TCBP) \ |
| 119 | (((tcbhead_t *) (TCBP))->dtv) |
| 120 | |
| 121 | /* Code to initially initialize the thread pointer. This might need |
| 122 | special attention since 'errno' is not yet available and if the |
| 123 | operation can cause a failure 'errno' must not be touched. */ |
| 124 | # define TLS_INIT_TP(TCBP, SECONDCALL) \ |
| 125 | ({ INTERNAL_SYSCALL_DECL (err); \ |
| 126 | long result_var; \ |
| 127 | result_var = INTERNAL_SYSCALL_ARM (set_tls, err, 1, (TCBP)); \ |
| 128 | INTERNAL_SYSCALL_ERROR_P (result_var, err) \ |
| 129 | ? "unknown error" : NULL; }) |
| 130 | |
| 131 | /* Return the address of the dtv for the current thread. */ |
| 132 | # define THREAD_DTV() \ |
| 133 | (((tcbhead_t *)__builtin_thread_pointer ())->dtv) |
| 134 | |
| 135 | /* Return the thread descriptor for the current thread. */ |
| 136 | # undef THREAD_SELF |
| 137 | # define THREAD_SELF \ |
| 138 | ((pthread_descr)__builtin_thread_pointer () - 1) |
| 139 | |
| 140 | # undef INIT_THREAD_SELF |
| 141 | # define INIT_THREAD_SELF(DESCR, NR) \ |
| 142 | TLS_INIT_TP ((struct _pthread_descr_struct *)(DESCR) + 1, 0) |
| 143 | |
| 144 | /* Get the thread descriptor definition. */ |
| 145 | # include <linuxthreads/descr.h> |
| 146 | |
| 147 | /* ??? Generic bits of LinuxThreads may call these macros with |
| 148 | DESCR set to NULL. We are expected to be able to reference |
| 149 | the "current" value. |
| 150 | |
| 151 | In our case, we'd really prefer to use DESCR, since lots of |
| 152 | PAL_code calls would be expensive. We can only trust that |
| 153 | the compiler does its job and unifies the multiple |
| 154 | __builtin_thread_pointer instances. */ |
| 155 | |
| 156 | #define THREAD_GETMEM(descr, member) \ |
| 157 | ((void) sizeof (descr), THREAD_SELF->member) |
| 158 | #define THREAD_GETMEM_NC(descr, member) \ |
| 159 | ((void) sizeof (descr), THREAD_SELF->member) |
| 160 | #define THREAD_SETMEM(descr, member, value) \ |
| 161 | ((void) sizeof (descr), THREAD_SELF->member = (value)) |
| 162 | #define THREAD_SETMEM_NC(descr, member, value) \ |
| 163 | ((void) sizeof (descr), THREAD_SELF->member = (value)) |
| 164 | |
| 165 | /* Initializing the thread pointer will generate a SIGILL if the syscall |
| 166 | is not available. */ |
| 167 | #define TLS_INIT_TP_EXPENSIVE 1 |
| 168 | |
| 169 | # endif /* HAVE_TLS_SUPPORT */ |
| 170 | #endif /* __ASSEMBLER__ */ |
| 171 | |
| 172 | #endif /* tls.h */ |