1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002-2015 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/>. */
22 #include <dl-sysdep.h>
29 # include <libc-internal.h>
30 # include <kernel-features.h>
33 /* Type for the dtv. */
47 void *tcb
; /* Pointer to the TCB. Not necessarily the
48 thread descriptor used by libpthread. */
50 void *self
; /* Pointer to the thread descriptor. */
53 uintptr_t stack_guard
;
54 uintptr_t pointer_guard
;
56 #ifndef __ASSUME_PRIVATE_FUTEX
59 int __glibc_reserved1
;
61 /* Reservation of some values for the TM ABI. */
62 void *__private_tm
[4];
63 /* GCC split stack support. */
67 # define TLS_MULTIPLE_THREADS_IN_TCB 1
69 #else /* __ASSEMBLER__ */
70 # include <tcb-offsets.h>
74 /* Alignment requirement for the stack. For IA-32 this is governed by
75 the SSE memory functions. */
76 #define STACK_ALIGN 16
79 /* Get system call information. */
82 /* The old way: using LDT. */
84 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
87 unsigned int entry_number
;
88 unsigned long int base_addr
;
90 unsigned int seg_32bit
:1;
91 unsigned int contents
:2;
92 unsigned int read_exec_only
:1;
93 unsigned int limit_in_pages
:1;
94 unsigned int seg_not_present
:1;
95 unsigned int useable
:1;
96 unsigned int empty
:25;
99 /* Initializing bit fields is slow. We speed it up by using a union. */
102 struct user_desc desc
;
103 unsigned int vals
[4];
107 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
108 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
109 struct pthread even when not linked with -lpthread. */
110 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
112 /* Alignment requirements for the initial TCB. */
113 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
115 /* This is the size of the TCB. */
116 # define TLS_TCB_SIZE sizeof (struct pthread)
118 /* Alignment requirements for the TCB. */
119 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
121 /* The TCB can have any size and the memory following the address the
122 thread pointer points to is unspecified. Allocate the TCB there. */
123 # define TLS_TCB_AT_TP 1
124 # define TLS_DTV_AT_TP 0
126 /* Get the thread descriptor definition. */
127 # include <nptl/descr.h>
130 /* Install the dtv pointer. The pointer passed is to the element with
131 index -1 which contain the length. */
132 # define INSTALL_DTV(descr, dtvp) \
133 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
135 /* Install new dtv for current thread. */
136 # define INSTALL_NEW_DTV(dtvp) \
137 ({ struct pthread *__pd; \
138 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
140 /* Return dtv of given thread descriptor. */
141 # define GET_DTV(descr) \
142 (((tcbhead_t *) (descr))->dtv)
144 /* Macros to load from and store into segment registers. */
146 # define TLS_GET_GS() \
147 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
150 # define TLS_SET_GS(val) \
151 __asm ("movw %w0, %%gs" :: "q" (val))
154 #ifdef NEED_DL_SYSINFO
155 # define INIT_SYSINFO \
156 _head->sysinfo = GLRO(dl_sysinfo)
157 # define SETUP_THREAD_SYSINFO(pd) \
158 ((pd)->header.sysinfo = THREAD_GETMEM (THREAD_SELF, header.sysinfo))
159 # define CHECK_THREAD_SYSINFO(pd) \
160 assert ((pd)->header.sysinfo == THREAD_GETMEM (THREAD_SELF, header.sysinfo))
162 # define INIT_SYSINFO
167 # define LOCK_PREFIX /* nothing */
169 # define LOCK_PREFIX "lock;"
173 static inline void __attribute__ ((unused
, always_inline
))
174 tls_fill_user_desc (union user_desc_init
*desc
,
175 unsigned int entry_number
,
178 desc
->vals
[0] = entry_number
;
179 /* The 'base_addr' field. Pointer to the TCB. */
180 desc
->vals
[1] = (unsigned long int) pd
;
181 /* The 'limit' field. We use 4GB which is 0xfffff pages. */
182 desc
->vals
[2] = 0xfffff;
183 /* Collapsed value of the bitfield:
190 desc
->vals
[3] = 0x51;
193 /* Code to initially initialize the thread pointer. This might need
194 special attention since 'errno' is not yet available and if the
195 operation can cause a failure 'errno' must not be touched. */
196 # define TLS_INIT_TP(thrdescr) \
197 ({ void *_thrdescr = (thrdescr); \
198 tcbhead_t *_head = _thrdescr; \
199 union user_desc_init _segdescr; \
202 _head->tcb = _thrdescr; \
203 /* For now the thread descriptor is at the same address. */ \
204 _head->self = _thrdescr; \
205 /* New syscall handling support. */ \
208 /* Let the kernel pick a value for the 'entry_number' field. */ \
209 tls_fill_user_desc (&_segdescr, -1, _thrdescr); \
211 /* Install the TLS. */ \
212 INTERNAL_SYSCALL_DECL (err); \
213 _result = INTERNAL_SYSCALL (set_thread_area, err, 1, &_segdescr.desc); \
216 /* We know the index in the GDT, now load the segment register. \
217 The use of the GDT is described by the value 3 in the lower \
218 three bits of the segment descriptor value. \
220 Note that we have to do this even if the numeric value of \
221 the descriptor does not change. Loading the segment register \
222 causes the segment information from the GDT to be loaded \
223 which is necessary since we have changed it. */ \
224 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
226 _result == 0 ? NULL \
227 : "set_thread_area failed when setting up thread-local storage\n"; })
229 # define TLS_DEFINE_INIT_TP(tp, pd) \
230 union user_desc_init _segdescr; \
231 /* Find the 'entry_number' field that the kernel selected in TLS_INIT_TP. \
232 The first three bits of the segment register value select the GDT, \
233 ignore them. We get the index from the value of the %gs register in \
234 the current thread. */ \
235 tls_fill_user_desc (&_segdescr, TLS_GET_GS () >> 3, pd); \
236 const struct user_desc *tp = &_segdescr.desc
239 /* Return the address of the dtv for the current thread. */
240 # define THREAD_DTV() \
241 ({ struct pthread *__pd; \
242 THREAD_GETMEM (__pd, header.dtv); })
245 /* Return the thread descriptor for the current thread.
247 The contained asm must *not* be marked volatile since otherwise
249 pthread_descr self = thread_self();
250 do not get optimized away. */
251 # define THREAD_SELF \
252 ({ struct pthread *__self; \
253 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
254 : "i" (offsetof (struct pthread, header.self))); \
257 /* Magic for libthread_db to know how to do THREAD_SELF. */
258 # define DB_THREAD_SELF \
259 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
260 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
263 /* Read member of the thread descriptor directly. */
264 # define THREAD_GETMEM(descr, member) \
265 ({ __typeof (descr->member) __value; \
266 if (sizeof (__value) == 1) \
267 asm volatile ("movb %%gs:%P2,%b0" \
269 : "0" (0), "i" (offsetof (struct pthread, member))); \
270 else if (sizeof (__value) == 4) \
271 asm volatile ("movl %%gs:%P1,%0" \
273 : "i" (offsetof (struct pthread, member))); \
276 if (sizeof (__value) != 8) \
277 /* There should not be any value with a size other than 1, \
281 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
282 "movl %%gs:%P2,%%edx" \
284 : "i" (offsetof (struct pthread, member)), \
285 "i" (offsetof (struct pthread, member) + 4)); \
290 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
291 # define THREAD_GETMEM_NC(descr, member, idx) \
292 ({ __typeof (descr->member[0]) __value; \
293 if (sizeof (__value) == 1) \
294 asm volatile ("movb %%gs:%P2(%3),%b0" \
296 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
298 else if (sizeof (__value) == 4) \
299 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
301 : "i" (offsetof (struct pthread, member[0])), \
305 if (sizeof (__value) != 8) \
306 /* There should not be any value with a size other than 1, \
310 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
311 "movl %%gs:4+%P1(,%2,8),%%edx" \
313 : "i" (offsetof (struct pthread, member[0])), \
319 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
320 # define THREAD_SETMEM(descr, member, value) \
321 ({ if (sizeof (descr->member) == 1) \
322 asm volatile ("movb %b0,%%gs:%P1" : \
324 "i" (offsetof (struct pthread, member))); \
325 else if (sizeof (descr->member) == 4) \
326 asm volatile ("movl %0,%%gs:%P1" : \
328 "i" (offsetof (struct pthread, member))); \
331 if (sizeof (descr->member) != 8) \
332 /* There should not be any value with a size other than 1, \
336 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
337 "movl %%edx,%%gs:%P2" : \
338 : "A" ((uint64_t) cast_to_integer (value)), \
339 "i" (offsetof (struct pthread, member)), \
340 "i" (offsetof (struct pthread, member) + 4)); \
344 /* Set member of the thread descriptor directly. */
345 # define THREAD_SETMEM_NC(descr, member, idx, value) \
346 ({ if (sizeof (descr->member[0]) == 1) \
347 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
349 "i" (offsetof (struct pthread, member)), \
351 else if (sizeof (descr->member[0]) == 4) \
352 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
354 "i" (offsetof (struct pthread, member)), \
358 if (sizeof (descr->member[0]) != 8) \
359 /* There should not be any value with a size other than 1, \
363 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
364 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
365 : "A" ((uint64_t) cast_to_integer (value)), \
366 "i" (offsetof (struct pthread, member)), \
371 /* Atomic compare and exchange on TLS, returning old value. */
372 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
373 ({ __typeof (descr->member) __ret; \
374 __typeof (oldval) __old = (oldval); \
375 if (sizeof (descr->member) == 4) \
376 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
378 : "0" (__old), "r" (newval), \
379 "i" (offsetof (struct pthread, member))); \
381 /* Not necessary for other sizes in the moment. */ \
386 /* Atomic logical and. */
387 #define THREAD_ATOMIC_AND(descr, member, val) \
388 (void) ({ if (sizeof ((descr)->member) == 4) \
389 asm volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
390 :: "i" (offsetof (struct pthread, member)), \
393 /* Not necessary for other sizes in the moment. */ \
397 /* Atomic set bit. */
398 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
399 (void) ({ if (sizeof ((descr)->member) == 4) \
400 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
401 :: "i" (offsetof (struct pthread, member)), \
402 "ir" (1 << (bit))); \
404 /* Not necessary for other sizes in the moment. */ \
408 /* Call the user-provided thread function. */
409 #define CALL_THREAD_FCT(descr) \
411 int __ignore1, __ignore2; \
412 asm volatile ("pushl %%eax\n\t" \
415 "pushl %%gs:%P4\n\t" \
416 "call *%%gs:%P3\n\t" \
418 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
419 : "i" (offsetof (struct pthread, start_routine)), \
420 "i" (offsetof (struct pthread, arg))); \
424 /* Set the stack guard field in TCB head. */
425 #define THREAD_SET_STACK_GUARD(value) \
426 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
427 #define THREAD_COPY_STACK_GUARD(descr) \
428 ((descr)->header.stack_guard \
429 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
432 /* Set the pointer guard field in the TCB head. */
433 #define THREAD_SET_POINTER_GUARD(value) \
434 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
435 #define THREAD_COPY_POINTER_GUARD(descr) \
436 ((descr)->header.pointer_guard \
437 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
440 /* Get and set the global scope generation counter in the TCB head. */
441 #define THREAD_GSCOPE_FLAG_UNUSED 0
442 #define THREAD_GSCOPE_FLAG_USED 1
443 #define THREAD_GSCOPE_FLAG_WAIT 2
444 #define THREAD_GSCOPE_RESET_FLAG() \
447 asm volatile ("xchgl %0, %%gs:%P1" \
449 : "i" (offsetof (struct pthread, header.gscope_flag)), \
450 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
451 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
452 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
455 #define THREAD_GSCOPE_SET_FLAG() \
456 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
457 #define THREAD_GSCOPE_WAIT() \
458 GL(dl_wait_lookup_done) ()
460 #endif /* __ASSEMBLER__ */