yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* Definition for thread-local data handling. NPTL/PowerPC version. |
| 2 | Copyright (C) 2003, 2005, 2006, 2007 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 1 |
| 22 | |
| 23 | #ifndef __ASSEMBLER__ |
| 24 | # include <stdbool.h> |
| 25 | # include <stddef.h> |
| 26 | # include <stdint.h> |
| 27 | |
| 28 | /* Type for the dtv. */ |
| 29 | typedef union dtv |
| 30 | { |
| 31 | size_t counter; |
| 32 | struct |
| 33 | { |
| 34 | void *val; |
| 35 | bool is_static; |
| 36 | } pointer; |
| 37 | } dtv_t; |
| 38 | |
| 39 | #else /* __ASSEMBLER__ */ |
| 40 | # include <tcb-offsets.h> |
| 41 | #endif /* __ASSEMBLER__ */ |
| 42 | |
| 43 | /* We require TLS support in the tools. */ |
| 44 | #define HAVE_TLS_SUPPORT 1 |
| 45 | #define HAVE_TLS_MODEL_ATTRIBUTE 1 |
| 46 | #define HAVE___THREAD 1 |
| 47 | |
| 48 | /* Signal that TLS support is available. */ |
| 49 | #define USE_TLS 1 |
| 50 | |
| 51 | /* We require TLS support in the tools. */ |
| 52 | #ifndef HAVE_TLS_SUPPORT |
| 53 | # error "TLS support is required." |
| 54 | #endif |
| 55 | |
| 56 | /* Signal that TLS support is available. */ |
| 57 | # define USE_TLS 1 |
| 58 | |
| 59 | #ifndef __ASSEMBLER__ |
| 60 | |
| 61 | /* Get system call information. */ |
| 62 | # include <sysdep.h> |
| 63 | |
| 64 | /* The TP points to the start of the thread blocks. */ |
| 65 | # define TLS_DTV_AT_TP 1 |
| 66 | |
| 67 | /* We use the multiple_threads field in the pthread struct */ |
| 68 | #define TLS_MULTIPLE_THREADS_IN_TCB 1 |
| 69 | |
| 70 | /* Get the thread descriptor definition. */ |
| 71 | # include <../../descr.h> |
| 72 | |
| 73 | /* The stack_guard is accessed directly by GCC -fstack-protector code, |
| 74 | so it is a part of public ABI. The dtv and pointer_guard fields |
| 75 | are private. */ |
| 76 | typedef struct |
| 77 | { |
| 78 | uintptr_t pointer_guard; |
| 79 | uintptr_t stack_guard; |
| 80 | dtv_t *dtv; |
| 81 | } tcbhead_t; |
| 82 | |
| 83 | /* This is the size of the initial TCB. */ |
| 84 | # define TLS_INIT_TCB_SIZE 0 |
| 85 | |
| 86 | /* Alignment requirements for the initial TCB. */ |
| 87 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) |
| 88 | |
| 89 | /* This is the size of the TCB. */ |
| 90 | # define TLS_TCB_SIZE 0 |
| 91 | |
| 92 | /* Alignment requirements for the TCB. */ |
| 93 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) |
| 94 | |
| 95 | /* This is the size we need before TCB. */ |
| 96 | # define TLS_PRE_TCB_SIZE \ |
| 97 | (sizeof (struct pthread) \ |
| 98 | + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1))) |
| 99 | |
| 100 | # ifndef __powerpc64__ |
| 101 | /* Register r2 (tp) is reserved by the ABI as "thread pointer". */ |
| 102 | register void *__thread_register __asm__ ("r2"); |
| 103 | # define PT_THREAD_POINTER PT_R2 |
| 104 | # else |
| 105 | /* Register r13 (tp) is reserved by the ABI as "thread pointer". */ |
| 106 | register void *__thread_register __asm__ ("r13"); |
| 107 | # define PT_THREAD_POINTER PT_R13 |
| 108 | # endif |
| 109 | |
| 110 | /* The following assumes that TP (R2 or R13) points to the end of the |
| 111 | TCB + 0x7000 (per the ABI). This implies that TCB address is |
| 112 | TP - 0x7000. As we define TLS_DTV_AT_TP we can |
| 113 | assume that the pthread struct is allocated immediately ahead of the |
| 114 | TCB. This implies that the pthread_descr address is |
| 115 | TP - (TLS_PRE_TCB_SIZE + 0x7000). */ |
| 116 | # define TLS_TCB_OFFSET 0x7000 |
| 117 | |
| 118 | /* Install the dtv pointer. The pointer passed is to the element with |
| 119 | index -1 which contain the length. */ |
| 120 | # define INSTALL_DTV(tcbp, dtvp) \ |
| 121 | ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1 |
| 122 | |
| 123 | /* Install new dtv for current thread. */ |
| 124 | # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv)) |
| 125 | |
| 126 | /* Return dtv of given thread descriptor. */ |
| 127 | # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv) |
| 128 | |
| 129 | /* Code to initially initialize the thread pointer. This might need |
| 130 | special attention since 'errno' is not yet available and if the |
| 131 | operation can cause a failure 'errno' must not be touched. */ |
| 132 | # define TLS_INIT_TP(tcbp, secondcall) \ |
| 133 | (__thread_register = (void *) (tcbp) + TLS_TCB_OFFSET, NULL) |
| 134 | |
| 135 | /* Return the address of the dtv for the current thread. */ |
| 136 | # define THREAD_DTV() \ |
| 137 | (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv) |
| 138 | |
| 139 | /* Return the thread descriptor for the current thread. */ |
| 140 | # define THREAD_SELF \ |
| 141 | ((struct pthread *) (__thread_register \ |
| 142 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) |
| 143 | |
| 144 | /* Magic for libthread_db to know how to do THREAD_SELF. */ |
| 145 | # define DB_THREAD_SELF \ |
| 146 | REGISTER (32, 32, PT_THREAD_POINTER * 4, \ |
| 147 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \ |
| 148 | REGISTER (64, 64, PT_THREAD_POINTER * 8, \ |
| 149 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) |
| 150 | |
| 151 | /* Read member of the thread descriptor directly. */ |
| 152 | # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member) |
| 153 | |
| 154 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ |
| 155 | # define THREAD_GETMEM_NC(descr, member, idx) \ |
| 156 | ((void)(descr), (THREAD_SELF)->member[idx]) |
| 157 | |
| 158 | /* Set member of the thread descriptor directly. */ |
| 159 | # define THREAD_SETMEM(descr, member, value) \ |
| 160 | ((void)(descr), (THREAD_SELF)->member = (value)) |
| 161 | |
| 162 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ |
| 163 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ |
| 164 | ((void)(descr), (THREAD_SELF)->member[idx] = (value)) |
| 165 | |
| 166 | /* Set the stack guard field in TCB head. */ |
| 167 | # define THREAD_SET_STACK_GUARD(value) \ |
| 168 | (((tcbhead_t *) ((char *) __thread_register \ |
| 169 | - TLS_TCB_OFFSET))[-1].stack_guard = (value)) |
| 170 | # define THREAD_COPY_STACK_GUARD(descr) \ |
| 171 | (((tcbhead_t *) ((char *) (descr) \ |
| 172 | + TLS_PRE_TCB_SIZE))[-1].stack_guard \ |
| 173 | = ((tcbhead_t *) ((char *) __thread_register \ |
| 174 | - TLS_TCB_OFFSET))[-1].stack_guard) |
| 175 | |
| 176 | /* Set the stack guard field in TCB head. */ |
| 177 | # define THREAD_GET_POINTER_GUARD() \ |
| 178 | (((tcbhead_t *) ((char *) __thread_register \ |
| 179 | - TLS_TCB_OFFSET))[-1].pointer_guard) |
| 180 | # define THREAD_SET_POINTER_GUARD(value) \ |
| 181 | (THREAD_GET_POINTER_GUARD () = (value)) |
| 182 | # define THREAD_COPY_POINTER_GUARD(descr) \ |
| 183 | (((tcbhead_t *) ((char *) (descr) \ |
| 184 | + TLS_PRE_TCB_SIZE))[-1].pointer_guard \ |
| 185 | = THREAD_GET_POINTER_GUARD()) |
| 186 | |
| 187 | /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some |
| 188 | different value to mean unset l_tls_offset. */ |
| 189 | # define NO_TLS_OFFSET -1 |
| 190 | |
| 191 | /* Get and set the global scope generation counter in struct pthread. */ |
| 192 | #define THREAD_GSCOPE_FLAG_UNUSED 0 |
| 193 | #define THREAD_GSCOPE_FLAG_USED 1 |
| 194 | #define THREAD_GSCOPE_FLAG_WAIT 2 |
| 195 | #define THREAD_GSCOPE_RESET_FLAG() \ |
| 196 | do \ |
| 197 | { int __res \ |
| 198 | = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \ |
| 199 | THREAD_GSCOPE_FLAG_UNUSED); \ |
| 200 | if (__res == THREAD_GSCOPE_FLAG_WAIT) \ |
| 201 | lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ |
| 202 | } \ |
| 203 | while (0) |
| 204 | #define THREAD_GSCOPE_SET_FLAG() \ |
| 205 | do \ |
| 206 | { \ |
| 207 | THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \ |
| 208 | atomic_write_barrier (); \ |
| 209 | } \ |
| 210 | while (0) |
| 211 | #define THREAD_GSCOPE_WAIT() \ |
| 212 | GL(dl_wait_lookup_done) () |
| 213 | |
| 214 | #endif /* __ASSEMBLER__ */ |
| 215 | |
| 216 | #endif /* tls.h */ |