1 /* Definition for thread-local data handling. NPTL/PowerPC version. 2 Copyright (C) 2003-2022 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 <https://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 # include <dl-dtv.h> 29 # include <thread_pointer.h> 30 31 #else /* __ASSEMBLER__ */ 32 # include <tcb-offsets.h> 33 # define __ASSEMBLY__ 34 # include <asm/ptrace.h> 35 #endif /* __ASSEMBLER__ */ 36 37 #ifndef __powerpc64__ 38 /* Register r2 (tp) is reserved by the ABI as "thread pointer". */ 39 # define PT_THREAD_POINTER PT_R2 40 41 #else /* __powerpc64__ */ 42 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */ 43 # define PT_THREAD_POINTER PT_R13 44 #endif /* __powerpc64__ */ 45 46 #ifndef __ASSEMBLER__ 47 48 /* Get system call information. */ 49 # include <sysdep.h> 50 51 /* The TP points to the start of the thread blocks. */ 52 # define TLS_DTV_AT_TP 1 53 # define TLS_TCB_AT_TP 0 54 55 /* We use the multiple_threads field in the pthread struct */ 56 #define TLS_MULTIPLE_THREADS_IN_TCB 1 57 58 /* Get the thread descriptor definition. */ 59 # include <nptl/descr.h> 60 61 62 /* The stack_guard is accessed directly by GCC -fstack-protector code, 63 so it is a part of public ABI. The dtv and pointer_guard fields 64 are private. */ 65 typedef struct 66 { 67 /* Reservation for HWCAP data. To be accessed by GCC in 68 __builtin_cpu_supports(), so it is a part of public ABI. */ 69 uint64_t hwcap; 70 /* Reservation for AT_PLATFORM data. To be accessed by GCC in 71 __builtin_cpu_is(), so it is a part of public ABI. Since there 72 are different ABIs for 32 and 64 bit, we put this field in a 73 previously empty padding space for powerpc64. */ 74 #ifndef __powerpc64__ 75 /* Padding to maintain alignment. */ 76 uint32_t padding; 77 uint32_t at_platform; 78 #endif 79 uint32_t __unused; 80 /* Reservation for AT_PLATFORM data - powerpc64. */ 81 #ifdef __powerpc64__ 82 uint32_t at_platform; 83 #endif 84 /* Reservation for Dynamic System Optimizer ABI. */ 85 uintptr_t dso_slot2; 86 uintptr_t dso_slot1; 87 /* Reservation for tar register (ISA 2.07). */ 88 uintptr_t tar_save; 89 /* GCC split stack support. */ 90 void *__private_ss; 91 /* Reservation for the Event-Based Branching ABI. */ 92 uintptr_t ebb_handler; 93 uintptr_t ebb_ctx_pointer; 94 uintptr_t ebb_reserved1; 95 uintptr_t ebb_reserved2; 96 uintptr_t pointer_guard; 97 uintptr_t stack_guard; 98 dtv_t *dtv; 99 } tcbhead_t; 100 101 # include <hwcapinfo.h> 102 103 /* This is the size of the initial TCB. */ 104 # define TLS_INIT_TCB_SIZE 0 105 106 /* This is the size of the TCB. */ 107 # define TLS_TCB_SIZE 0 108 109 /* This is the size we need before TCB. */ 110 # define TLS_PRE_TCB_SIZE \ 111 (sizeof (struct pthread) \ 112 + ((sizeof (tcbhead_t) + __alignof (struct pthread) - 1) \ 113 & ~(__alignof (struct pthread) - 1))) 114 115 /* The following assumes that TP (R2 or R13) points to the end of the 116 TCB + 0x7000 (per the ABI). This implies that TCB address is 117 TP - 0x7000. As we define TLS_DTV_AT_TP we can 118 assume that the pthread struct is allocated immediately ahead of the 119 TCB. This implies that the pthread_descr address is 120 TP - (TLS_PRE_TCB_SIZE + 0x7000). */ 121 # define TLS_TCB_OFFSET 0x7000 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) \ 138 ({ \ 139 __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET; \ 140 THREAD_SET_HWCAP (__tcb.hwcap); \ 141 THREAD_SET_AT_PLATFORM (__tcb.at_platform); \ 142 NULL; \ 143 }) 144 145 /* Value passed to 'clone' for initialization of the thread register. */ 146 # define TLS_DEFINE_INIT_TP(tp, pd) \ 147 void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE; \ 148 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].hwcap) = \ 149 THREAD_GET_HWCAP (); \ 150 (((tcbhead_t *) ((char *) tp - TLS_TCB_OFFSET))[-1].at_platform) = \ 151 THREAD_GET_AT_PLATFORM (); 152 153 /* Return the address of the dtv for the current thread. */ 154 # define THREAD_DTV() \ 155 (((tcbhead_t *) (__thread_register - TLS_TCB_OFFSET))[-1].dtv) 156 157 /* Return the thread descriptor for the current thread. */ 158 # define THREAD_SELF \ 159 ((struct pthread *) (__thread_register \ 160 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE)) 161 162 /* Magic for libthread_db to know how to do THREAD_SELF. */ 163 # define DB_THREAD_SELF \ 164 REGISTER (32, 32, PT_THREAD_POINTER * 4, \ 165 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) \ 166 REGISTER (64, 64, PT_THREAD_POINTER * 8, \ 167 - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE) 168 169 # include <tcb-access.h> 170 171 /* Set the stack guard field in TCB head. */ 172 # define THREAD_SET_STACK_GUARD(value) \ 173 (((tcbhead_t *) ((char *) __thread_register \ 174 - TLS_TCB_OFFSET))[-1].stack_guard = (value)) 175 # define THREAD_COPY_STACK_GUARD(descr) \ 176 (((tcbhead_t *) ((char *) (descr) \ 177 + TLS_PRE_TCB_SIZE))[-1].stack_guard \ 178 = ((tcbhead_t *) ((char *) __thread_register \ 179 - TLS_TCB_OFFSET))[-1].stack_guard) 180 181 /* Set the stack guard field in TCB head. */ 182 # define THREAD_GET_POINTER_GUARD() \ 183 (((tcbhead_t *) ((char *) __thread_register \ 184 - TLS_TCB_OFFSET))[-1].pointer_guard) 185 # define THREAD_SET_POINTER_GUARD(value) \ 186 (THREAD_GET_POINTER_GUARD () = (value)) 187 # define THREAD_COPY_POINTER_GUARD(descr) \ 188 (((tcbhead_t *) ((char *) (descr) \ 189 + TLS_PRE_TCB_SIZE))[-1].pointer_guard \ 190 = THREAD_GET_POINTER_GUARD()) 191 192 /* hwcap field in TCB head. */ 193 # define THREAD_GET_HWCAP() \ 194 (((tcbhead_t *) ((char *) __thread_register \ 195 - TLS_TCB_OFFSET))[-1].hwcap) 196 # define THREAD_SET_HWCAP(value) \ 197 (THREAD_GET_HWCAP () = (value)) 198 199 /* at_platform field in TCB head. */ 200 # define THREAD_GET_AT_PLATFORM() \ 201 (((tcbhead_t *) ((char *) __thread_register \ 202 - TLS_TCB_OFFSET))[-1].at_platform) 203 # define THREAD_SET_AT_PLATFORM(value) \ 204 (THREAD_GET_AT_PLATFORM () = (value)) 205 206 /* l_tls_offset == 0 is perfectly valid on PPC, so we have to use some 207 different value to mean unset l_tls_offset. */ 208 # define NO_TLS_OFFSET -1 209 210 /* Get and set the global scope generation counter in struct pthread. */ 211 #define THREAD_GSCOPE_FLAG_UNUSED 0 212 #define THREAD_GSCOPE_FLAG_USED 1 213 #define THREAD_GSCOPE_FLAG_WAIT 2 214 #define THREAD_GSCOPE_RESET_FLAG() \ 215 do \ 216 { int __res \ 217 = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag, \ 218 THREAD_GSCOPE_FLAG_UNUSED); \ 219 if (__res == THREAD_GSCOPE_FLAG_WAIT) \ 220 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ 221 } \ 222 while (0) 223 #define THREAD_GSCOPE_SET_FLAG() \ 224 do \ 225 { \ 226 THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED; \ 227 atomic_write_barrier (); \ 228 } \ 229 while (0) 230 231 #endif /* __ASSEMBLER__ */ 232 233 #endif /* tls.h */ 234