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