xf.li | bfc6e71 | 2025-02-07 01:54:34 -0800 | [diff] [blame^] | 1 | /* Definition for thread-local data handling. NPTL/PowerPC version. |
| 2 | Copyright (C) 2003-2016 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, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifndef _TLS_H |
| 20 | #define _TLS_H 1 |
| 21 | |
| 22 | # include <dl-sysdep.h> |
| 23 | |
| 24 | #ifndef __ASSEMBLER__ |
| 25 | # include <stdbool.h> |
| 26 | # include <stddef.h> |
| 27 | # include <stdint.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 | #else /* __ASSEMBLER__ */ |
| 41 | # include <tcb-offsets.h> |
| 42 | #endif /* __ASSEMBLER__ */ |
| 43 | |
| 44 | |
| 45 | #ifndef __ASSEMBLER__ |
| 46 | |
| 47 | # include <hwcapinfo.h> |
| 48 | |
| 49 | /* Get system call information. */ |
| 50 | # include <sysdep.h> |
| 51 | |
| 52 | /* The TP points to the start of the thread blocks. */ |
| 53 | # define TLS_DTV_AT_TP 1 |
| 54 | # define TLS_TCB_AT_TP 0 |
| 55 | |
| 56 | /* We use the multiple_threads field in the pthread struct */ |
| 57 | #define TLS_MULTIPLE_THREADS_IN_TCB 1 |
| 58 | |
| 59 | /* Get the thread descriptor definition. */ |
| 60 | # include <nptl/descr.h> |
| 61 | |
| 62 | |
| 63 | /* The stack_guard is accessed directly by GCC -fstack-protector code, |
| 64 | so it is a part of public ABI. The dtv and pointer_guard fields |
| 65 | are private. */ |
| 66 | typedef struct |
| 67 | { |
| 68 | /* Reservation for HWCAP data. To be accessed by GCC in |
| 69 | __builtin_cpu_supports(), so it is a part of public ABI. */ |
| 70 | uint64_t hwcap; |
| 71 | /* Reservation for AT_PLATFORM data. To be accessed by GCC in |
| 72 | __builtin_cpu_is(), so it is a part of public ABI. Since there |
| 73 | are different ABIs for 32 and 64 bit, we put this field in a |
| 74 | previously empty padding space for powerpc64. */ |
| 75 | #ifndef __powerpc64__ |
| 76 | /* Padding to maintain alignment. */ |
| 77 | uint32_t padding; |
| 78 | uint32_t at_platform; |
| 79 | #endif |
| 80 | /* Indicate if HTM capable (ISA 2.07). */ |
| 81 | uint32_t tm_capable; |
| 82 | /* Reservation for AT_PLATFORM data - powerpc64. */ |
| 83 | #ifdef __powerpc64__ |
| 84 | uint32_t at_platform; |
| 85 | #endif |
| 86 | /* Reservation for Dynamic System Optimizer ABI. */ |
| 87 | uintptr_t dso_slot2; |
| 88 | uintptr_t dso_slot1; |
| 89 | /* Reservation for tar register (ISA 2.07). */ |
| 90 | uintptr_t tar_save; |
| 91 | /* GCC split stack support. */ |
| 92 | void *__private_ss; |
| 93 | /* Reservation for the Event-Based Branching ABI. */ |
| 94 | uintptr_t ebb_handler; |
| 95 | uintptr_t ebb_ctx_pointer; |
| 96 | uintptr_t ebb_reserved1; |
| 97 | uintptr_t ebb_reserved2; |
| 98 | uintptr_t pointer_guard; |
| 99 | uintptr_t stack_guard; |
| 100 | dtv_t *dtv; |
| 101 | } tcbhead_t; |
| 102 | |
| 103 | /* This is the size of the initial TCB. */ |
| 104 | # define TLS_INIT_TCB_SIZE 0 |
| 105 | |
| 106 | /* Alignment requirements for the initial TCB. */ |
| 107 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) |
| 108 | |
| 109 | /* This is the size of the TCB. */ |
| 110 | # define TLS_TCB_SIZE 0 |
| 111 | |
| 112 | /* Alignment requirements for the TCB. */ |
| 113 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) |
| 114 | |
| 115 | /* This is the size we need before TCB. */ |
| 116 | # define TLS_PRE_TCB_SIZE \ |
| 117 | (sizeof (struct pthread) \ |
| 118 | + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1))) |
| 119 | |
| 120 | # ifndef __powerpc64__ |
| 121 | /* Register r2 (tp) is reserved by the ABI as "thread pointer". */ |
| 122 | register void *__thread_register __asm__ ("r2"); |
| 123 | # define PT_THREAD_POINTER PT_R2 |
| 124 | # else |
| 125 | /* Register r13 (tp) is reserved by the ABI as "thread pointer". */ |
| 126 | register void *__thread_register __asm__ ("r13"); |
| 127 | # define PT_THREAD_POINTER PT_R13 |
| 128 | # endif |
| 129 | |
| 130 | /* The following assumes that TP (R2 or R13) points to the end of the |
| 131 | TCB + 0x7000 (per the ABI). This implies that TCB address is |
| 132 | TP - 0x7000. As we define TLS_DTV_AT_TP we can |
| 133 | assume that the pthread struct is allocated immediately ahead of the |
| 134 | TCB. This implies that the pthread_descr address is |
| 135 | TP - (TLS_PRE_TCB_SIZE + 0x7000). */ |
| 136 | # define TLS_TCB_OFFSET 0x7000 |
| 137 | |
| 138 | /* Install the dtv pointer. The pointer passed is to the element with |
| 139 | index -1 which contain the length. */ |
| 140 | # define INSTALL_DTV(tcbp, dtvp) \ |
| 141 | ((tcbhead_t *) (tcbp))[-1].dtv = dtvp + 1 |
| 142 | |
| 143 | /* Install new dtv for current thread. */ |
| 144 | # define INSTALL_NEW_DTV(dtv) (THREAD_DTV() = (dtv)) |
| 145 | |
| 146 | /* Return dtv of given thread descriptor. */ |
| 147 | # define GET_DTV(tcbp) (((tcbhead_t *) (tcbp))[-1].dtv) |
| 148 | |
| 149 | /* Code to initially initialize the thread pointer. This might need |
| 150 | special attention since 'errno' is not yet available and if the |
| 151 | operation can cause a failure 'errno' must not be touched. */ |
| 152 | # define TLS_INIT_TP(tcbp) \ |
| 153 | ({ \ |
| 154 | __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET; \ |
| 155 | THREAD_SET_TM_CAPABLE (__tcb_hwcap & PPC_FEATURE2_HAS_HTM ? 1 : 0); \ |
| 156 | THREAD_SET_HWCAP (__tcb_hwcap); \ |
| 157 | THREAD_SET_AT_PLATFORM (__tcb_platform); \ |
| 158 | NULL; \ |
| 159 | }) |
| 160 | |
| 161 | /* Value passed to 'clone' for initialization of the thread register. */ |
| 162 | # define TLS_DEFINE_INIT_TP(tp, pd) \ |
| 163 | void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE; \ |
| 164 | (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].tm_capable) = \ |
| 165 | THREAD_GET_TM_CAPABLE (); \ |
| 166 | (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].hwcap) = \ |
| 167 | THREAD_GET_HWCAP (); \ |
| 168 | (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].at_platform) = \ |
| 169 | THREAD_GET_AT_PLATFORM (); |
| 170 | |
| 171 | /* Return the address of the dtv for the current thread. */ |
| 172 | # define THREAD_DTV() \ |
| 173 | (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv) |
| 174 | |
| 175 | /* Return the thread descriptor for the current thread. */ |
| 176 | # define THREAD_SELF \ |
| 177 | ((struct pthread *) (__thread_register \ |
| 178 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) |
| 179 | |
| 180 | /* Magic for libthread_db to know how to do THREAD_SELF. */ |
| 181 | # define DB_THREAD_SELF \ |
| 182 | REGISTER (32, 32, PT_THREAD_POINTER * 4, \ |
| 183 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \ |
| 184 | REGISTER (64, 64, PT_THREAD_POINTER * 8, \ |
| 185 | - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) |
| 186 | |
| 187 | /* Read member of the thread descriptor directly. */ |
| 188 | # define THREAD_GETMEM(descr, member) ((void)(descr), (THREAD_SELF)->member) |
| 189 | |
| 190 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ |
| 191 | # define THREAD_GETMEM_NC(descr, member, idx) \ |
| 192 | ((void)(descr), (THREAD_SELF)->member[idx]) |
| 193 | |
| 194 | /* Set member of the thread descriptor directly. */ |
| 195 | # define THREAD_SETMEM(descr, member, value) \ |
| 196 | ((void)(descr), (THREAD_SELF)->member = (value)) |
| 197 | |
| 198 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ |
| 199 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ |
| 200 | ((void)(descr), (THREAD_SELF)->member[idx] = (value)) |
| 201 | |
| 202 | /* Set the stack guard field in TCB head. */ |
| 203 | # define THREAD_SET_STACK_GUARD(value) \ |
| 204 | (((tcbhead_t *) ((char *) __thread_register \ |
| 205 | - TLS_TCB_OFFSET))[-1].stack_guard = (value)) |
| 206 | # define THREAD_COPY_STACK_GUARD(descr) \ |
| 207 | (((tcbhead_t *) ((char *) (descr) \ |
| 208 | + TLS_PRE_TCB_SIZE))[-1].stack_guard \ |
| 209 | = ((tcbhead_t *) ((char *) __thread_register \ |
| 210 | - TLS_TCB_OFFSET))[-1].stack_guard) |
| 211 | |
| 212 | /* Set the stack guard field in TCB head. */ |
| 213 | # define THREAD_GET_POINTER_GUARD() \ |
| 214 | (((tcbhead_t *) ((char *) __thread_register \ |
| 215 | - TLS_TCB_OFFSET))[-1].pointer_guard) |
| 216 | # define THREAD_SET_POINTER_GUARD(value) \ |
| 217 | (THREAD_GET_POINTER_GUARD () = (value)) |
| 218 | # define THREAD_COPY_POINTER_GUARD(descr) \ |
| 219 | (((tcbhead_t *) ((char *) (descr) \ |
| 220 | + TLS_PRE_TCB_SIZE))[-1].pointer_guard \ |
| 221 | = THREAD_GET_POINTER_GUARD()) |
| 222 | |
| 223 | /* tm_capable field in TCB head. */ |
| 224 | # define THREAD_GET_TM_CAPABLE() \ |
| 225 | (((tcbhead_t *) ((char *) __thread_register \ |
| 226 | - TLS_TCB_OFFSET))[-1].tm_capable) |
| 227 | # define THREAD_SET_TM_CAPABLE(value) \ |
| 228 | (THREAD_GET_TM_CAPABLE () = (value)) |
| 229 | |
| 230 | /* hwcap field in TCB head. */ |
| 231 | # define THREAD_GET_HWCAP() \ |
| 232 | (((tcbhead_t *) ((char *) __thread_register \ |
| 233 | - TLS_TCB_OFFSET))[-1].hwcap) |
| 234 | # define THREAD_SET_HWCAP(value) \ |
| 235 | (THREAD_GET_HWCAP () = (value)) |
| 236 | |
| 237 | /* at_platform field in TCB head. */ |
| 238 | # define THREAD_GET_AT_PLATFORM() \ |
| 239 | (((tcbhead_t *) ((char *) __thread_register \ |
| 240 | - TLS_TCB_OFFSET))[-1].at_platform) |
| 241 | # define THREAD_SET_AT_PLATFORM(value) \ |
| 242 | (THREAD_GET_AT_PLATFORM () = (value)) |
| 243 | |
| 244 | /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some |
| 245 | different value to mean unset l_tls_offset. */ |
| 246 | # define NO_TLS_OFFSET -1 |
| 247 | |
| 248 | /* Get and set the global scope generation counter in struct pthread. */ |
| 249 | #define THREAD_GSCOPE_FLAG_UNUSED 0 |
| 250 | #define THREAD_GSCOPE_FLAG_USED 1 |
| 251 | #define THREAD_GSCOPE_FLAG_WAIT 2 |
| 252 | #define THREAD_GSCOPE_RESET_FLAG() \ |
| 253 | do \ |
| 254 | { int __res \ |
| 255 | = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \ |
| 256 | THREAD_GSCOPE_FLAG_UNUSED); \ |
| 257 | if (__res == THREAD_GSCOPE_FLAG_WAIT) \ |
| 258 | lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ |
| 259 | } \ |
| 260 | while (0) |
| 261 | #define THREAD_GSCOPE_SET_FLAG() \ |
| 262 | do \ |
| 263 | { \ |
| 264 | THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \ |
| 265 | atomic_write_barrier (); \ |
| 266 | } \ |
| 267 | while (0) |
| 268 | #define THREAD_GSCOPE_WAIT() \ |
| 269 | GL(dl_wait_lookup_done) () |
| 270 | |
| 271 | #endif /* __ASSEMBLER__ */ |
| 272 | |
| 273 | #endif /* tls.h */ |