1 | /* Definition for thread-local data handling. nptl/x86_64 version. |
2 | Copyright (C) 2002-2020 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 | #ifndef __ASSEMBLER__ |
23 | # include <asm/prctl.h> /* For ARCH_SET_FS. */ |
24 | # include <stdbool.h> |
25 | # include <stddef.h> |
26 | # include <stdint.h> |
27 | # include <stdlib.h> |
28 | # include <sysdep.h> |
29 | # include <libc-pointer-arith.h> /* For cast_to_integer. */ |
30 | # include <kernel-features.h> |
31 | # include <dl-dtv.h> |
32 | |
33 | /* Replacement type for __m128 since this file is included by ld.so, |
34 | which is compiled with -mno-sse. It must not change the alignment |
35 | of rtld_savespace_sse. */ |
36 | typedef struct |
37 | { |
38 | int i[4]; |
39 | } __128bits; |
40 | |
41 | |
42 | typedef struct |
43 | { |
44 | void *tcb; /* Pointer to the TCB. Not necessarily the |
45 | thread descriptor used by libpthread. */ |
46 | dtv_t *dtv; |
47 | void *self; /* Pointer to the thread descriptor. */ |
48 | int multiple_threads; |
49 | int gscope_flag; |
50 | uintptr_t sysinfo; |
51 | uintptr_t stack_guard; |
52 | uintptr_t pointer_guard; |
53 | unsigned long int unused_vgetcpu_cache[2]; |
54 | /* Bit 0: X86_FEATURE_1_IBT. |
55 | Bit 1: X86_FEATURE_1_SHSTK. |
56 | */ |
57 | unsigned int feature_1; |
58 | int __glibc_unused1; |
59 | /* Reservation of some values for the TM ABI. */ |
60 | void *__private_tm[4]; |
61 | /* GCC split stack support. */ |
62 | void *__private_ss; |
63 | /* The lowest address of shadow stack, */ |
64 | unsigned long long int ssp_base; |
65 | /* Must be kept even if it is no longer used by glibc since programs, |
66 | like AddressSanitizer, depend on the size of tcbhead_t. */ |
67 | __128bits __glibc_unused2[8][4] __attribute__ ((aligned (32))); |
68 | |
69 | void *__padding[8]; |
70 | } tcbhead_t; |
71 | |
72 | # ifdef __ILP32__ |
73 | /* morestack.S in libgcc uses offset 0x40 to access __private_ss, */ |
74 | _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x40, |
75 | "offset of __private_ss != 0x40" ); |
76 | /* NB: ssp_base used to be "long int __glibc_reserved2", which was |
77 | changed from 32 bits to 64 bits. Make sure that the offset of the |
78 | next field, __glibc_unused2, is unchanged. */ |
79 | _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x60, |
80 | "offset of __glibc_unused2 != 0x60" ); |
81 | # else |
82 | /* morestack.S in libgcc uses offset 0x70 to access __private_ss, */ |
83 | _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x70, |
84 | "offset of __private_ss != 0x70" ); |
85 | _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80, |
86 | "offset of __glibc_unused2 != 0x80" ); |
87 | # endif |
88 | |
89 | #else /* __ASSEMBLER__ */ |
90 | # include <tcb-offsets.h> |
91 | #endif |
92 | |
93 | |
94 | /* Alignment requirement for the stack. */ |
95 | #define STACK_ALIGN 16 |
96 | |
97 | |
98 | #ifndef __ASSEMBLER__ |
99 | /* Get system call information. */ |
100 | # include <sysdep.h> |
101 | |
102 | #ifndef LOCK_PREFIX |
103 | # ifdef UP |
104 | # define LOCK_PREFIX /* nothing */ |
105 | # else |
106 | # define LOCK_PREFIX "lock;" |
107 | # endif |
108 | #endif |
109 | |
110 | /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t), |
111 | because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole |
112 | struct pthread even when not linked with -lpthread. */ |
113 | # define TLS_INIT_TCB_SIZE sizeof (struct pthread) |
114 | |
115 | /* Alignment requirements for the initial TCB. */ |
116 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) |
117 | |
118 | /* This is the size of the TCB. */ |
119 | # define TLS_TCB_SIZE sizeof (struct pthread) |
120 | |
121 | /* Alignment requirements for the TCB. */ |
122 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) |
123 | |
124 | /* The TCB can have any size and the memory following the address the |
125 | thread pointer points to is unspecified. Allocate the TCB there. */ |
126 | # define TLS_TCB_AT_TP 1 |
127 | # define TLS_DTV_AT_TP 0 |
128 | |
129 | /* Get the thread descriptor definition. */ |
130 | # include <nptl/descr.h> |
131 | |
132 | |
133 | /* Install the dtv pointer. The pointer passed is to the element with |
134 | index -1 which contain the length. */ |
135 | # define INSTALL_DTV(descr, dtvp) \ |
136 | ((tcbhead_t *) (descr))->dtv = (dtvp) + 1 |
137 | |
138 | /* Install new dtv for current thread. */ |
139 | # define INSTALL_NEW_DTV(dtvp) \ |
140 | ({ struct pthread *__pd; \ |
141 | THREAD_SETMEM (__pd, header.dtv, (dtvp)); }) |
142 | |
143 | /* Return dtv of given thread descriptor. */ |
144 | # define GET_DTV(descr) \ |
145 | (((tcbhead_t *) (descr))->dtv) |
146 | |
147 | |
148 | /* Code to initially initialize the thread pointer. This might need |
149 | special attention since 'errno' is not yet available and if the |
150 | operation can cause a failure 'errno' must not be touched. |
151 | |
152 | We have to make the syscall for both uses of the macro since the |
153 | address might be (and probably is) different. */ |
154 | # define TLS_INIT_TP(thrdescr) \ |
155 | ({ void *_thrdescr = (thrdescr); \ |
156 | tcbhead_t *_head = _thrdescr; \ |
157 | int _result; \ |
158 | \ |
159 | _head->tcb = _thrdescr; \ |
160 | /* For now the thread descriptor is at the same address. */ \ |
161 | _head->self = _thrdescr; \ |
162 | \ |
163 | /* It is a simple syscall to set the %fs value for the thread. */ \ |
164 | asm volatile ("syscall" \ |
165 | : "=a" (_result) \ |
166 | : "0" ((unsigned long int) __NR_arch_prctl), \ |
167 | "D" ((unsigned long int) ARCH_SET_FS), \ |
168 | "S" (_thrdescr) \ |
169 | : "memory", "cc", "r11", "cx"); \ |
170 | \ |
171 | _result ? "cannot set %fs base address for thread-local storage" : 0; \ |
172 | }) |
173 | |
174 | # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) |
175 | |
176 | |
177 | /* Return the address of the dtv for the current thread. */ |
178 | # define THREAD_DTV() \ |
179 | ({ struct pthread *__pd; \ |
180 | THREAD_GETMEM (__pd, header.dtv); }) |
181 | |
182 | |
183 | /* Return the thread descriptor for the current thread. |
184 | |
185 | The contained asm must *not* be marked volatile since otherwise |
186 | assignments like |
187 | pthread_descr self = thread_self(); |
188 | do not get optimized away. */ |
189 | # define THREAD_SELF \ |
190 | ({ struct pthread *__self; \ |
191 | asm ("mov %%fs:%c1,%0" : "=r" (__self) \ |
192 | : "i" (offsetof (struct pthread, header.self))); \ |
193 | __self;}) |
194 | |
195 | /* Magic for libthread_db to know how to do THREAD_SELF. */ |
196 | # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */ |
197 | # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS) |
198 | |
199 | /* Read member of the thread descriptor directly. */ |
200 | # define THREAD_GETMEM(descr, member) \ |
201 | ({ __typeof (descr->member) __value; \ |
202 | _Static_assert (sizeof (__value) == 1 \ |
203 | || sizeof (__value) == 4 \ |
204 | || sizeof (__value) == 8, \ |
205 | "size of per-thread data"); \ |
206 | if (sizeof (__value) == 1) \ |
207 | asm volatile ("movb %%fs:%P2,%b0" \ |
208 | : "=q" (__value) \ |
209 | : "0" (0), "i" (offsetof (struct pthread, member))); \ |
210 | else if (sizeof (__value) == 4) \ |
211 | asm volatile ("movl %%fs:%P1,%0" \ |
212 | : "=r" (__value) \ |
213 | : "i" (offsetof (struct pthread, member))); \ |
214 | else /* 8 */ \ |
215 | { \ |
216 | asm volatile ("movq %%fs:%P1,%q0" \ |
217 | : "=r" (__value) \ |
218 | : "i" (offsetof (struct pthread, member))); \ |
219 | } \ |
220 | __value; }) |
221 | |
222 | |
223 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ |
224 | # define THREAD_GETMEM_NC(descr, member, idx) \ |
225 | ({ __typeof (descr->member[0]) __value; \ |
226 | _Static_assert (sizeof (__value) == 1 \ |
227 | || sizeof (__value) == 4 \ |
228 | || sizeof (__value) == 8, \ |
229 | "size of per-thread data"); \ |
230 | if (sizeof (__value) == 1) \ |
231 | asm volatile ("movb %%fs:%P2(%q3),%b0" \ |
232 | : "=q" (__value) \ |
233 | : "0" (0), "i" (offsetof (struct pthread, member[0])), \ |
234 | "r" (idx)); \ |
235 | else if (sizeof (__value) == 4) \ |
236 | asm volatile ("movl %%fs:%P1(,%q2,4),%0" \ |
237 | : "=r" (__value) \ |
238 | : "i" (offsetof (struct pthread, member[0])), "r" (idx));\ |
239 | else /* 8 */ \ |
240 | { \ |
241 | asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \ |
242 | : "=r" (__value) \ |
243 | : "i" (offsetof (struct pthread, member[0])), \ |
244 | "r" (idx)); \ |
245 | } \ |
246 | __value; }) |
247 | |
248 | |
249 | /* Loading addresses of objects on x86-64 needs to be treated special |
250 | when generating PIC code. */ |
251 | #ifdef __pic__ |
252 | # define IMM_MODE "nr" |
253 | #else |
254 | # define IMM_MODE "ir" |
255 | #endif |
256 | |
257 | |
258 | /* Set member of the thread descriptor directly. */ |
259 | # define THREAD_SETMEM(descr, member, value) \ |
260 | ({ \ |
261 | _Static_assert (sizeof (descr->member) == 1 \ |
262 | || sizeof (descr->member) == 4 \ |
263 | || sizeof (descr->member) == 8, \ |
264 | "size of per-thread data"); \ |
265 | if (sizeof (descr->member) == 1) \ |
266 | asm volatile ("movb %b0,%%fs:%P1" : \ |
267 | : "iq" (value), \ |
268 | "i" (offsetof (struct pthread, member))); \ |
269 | else if (sizeof (descr->member) == 4) \ |
270 | asm volatile ("movl %0,%%fs:%P1" : \ |
271 | : IMM_MODE (value), \ |
272 | "i" (offsetof (struct pthread, member))); \ |
273 | else /* 8 */ \ |
274 | { \ |
275 | asm volatile ("movq %q0,%%fs:%P1" : \ |
276 | : IMM_MODE ((uint64_t) cast_to_integer (value)), \ |
277 | "i" (offsetof (struct pthread, member))); \ |
278 | }}) |
279 | |
280 | |
281 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ |
282 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ |
283 | ({ \ |
284 | _Static_assert (sizeof (descr->member[0]) == 1 \ |
285 | || sizeof (descr->member[0]) == 4 \ |
286 | || sizeof (descr->member[0]) == 8, \ |
287 | "size of per-thread data"); \ |
288 | if (sizeof (descr->member[0]) == 1) \ |
289 | asm volatile ("movb %b0,%%fs:%P1(%q2)" : \ |
290 | : "iq" (value), \ |
291 | "i" (offsetof (struct pthread, member[0])), \ |
292 | "r" (idx)); \ |
293 | else if (sizeof (descr->member[0]) == 4) \ |
294 | asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \ |
295 | : IMM_MODE (value), \ |
296 | "i" (offsetof (struct pthread, member[0])), \ |
297 | "r" (idx)); \ |
298 | else /* 8 */ \ |
299 | { \ |
300 | asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \ |
301 | : IMM_MODE ((uint64_t) cast_to_integer (value)), \ |
302 | "i" (offsetof (struct pthread, member[0])), \ |
303 | "r" (idx)); \ |
304 | }}) |
305 | |
306 | |
307 | /* Set the stack guard field in TCB head. */ |
308 | # define THREAD_SET_STACK_GUARD(value) \ |
309 | THREAD_SETMEM (THREAD_SELF, header.stack_guard, value) |
310 | # define THREAD_COPY_STACK_GUARD(descr) \ |
311 | ((descr)->header.stack_guard \ |
312 | = THREAD_GETMEM (THREAD_SELF, header.stack_guard)) |
313 | |
314 | |
315 | /* Set the pointer guard field in the TCB head. */ |
316 | # define THREAD_SET_POINTER_GUARD(value) \ |
317 | THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value) |
318 | # define THREAD_COPY_POINTER_GUARD(descr) \ |
319 | ((descr)->header.pointer_guard \ |
320 | = THREAD_GETMEM (THREAD_SELF, header.pointer_guard)) |
321 | |
322 | |
323 | /* Get and set the global scope generation counter in the TCB head. */ |
324 | # define THREAD_GSCOPE_IN_TCB 1 |
325 | # define THREAD_GSCOPE_FLAG_UNUSED 0 |
326 | # define THREAD_GSCOPE_FLAG_USED 1 |
327 | # define THREAD_GSCOPE_FLAG_WAIT 2 |
328 | # define THREAD_GSCOPE_RESET_FLAG() \ |
329 | do \ |
330 | { int __res; \ |
331 | asm volatile ("xchgl %0, %%fs:%P1" \ |
332 | : "=r" (__res) \ |
333 | : "i" (offsetof (struct pthread, header.gscope_flag)), \ |
334 | "0" (THREAD_GSCOPE_FLAG_UNUSED)); \ |
335 | if (__res == THREAD_GSCOPE_FLAG_WAIT) \ |
336 | lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ |
337 | } \ |
338 | while (0) |
339 | # define THREAD_GSCOPE_SET_FLAG() \ |
340 | THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED) |
341 | # define THREAD_GSCOPE_WAIT() \ |
342 | GL(dl_wait_lookup_done) () |
343 | |
344 | #endif /* __ASSEMBLER__ */ |
345 | |
346 | #endif /* tls.h */ |
347 | |