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