yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* Definitions for thread-local data handling. linuxthreads/MIPS version. |
| 2 | Copyright (C) 2005 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 | # define READ_THREAD_POINTER() \ |
| 41 | ({ void *__result; \ |
| 42 | __asm__ __volatile__ (".set\tpush\n\t.set\tmips32r2\n\t" \ |
| 43 | "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result)); \ |
| 44 | __result; }) |
| 45 | |
| 46 | #else /* __ASSEMBLER__ */ |
| 47 | # include <tcb-offsets.h> |
| 48 | |
| 49 | /* Note: rd must be $v1 to be ABI-conformant. */ |
| 50 | # define READ_THREAD_POINTER(rd) \ |
| 51 | .set push; \ |
| 52 | .set mips32r2; \ |
| 53 | rdhwr rd, $29; \ |
| 54 | .set pop |
| 55 | #endif /* __ASSEMBLER__ */ |
| 56 | |
| 57 | /* LinuxThreads can only use TLS if both floating stacks (in the MIPS case, |
| 58 | that means support for "rdhwr") and support from the tools are available. |
| 59 | |
| 60 | We have to define USE_TLS consistently, or ldsodefs.h will lay out types |
| 61 | differently between an NPTL build and a LinuxThreads build. It can be set |
| 62 | for libc.so and not libpthread.so, but only if we provide appropriate padding |
| 63 | in the _pthread_descr_struct. |
| 64 | |
| 65 | Currently nothing defines FLOATING_STACKS. We could assume this based on |
| 66 | kernel version once the TLS patches are available in kernel.org, but |
| 67 | it hardly seems worth it. Use NPTL if you can. |
| 68 | |
| 69 | To avoid bothering with the TLS support code at all, use configure |
| 70 | --without-tls. */ |
| 71 | |
| 72 | #if defined HAVE_TLS_SUPPORT \ |
| 73 | && (defined FLOATING_STACKS || !defined IS_IN_libpthread) |
| 74 | |
| 75 | /* Signal that TLS support is available. */ |
| 76 | # define USE_TLS 1 |
| 77 | |
| 78 | /* Include padding in _pthread_descr_struct so that libc can find p_errno, |
| 79 | if libpthread will only include the padding because of the !IS_IN_libpthread |
| 80 | check. */ |
| 81 | #ifndef FLOATING_STACKS |
| 82 | # define INCLUDE_TLS_PADDING 1 |
| 83 | #endif |
| 84 | |
| 85 | # ifndef __ASSEMBLER__ |
| 86 | |
| 87 | /* This layout is actually wholly private and not affected by the ABI. |
| 88 | Nor does it overlap the pthread data structure, so we need nothing |
| 89 | extra here at all. */ |
| 90 | typedef struct |
| 91 | { |
| 92 | dtv_t *dtv; |
| 93 | void *private; |
| 94 | } tcbhead_t; |
| 95 | |
| 96 | /* This is the size of the initial TCB. */ |
| 97 | # define TLS_INIT_TCB_SIZE 0 |
| 98 | |
| 99 | /* Alignment requirements for the initial TCB. */ |
| 100 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct _pthread_descr_struct) |
| 101 | |
| 102 | /* This is the size of the TCB. */ |
| 103 | # define TLS_TCB_SIZE 0 |
| 104 | |
| 105 | /* Alignment requirements for the TCB. */ |
| 106 | # define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct) |
| 107 | |
| 108 | /* This is the size we need before TCB. */ |
| 109 | # define TLS_PRE_TCB_SIZE \ |
| 110 | (sizeof (struct _pthread_descr_struct) \ |
| 111 | + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1))) |
| 112 | |
| 113 | /* The thread pointer (in hardware register $29) points to the end of |
| 114 | the TCB + 0x7000, as for PowerPC. The pthread_descr structure is |
| 115 | immediately in front of the TCB. */ |
| 116 | #define TLS_TCB_OFFSET 0x7000 |
| 117 | |
| 118 | /* The DTV is allocated at the TP; the TCB is placed elsewhere. */ |
| 119 | /* This is not really true for powerpc64. We are following alpha |
| 120 | where the DTV pointer is first doubleword in the TCB. */ |
| 121 | # define TLS_DTV_AT_TP 1 |
| 122 | |
| 123 | /* Install the dtv pointer. The pointer passed is to the element with |
| 124 | index -1 which contain the length. */ |
| 125 | # define INSTALL_DTV(TCBP, DTVP) \ |
| 126 | (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1) |
| 127 | |
| 128 | /* Install new dtv for current thread. */ |
| 129 | # define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV)) |
| 130 | |
| 131 | /* Return dtv of given thread descriptor. */ |
| 132 | # define GET_DTV(TCBP) (((tcbhead_t *) (TCBP))[-1].dtv) |
| 133 | |
| 134 | /* Code to initially initialize the thread pointer. This might need |
| 135 | special attention since 'errno' is not yet available and if the |
| 136 | operation can cause a failure 'errno' must not be touched. */ |
| 137 | # define TLS_INIT_TP(tcbp, secondcall) \ |
| 138 | ({ INTERNAL_SYSCALL_DECL (err); \ |
| 139 | long result_var; \ |
| 140 | result_var = INTERNAL_SYSCALL (set_thread_area, err, 1, \ |
| 141 | (char *) (tcbp) + TLS_TCB_OFFSET); \ |
| 142 | INTERNAL_SYSCALL_ERROR_P (result_var, err) \ |
| 143 | ? "unknown error" : NULL; }) |
| 144 | |
| 145 | /* Return the address of the dtv for the current thread. */ |
| 146 | # define THREAD_DTV() \ |
| 147 | (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv) |
| 148 | |
| 149 | /* Return the thread descriptor for the current thread. */ |
| 150 | # undef THREAD_SELF |
| 151 | # define THREAD_SELF \ |
| 152 | ((pthread_descr) (READ_THREAD_POINTER () \ |
| 153 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) |
| 154 | |
| 155 | /* Get the thread descriptor definition. */ |
| 156 | # include <linuxthreads/descr.h> |
| 157 | |
| 158 | /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some |
| 159 | different value to mean unset l_tls_offset. */ |
| 160 | # define NO_TLS_OFFSET -1 |
| 161 | |
| 162 | /* Initializing the thread pointer requires a syscall which may not be |
| 163 | available, so don't do it if we don't need to. */ |
| 164 | # define TLS_INIT_TP_EXPENSIVE 1 |
| 165 | |
| 166 | # endif /* __ASSEMBLER__ */ |
| 167 | |
| 168 | #endif /* HAVE_TLS_SUPPORT */ |
| 169 | |
| 170 | #endif /* tls.h */ |