]>
Commit | Line | Data |
---|---|---|
a3931336 | 1 | /* Definition for thread-local data handling. nptl/x86_64 version. |
d614a753 | 2 | Copyright (C) 2002-2020 Free Software Foundation, Inc. |
a3931336 UD |
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/>. */ |
a3931336 UD |
18 | |
19 | #ifndef _TLS_H | |
20 | #define _TLS_H 1 | |
21 | ||
22 | #ifndef __ASSEMBLER__ | |
16a1d952 | 23 | # include <asm/prctl.h> /* For ARCH_SET_FS. */ |
9dcafc55 | 24 | # include <stdbool.h> |
a3931336 UD |
25 | # include <stddef.h> |
26 | # include <stdint.h> | |
33b5d0cc | 27 | # include <stdlib.h> |
df94b641 | 28 | # include <sysdep.h> |
9090848d | 29 | # include <libc-pointer-arith.h> /* For cast_to_integer. */ |
5a8075b1 | 30 | # include <kernel-features.h> |
aca1daef | 31 | # include <dl-dtv.h> |
f62c8abc L |
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; | |
a3931336 UD |
40 | |
41 | ||
a3931336 UD |
42 | typedef struct |
43 | { | |
46d51e9b | 44 | void *tcb; /* Pointer to the TCB. Not necessarily the |
a3931336 UD |
45 | thread descriptor used by libpthread. */ |
46 | dtv_t *dtv; | |
47 | void *self; /* Pointer to the thread descriptor. */ | |
bbde8527 | 48 | int multiple_threads; |
df94b641 | 49 | int gscope_flag; |
35f1e827 UD |
50 | uintptr_t sysinfo; |
51 | uintptr_t stack_guard; | |
827b7087 | 52 | uintptr_t pointer_guard; |
501bdb5d | 53 | unsigned long int unused_vgetcpu_cache[2]; |
ebff9c5c L |
54 | /* Bit 0: X86_FEATURE_1_IBT. |
55 | Bit 1: X86_FEATURE_1_SHSTK. | |
56 | */ | |
57 | unsigned int feature_1; | |
2ec0e7ea | 58 | int __glibc_unused1; |
71bb2639 | 59 | /* Reservation of some values for the TM ABI. */ |
ecbf4342 AJ |
60 | void *__private_tm[4]; |
61 | /* GCC split stack support. */ | |
62 | void *__private_ss; | |
9aa3113a L |
63 | /* The lowest address of shadow stack, */ |
64 | unsigned long long int ssp_base; | |
f3dcae82 L |
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))); | |
b48a267b UD |
68 | |
69 | void *__padding[8]; | |
a3931336 | 70 | } tcbhead_t; |
bbde8527 | 71 | |
0221ce2a L |
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"); | |
9aa3113a L |
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"); | |
0221ce2a L |
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"); | |
9aa3113a L |
85 | _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80, |
86 | "offset of __glibc_unused2 != 0x80"); | |
0221ce2a L |
87 | # endif |
88 | ||
d0369fb8 UD |
89 | #else /* __ASSEMBLER__ */ |
90 | # include <tcb-offsets.h> | |
a3931336 UD |
91 | #endif |
92 | ||
93 | ||
a3931336 UD |
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 | ||
0f34d426 | 102 | #define LOCK_PREFIX "lock;" |
bd4f43b4 | 103 | |
f7d78e18 UD |
104 | /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t), |
105 | because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole | |
106 | struct pthread even when not linked with -lpthread. */ | |
107 | # define TLS_INIT_TCB_SIZE sizeof (struct pthread) | |
a3931336 UD |
108 | |
109 | /* Alignment requirements for the initial TCB. */ | |
f7d78e18 | 110 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) |
a3931336 UD |
111 | |
112 | /* This is the size of the TCB. */ | |
113 | # define TLS_TCB_SIZE sizeof (struct pthread) | |
114 | ||
115 | /* Alignment requirements for the TCB. */ | |
f9068148 | 116 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) |
a3931336 UD |
117 | |
118 | /* The TCB can have any size and the memory following the address the | |
119 | thread pointer points to is unspecified. Allocate the TCB there. */ | |
120 | # define TLS_TCB_AT_TP 1 | |
498a2233 RM |
121 | # define TLS_DTV_AT_TP 0 |
122 | ||
123 | /* Get the thread descriptor definition. */ | |
124 | # include <nptl/descr.h> | |
a3931336 UD |
125 | |
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(descr, dtvp) \ | |
d4f64e1a | 130 | ((tcbhead_t *) (descr))->dtv = (dtvp) + 1 |
a3931336 UD |
131 | |
132 | /* Install new dtv for current thread. */ | |
d4f64e1a | 133 | # define INSTALL_NEW_DTV(dtvp) \ |
a3931336 | 134 | ({ struct pthread *__pd; \ |
55c11fbd | 135 | THREAD_SETMEM (__pd, header.dtv, (dtvp)); }) |
a3931336 UD |
136 | |
137 | /* Return dtv of given thread descriptor. */ | |
138 | # define GET_DTV(descr) \ | |
139 | (((tcbhead_t *) (descr))->dtv) | |
140 | ||
141 | ||
a3931336 UD |
142 | /* Code to initially initialize the thread pointer. This might need |
143 | special attention since 'errno' is not yet available and if the | |
144 | operation can cause a failure 'errno' must not be touched. | |
145 | ||
146 | We have to make the syscall for both uses of the macro since the | |
147 | address might be (and probably is) different. */ | |
774f9285 | 148 | # define TLS_INIT_TP(thrdescr) \ |
a3931336 UD |
149 | ({ void *_thrdescr = (thrdescr); \ |
150 | tcbhead_t *_head = _thrdescr; \ | |
151 | int _result; \ | |
152 | \ | |
153 | _head->tcb = _thrdescr; \ | |
154 | /* For now the thread descriptor is at the same address. */ \ | |
155 | _head->self = _thrdescr; \ | |
156 | \ | |
157 | /* It is a simple syscall to set the %fs value for the thread. */ \ | |
158 | asm volatile ("syscall" \ | |
159 | : "=a" (_result) \ | |
160 | : "0" ((unsigned long int) __NR_arch_prctl), \ | |
161 | "D" ((unsigned long int) ARCH_SET_FS), \ | |
3e976b96 | 162 | "S" (_thrdescr) \ |
a3931336 UD |
163 | : "memory", "cc", "r11", "cx"); \ |
164 | \ | |
fde89ad0 RM |
165 | _result ? "cannot set %fs base address for thread-local storage" : 0; \ |
166 | }) | |
a3931336 | 167 | |
962b744d RM |
168 | # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) |
169 | ||
a3931336 UD |
170 | |
171 | /* Return the address of the dtv for the current thread. */ | |
172 | # define THREAD_DTV() \ | |
173 | ({ struct pthread *__pd; \ | |
55c11fbd | 174 | THREAD_GETMEM (__pd, header.dtv); }) |
a3931336 UD |
175 | |
176 | ||
177 | /* Return the thread descriptor for the current thread. | |
178 | ||
179 | The contained asm must *not* be marked volatile since otherwise | |
180 | assignments like | |
84088310 | 181 | pthread_descr self = thread_self(); |
a3931336 UD |
182 | do not get optimized away. */ |
183 | # define THREAD_SELF \ | |
184 | ({ struct pthread *__self; \ | |
512ec530 | 185 | asm ("mov %%fs:%c1,%0" : "=r" (__self) \ |
55c11fbd | 186 | : "i" (offsetof (struct pthread, header.self))); \ |
a3931336 UD |
187 | __self;}) |
188 | ||
7f08f55a RM |
189 | /* Magic for libthread_db to know how to do THREAD_SELF. */ |
190 | # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */ | |
191 | # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS) | |
a3931336 UD |
192 | |
193 | /* Read member of the thread descriptor directly. */ | |
194 | # define THREAD_GETMEM(descr, member) \ | |
195 | ({ __typeof (descr->member) __value; \ | |
f1bdee61 | 196 | _Static_assert (sizeof (__value) == 1 \ |
fe49a733 FW |
197 | || sizeof (__value) == 4 \ |
198 | || sizeof (__value) == 8, \ | |
199 | "size of per-thread data"); \ | |
a3931336 | 200 | if (sizeof (__value) == 1) \ |
7f293686 UD |
201 | asm volatile ("movb %%fs:%P2,%b0" \ |
202 | : "=q" (__value) \ | |
203 | : "0" (0), "i" (offsetof (struct pthread, member))); \ | |
a3931336 | 204 | else if (sizeof (__value) == 4) \ |
7f293686 UD |
205 | asm volatile ("movl %%fs:%P1,%0" \ |
206 | : "=r" (__value) \ | |
207 | : "i" (offsetof (struct pthread, member))); \ | |
f1bdee61 | 208 | else /* 8 */ \ |
a3931336 | 209 | { \ |
7f293686 UD |
210 | asm volatile ("movq %%fs:%P1,%q0" \ |
211 | : "=r" (__value) \ | |
212 | : "i" (offsetof (struct pthread, member))); \ | |
a3931336 UD |
213 | } \ |
214 | __value; }) | |
215 | ||
216 | ||
217 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ | |
218 | # define THREAD_GETMEM_NC(descr, member, idx) \ | |
219 | ({ __typeof (descr->member[0]) __value; \ | |
f1bdee61 | 220 | _Static_assert (sizeof (__value) == 1 \ |
fe49a733 FW |
221 | || sizeof (__value) == 4 \ |
222 | || sizeof (__value) == 8, \ | |
223 | "size of per-thread data"); \ | |
a3931336 | 224 | if (sizeof (__value) == 1) \ |
7f293686 UD |
225 | asm volatile ("movb %%fs:%P2(%q3),%b0" \ |
226 | : "=q" (__value) \ | |
227 | : "0" (0), "i" (offsetof (struct pthread, member[0])), \ | |
228 | "r" (idx)); \ | |
a3931336 | 229 | else if (sizeof (__value) == 4) \ |
7f293686 UD |
230 | asm volatile ("movl %%fs:%P1(,%q2,4),%0" \ |
231 | : "=r" (__value) \ | |
232 | : "i" (offsetof (struct pthread, member[0])), "r" (idx));\ | |
f1bdee61 | 233 | else /* 8 */ \ |
a3931336 | 234 | { \ |
7f293686 UD |
235 | asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \ |
236 | : "=r" (__value) \ | |
237 | : "i" (offsetof (struct pthread, member[0])), \ | |
238 | "r" (idx)); \ | |
a3931336 UD |
239 | } \ |
240 | __value; }) | |
241 | ||
242 | ||
177d1ad3 UD |
243 | /* Loading addresses of objects on x86-64 needs to be treated special |
244 | when generating PIC code. */ | |
245 | #ifdef __pic__ | |
246 | # define IMM_MODE "nr" | |
247 | #else | |
248 | # define IMM_MODE "ir" | |
249 | #endif | |
250 | ||
251 | ||
4d611e12 | 252 | /* Set member of the thread descriptor directly. */ |
a3931336 | 253 | # define THREAD_SETMEM(descr, member, value) \ |
f1bdee61 ST |
254 | ({ \ |
255 | _Static_assert (sizeof (descr->member) == 1 \ | |
fe49a733 FW |
256 | || sizeof (descr->member) == 4 \ |
257 | || sizeof (descr->member) == 8, \ | |
258 | "size of per-thread data"); \ | |
f1bdee61 | 259 | if (sizeof (descr->member) == 1) \ |
5a03acfe | 260 | asm volatile ("movb %b0,%%fs:%P1" : \ |
a3931336 UD |
261 | : "iq" (value), \ |
262 | "i" (offsetof (struct pthread, member))); \ | |
263 | else if (sizeof (descr->member) == 4) \ | |
264 | asm volatile ("movl %0,%%fs:%P1" : \ | |
177d1ad3 | 265 | : IMM_MODE (value), \ |
a3931336 | 266 | "i" (offsetof (struct pthread, member))); \ |
f1bdee61 | 267 | else /* 8 */ \ |
a3931336 | 268 | { \ |
5a03acfe | 269 | asm volatile ("movq %q0,%%fs:%P1" : \ |
c515fb51 | 270 | : IMM_MODE ((uint64_t) cast_to_integer (value)), \ |
a3931336 UD |
271 | "i" (offsetof (struct pthread, member))); \ |
272 | }}) | |
273 | ||
274 | ||
4d611e12 | 275 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ |
a3931336 | 276 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ |
f1bdee61 ST |
277 | ({ \ |
278 | _Static_assert (sizeof (descr->member[0]) == 1 \ | |
fe49a733 FW |
279 | || sizeof (descr->member[0]) == 4 \ |
280 | || sizeof (descr->member[0]) == 8, \ | |
281 | "size of per-thread data"); \ | |
f1bdee61 | 282 | if (sizeof (descr->member[0]) == 1) \ |
5a03acfe | 283 | asm volatile ("movb %b0,%%fs:%P1(%q2)" : \ |
a3931336 UD |
284 | : "iq" (value), \ |
285 | "i" (offsetof (struct pthread, member[0])), \ | |
286 | "r" (idx)); \ | |
287 | else if (sizeof (descr->member[0]) == 4) \ | |
5a03acfe | 288 | asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \ |
177d1ad3 | 289 | : IMM_MODE (value), \ |
a3931336 UD |
290 | "i" (offsetof (struct pthread, member[0])), \ |
291 | "r" (idx)); \ | |
f1bdee61 | 292 | else /* 8 */ \ |
a3931336 | 293 | { \ |
5a03acfe | 294 | asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \ |
c515fb51 | 295 | : IMM_MODE ((uint64_t) cast_to_integer (value)), \ |
a3931336 UD |
296 | "i" (offsetof (struct pthread, member[0])), \ |
297 | "r" (idx)); \ | |
298 | }}) | |
299 | ||
300 | ||
35f1e827 UD |
301 | /* Set the stack guard field in TCB head. */ |
302 | # define THREAD_SET_STACK_GUARD(value) \ | |
303 | THREAD_SETMEM (THREAD_SELF, header.stack_guard, value) | |
304 | # define THREAD_COPY_STACK_GUARD(descr) \ | |
305 | ((descr)->header.stack_guard \ | |
306 | = THREAD_GETMEM (THREAD_SELF, header.stack_guard)) | |
307 | ||
827b7087 UD |
308 | |
309 | /* Set the pointer guard field in the TCB head. */ | |
b48a267b | 310 | # define THREAD_SET_POINTER_GUARD(value) \ |
827b7087 | 311 | THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value) |
b48a267b | 312 | # define THREAD_COPY_POINTER_GUARD(descr) \ |
827b7087 UD |
313 | ((descr)->header.pointer_guard \ |
314 | = THREAD_GETMEM (THREAD_SELF, header.pointer_guard)) | |
315 | ||
316 | ||
df94b641 | 317 | /* Get and set the global scope generation counter in the TCB head. */ |
a5df0318 | 318 | # define THREAD_GSCOPE_IN_TCB 1 |
b48a267b UD |
319 | # define THREAD_GSCOPE_FLAG_UNUSED 0 |
320 | # define THREAD_GSCOPE_FLAG_USED 1 | |
321 | # define THREAD_GSCOPE_FLAG_WAIT 2 | |
322 | # define THREAD_GSCOPE_RESET_FLAG() \ | |
df94b641 UD |
323 | do \ |
324 | { int __res; \ | |
325 | asm volatile ("xchgl %0, %%fs:%P1" \ | |
326 | : "=r" (__res) \ | |
327 | : "i" (offsetof (struct pthread, header.gscope_flag)), \ | |
328 | "0" (THREAD_GSCOPE_FLAG_UNUSED)); \ | |
329 | if (__res == THREAD_GSCOPE_FLAG_WAIT) \ | |
085a4412 | 330 | lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \ |
df94b641 UD |
331 | } \ |
332 | while (0) | |
b48a267b | 333 | # define THREAD_GSCOPE_SET_FLAG() \ |
df94b641 | 334 | THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED) |
df94b641 | 335 | |
a3931336 UD |
336 | #endif /* __ASSEMBLER__ */ |
337 | ||
338 | #endif /* tls.h */ |