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