]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/nptl/tls.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / i386 / nptl / tls.h
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002-2019 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 #ifndef __ASSEMBLER__
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 typedef struct
34 {
35 void *tcb; /* Pointer to the TCB. Not necessarily the
36 thread descriptor used by libpthread. */
37 dtv_t *dtv;
38 void *self; /* Pointer to the thread descriptor. */
39 int multiple_threads;
40 uintptr_t sysinfo;
41 uintptr_t stack_guard;
42 uintptr_t pointer_guard;
43 int gscope_flag;
44 /* Bit 0: X86_FEATURE_1_IBT.
45 Bit 1: X86_FEATURE_1_SHSTK.
46 */
47 unsigned int feature_1;
48 /* Reservation of some values for the TM ABI. */
49 void *__private_tm[3];
50 /* GCC split stack support. */
51 void *__private_ss;
52 /* The lowest address of shadow stack, */
53 unsigned long ssp_base;
54 } tcbhead_t;
55
56 /* morestack.S in libgcc uses offset 0x30 to access __private_ss, */
57 _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x30,
58 "offset of __private_ss != 0x30");
59
60 # define TLS_MULTIPLE_THREADS_IN_TCB 1
61
62 #else /* __ASSEMBLER__ */
63 # include <tcb-offsets.h>
64 #endif
65
66
67 /* Alignment requirement for the stack. For IA-32 this is governed by
68 the SSE memory functions. */
69 #define STACK_ALIGN 16
70
71 #ifndef __ASSEMBLER__
72 /* Get system call information. */
73 # include <sysdep.h>
74
75 /* The old way: using LDT. */
76
77 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
78 struct user_desc
79 {
80 unsigned int entry_number;
81 unsigned long int base_addr;
82 unsigned int limit;
83 unsigned int seg_32bit:1;
84 unsigned int contents:2;
85 unsigned int read_exec_only:1;
86 unsigned int limit_in_pages:1;
87 unsigned int seg_not_present:1;
88 unsigned int useable:1;
89 unsigned int empty:25;
90 };
91
92 /* Initializing bit fields is slow. We speed it up by using a union. */
93 union user_desc_init
94 {
95 struct user_desc desc;
96 unsigned int vals[4];
97 };
98
99
100 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
101 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
102 struct pthread even when not linked with -lpthread. */
103 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
104
105 /* Alignment requirements for the initial TCB. */
106 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
107
108 /* This is the size of the TCB. */
109 # define TLS_TCB_SIZE sizeof (struct pthread)
110
111 /* Alignment requirements for the TCB. */
112 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
113
114 /* The TCB can have any size and the memory following the address the
115 thread pointer points to is unspecified. Allocate the TCB there. */
116 # define TLS_TCB_AT_TP 1
117 # define TLS_DTV_AT_TP 0
118
119 /* Get the thread descriptor definition. */
120 # include <nptl/descr.h>
121
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(descr, dtvp) \
126 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
127
128 /* Install new dtv for current thread. */
129 # define INSTALL_NEW_DTV(dtvp) \
130 ({ struct pthread *__pd; \
131 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
132
133 /* Return dtv of given thread descriptor. */
134 # define GET_DTV(descr) \
135 (((tcbhead_t *) (descr))->dtv)
136
137 /* Macros to load from and store into segment registers. */
138 # ifndef TLS_GET_GS
139 # define TLS_GET_GS() \
140 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
141 # endif
142 # ifndef TLS_SET_GS
143 # define TLS_SET_GS(val) \
144 __asm ("movw %w0, %%gs" :: "q" (val))
145 # endif
146
147 #ifdef NEED_DL_SYSINFO
148 # define INIT_SYSINFO \
149 _head->sysinfo = GLRO(dl_sysinfo)
150 # define SETUP_THREAD_SYSINFO(pd) \
151 ((pd)->header.sysinfo = THREAD_GETMEM (THREAD_SELF, header.sysinfo))
152 # define CHECK_THREAD_SYSINFO(pd) \
153 assert ((pd)->header.sysinfo == THREAD_GETMEM (THREAD_SELF, header.sysinfo))
154 #else
155 # define INIT_SYSINFO
156 #endif
157
158 #ifndef LOCK_PREFIX
159 # ifdef UP
160 # define LOCK_PREFIX /* nothing */
161 # else
162 # define LOCK_PREFIX "lock;"
163 # endif
164 #endif
165
166 static inline void __attribute__ ((unused, always_inline))
167 tls_fill_user_desc (union user_desc_init *desc,
168 unsigned int entry_number,
169 void *pd)
170 {
171 desc->vals[0] = entry_number;
172 /* The 'base_addr' field. Pointer to the TCB. */
173 desc->vals[1] = (unsigned long int) pd;
174 /* The 'limit' field. We use 4GB which is 0xfffff pages. */
175 desc->vals[2] = 0xfffff;
176 /* Collapsed value of the bitfield:
177 .seg_32bit = 1
178 .contents = 0
179 .read_exec_only = 0
180 .limit_in_pages = 1
181 .seg_not_present = 0
182 .useable = 1 */
183 desc->vals[3] = 0x51;
184 }
185
186 /* Code to initially initialize the thread pointer. This might need
187 special attention since 'errno' is not yet available and if the
188 operation can cause a failure 'errno' must not be touched. */
189 # define TLS_INIT_TP(thrdescr) \
190 ({ void *_thrdescr = (thrdescr); \
191 tcbhead_t *_head = _thrdescr; \
192 union user_desc_init _segdescr; \
193 int _result; \
194 \
195 _head->tcb = _thrdescr; \
196 /* For now the thread descriptor is at the same address. */ \
197 _head->self = _thrdescr; \
198 /* New syscall handling support. */ \
199 INIT_SYSINFO; \
200 \
201 /* Let the kernel pick a value for the 'entry_number' field. */ \
202 tls_fill_user_desc (&_segdescr, -1, _thrdescr); \
203 \
204 /* Install the TLS. */ \
205 INTERNAL_SYSCALL_DECL (err); \
206 _result = INTERNAL_SYSCALL (set_thread_area, err, 1, &_segdescr.desc); \
207 \
208 if (_result == 0) \
209 /* We know the index in the GDT, now load the segment register. \
210 The use of the GDT is described by the value 3 in the lower \
211 three bits of the segment descriptor value. \
212 \
213 Note that we have to do this even if the numeric value of \
214 the descriptor does not change. Loading the segment register \
215 causes the segment information from the GDT to be loaded \
216 which is necessary since we have changed it. */ \
217 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
218 \
219 _result == 0 ? NULL \
220 : "set_thread_area failed when setting up thread-local storage\n"; })
221
222 # define TLS_DEFINE_INIT_TP(tp, pd) \
223 union user_desc_init _segdescr; \
224 /* Find the 'entry_number' field that the kernel selected in TLS_INIT_TP. \
225 The first three bits of the segment register value select the GDT, \
226 ignore them. We get the index from the value of the %gs register in \
227 the current thread. */ \
228 tls_fill_user_desc (&_segdescr, TLS_GET_GS () >> 3, pd); \
229 const struct user_desc *tp = &_segdescr.desc
230
231
232 /* Return the address of the dtv for the current thread. */
233 # define THREAD_DTV() \
234 ({ struct pthread *__pd; \
235 THREAD_GETMEM (__pd, header.dtv); })
236
237
238 /* Return the thread descriptor for the current thread.
239
240 The contained asm must *not* be marked volatile since otherwise
241 assignments like
242 pthread_descr self = thread_self();
243 do not get optimized away. */
244 # define THREAD_SELF \
245 ({ struct pthread *__self; \
246 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
247 : "i" (offsetof (struct pthread, header.self))); \
248 __self;})
249
250 /* Magic for libthread_db to know how to do THREAD_SELF. */
251 # define DB_THREAD_SELF \
252 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
253 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
254
255
256 /* Read member of the thread descriptor directly. */
257 # define THREAD_GETMEM(descr, member) \
258 ({ __typeof (descr->member) __value; \
259 if (sizeof (__value) == 1) \
260 asm volatile ("movb %%gs:%P2,%b0" \
261 : "=q" (__value) \
262 : "0" (0), "i" (offsetof (struct pthread, member))); \
263 else if (sizeof (__value) == 4) \
264 asm volatile ("movl %%gs:%P1,%0" \
265 : "=r" (__value) \
266 : "i" (offsetof (struct pthread, member))); \
267 else \
268 { \
269 if (sizeof (__value) != 8) \
270 /* There should not be any value with a size other than 1, \
271 4 or 8. */ \
272 abort (); \
273 \
274 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
275 "movl %%gs:%P2,%%edx" \
276 : "=A" (__value) \
277 : "i" (offsetof (struct pthread, member)), \
278 "i" (offsetof (struct pthread, member) + 4)); \
279 } \
280 __value; })
281
282
283 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
284 # define THREAD_GETMEM_NC(descr, member, idx) \
285 ({ __typeof (descr->member[0]) __value; \
286 if (sizeof (__value) == 1) \
287 asm volatile ("movb %%gs:%P2(%3),%b0" \
288 : "=q" (__value) \
289 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
290 "r" (idx)); \
291 else if (sizeof (__value) == 4) \
292 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
293 : "=r" (__value) \
294 : "i" (offsetof (struct pthread, member[0])), \
295 "r" (idx)); \
296 else \
297 { \
298 if (sizeof (__value) != 8) \
299 /* There should not be any value with a size other than 1, \
300 4 or 8. */ \
301 abort (); \
302 \
303 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
304 "movl %%gs:4+%P1(,%2,8),%%edx" \
305 : "=&A" (__value) \
306 : "i" (offsetof (struct pthread, member[0])), \
307 "r" (idx)); \
308 } \
309 __value; })
310
311
312
313 /* Set member of the thread descriptor directly. */
314 # define THREAD_SETMEM(descr, member, value) \
315 ({ if (sizeof (descr->member) == 1) \
316 asm volatile ("movb %b0,%%gs:%P1" : \
317 : "iq" (value), \
318 "i" (offsetof (struct pthread, member))); \
319 else if (sizeof (descr->member) == 4) \
320 asm volatile ("movl %0,%%gs:%P1" : \
321 : "ir" (value), \
322 "i" (offsetof (struct pthread, member))); \
323 else \
324 { \
325 if (sizeof (descr->member) != 8) \
326 /* There should not be any value with a size other than 1, \
327 4 or 8. */ \
328 abort (); \
329 \
330 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
331 "movl %%edx,%%gs:%P2" : \
332 : "A" ((uint64_t) cast_to_integer (value)), \
333 "i" (offsetof (struct pthread, member)), \
334 "i" (offsetof (struct pthread, member) + 4)); \
335 }})
336
337
338 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
339 # define THREAD_SETMEM_NC(descr, member, idx, value) \
340 ({ if (sizeof (descr->member[0]) == 1) \
341 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
342 : "iq" (value), \
343 "i" (offsetof (struct pthread, member)), \
344 "r" (idx)); \
345 else if (sizeof (descr->member[0]) == 4) \
346 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
347 : "ir" (value), \
348 "i" (offsetof (struct pthread, member)), \
349 "r" (idx)); \
350 else \
351 { \
352 if (sizeof (descr->member[0]) != 8) \
353 /* There should not be any value with a size other than 1, \
354 4 or 8. */ \
355 abort (); \
356 \
357 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
358 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
359 : "A" ((uint64_t) cast_to_integer (value)), \
360 "i" (offsetof (struct pthread, member)), \
361 "r" (idx)); \
362 }})
363
364
365 /* Atomic compare and exchange on TLS, returning old value. */
366 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
367 ({ __typeof (descr->member) __ret; \
368 __typeof (oldval) __old = (oldval); \
369 if (sizeof (descr->member) == 4) \
370 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
371 : "=a" (__ret) \
372 : "0" (__old), "r" (newval), \
373 "i" (offsetof (struct pthread, member))); \
374 else \
375 /* Not necessary for other sizes in the moment. */ \
376 abort (); \
377 __ret; })
378
379
380 /* Atomic logical and. */
381 #define THREAD_ATOMIC_AND(descr, member, val) \
382 (void) ({ if (sizeof ((descr)->member) == 4) \
383 asm volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
384 :: "i" (offsetof (struct pthread, member)), \
385 "ir" (val)); \
386 else \
387 /* Not necessary for other sizes in the moment. */ \
388 abort (); })
389
390
391 /* Atomic set bit. */
392 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
393 (void) ({ if (sizeof ((descr)->member) == 4) \
394 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
395 :: "i" (offsetof (struct pthread, member)), \
396 "ir" (1 << (bit))); \
397 else \
398 /* Not necessary for other sizes in the moment. */ \
399 abort (); })
400
401
402 /* Set the stack guard field in TCB head. */
403 #define THREAD_SET_STACK_GUARD(value) \
404 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
405 #define THREAD_COPY_STACK_GUARD(descr) \
406 ((descr)->header.stack_guard \
407 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
408
409
410 /* Set the pointer guard field in the TCB head. */
411 #define THREAD_SET_POINTER_GUARD(value) \
412 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
413 #define THREAD_COPY_POINTER_GUARD(descr) \
414 ((descr)->header.pointer_guard \
415 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
416
417
418 /* Get and set the global scope generation counter in the TCB head. */
419 #define THREAD_GSCOPE_IN_TCB 1
420 #define THREAD_GSCOPE_FLAG_UNUSED 0
421 #define THREAD_GSCOPE_FLAG_USED 1
422 #define THREAD_GSCOPE_FLAG_WAIT 2
423 #define THREAD_GSCOPE_RESET_FLAG() \
424 do \
425 { int __res; \
426 asm volatile ("xchgl %0, %%gs:%P1" \
427 : "=r" (__res) \
428 : "i" (offsetof (struct pthread, header.gscope_flag)), \
429 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
430 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
431 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
432 } \
433 while (0)
434 #define THREAD_GSCOPE_SET_FLAG() \
435 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
436 #define THREAD_GSCOPE_WAIT() \
437 GL(dl_wait_lookup_done) ()
438
439 #endif /* __ASSEMBLER__ */
440
441 #endif /* tls.h */