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