1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002-2007, 2009 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/>. */
29 # include <bits/kernel-features.h>
32 /* Type for the dtv. */
46 void *tcb; /* Pointer to the TCB. Not necessarily the
47 thread descriptor used by libpthread. */
49 void *self; /* Pointer to the thread descriptor. */
52 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
55 #ifndef __ASSUME_PRIVATE_FUTEX
60 /* Reservation of some values for the TM ABI. */
61 void *__private_tm[5];
64 # define TLS_MULTIPLE_THREADS_IN_TCB 1
66 #else /* __ASSEMBLER__ */
67 # include <tcb-offsets.h>
71 /* We require TLS support in the tools. */
72 #define HAVE_TLS_SUPPORT
73 #define HAVE___THREAD 1
74 #define HAVE_TLS_MODEL_ATTRIBUTE 1
76 /* Signal that TLS support is available. */
80 /* Alignment requirement for the stack. For IA-32 this is governed by
81 the SSE memory functions. */
82 #define STACK_ALIGN 16
85 /* Get system call information. */
88 /* The old way: using LDT. */
90 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
93 unsigned int entry_number;
94 unsigned long int base_addr;
96 unsigned int seg_32bit:1;
97 unsigned int contents:2;
98 unsigned int read_exec_only:1;
99 unsigned int limit_in_pages:1;
100 unsigned int seg_not_present:1;
101 unsigned int useable:1;
102 unsigned int empty:25;
105 /* Initializing bit fields is slow. We speed it up by using a union. */
108 struct user_desc desc;
109 unsigned int vals[4];
113 /* Get the thread descriptor definition. */
116 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
117 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
118 struct pthread even when not linked with -lpthread. */
119 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
121 /* Alignment requirements for the initial TCB. */
122 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
124 /* This is the size of the TCB. */
125 # define TLS_TCB_SIZE sizeof (struct pthread)
127 /* Alignment requirements for the TCB. */
128 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
130 /* The TCB can have any size and the memory following the address the
131 thread pointer points to is unspecified. Allocate the TCB there. */
132 # define TLS_TCB_AT_TP 1
135 /* Install the dtv pointer. The pointer passed is to the element with
136 index -1 which contain the length. */
137 # define INSTALL_DTV(descr, dtvp) \
138 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
140 /* Install new dtv for current thread. */
141 # define INSTALL_NEW_DTV(dtvp) \
142 ({ struct pthread *__pd; \
143 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
145 /* Return dtv of given thread descriptor. */
146 # define GET_DTV(descr) \
147 (((tcbhead_t *) (descr))->dtv)
149 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
150 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
152 /* Macros to load from and store into segment registers. */
154 # define TLS_GET_GS() \
155 ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
158 # define TLS_SET_GS(val) \
159 __asm__ ("movw %w0, %%gs" :: "q" (val))
163 # ifndef __NR_set_thread_area
164 # define __NR_set_thread_area 243
166 # ifndef TLS_FLAG_WRITABLE
167 # define TLS_FLAG_WRITABLE 0x00000001
170 // XXX Enable for the real world.
172 # ifndef __ASSUME_SET_THREAD_AREA
173 # error "we need set_thread_area"
178 # define TLS_EBX_ARG "r"
179 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
181 # define TLS_EBX_ARG "b"
182 # define TLS_LOAD_EBX
185 #if defined NEED_DL_SYSINFO
186 # define INIT_SYSINFO \
187 _head->sysinfo = GLRO(dl_sysinfo)
189 # define INIT_SYSINFO
194 # define LOCK_PREFIX /* nothing */
196 # define LOCK_PREFIX "lock;"
200 /* Code to initially initialize the thread pointer. This might need
201 special attention since 'errno' is not yet available and if the
202 operation can cause a failure 'errno' must not be touched. */
203 # define TLS_INIT_TP(thrdescr, secondcall) \
204 ({ void *_thrdescr = (thrdescr); \
205 tcbhead_t *_head = _thrdescr; \
206 union user_desc_init _segdescr; \
209 _head->tcb = _thrdescr; \
210 /* For now the thread descriptor is at the same address. */ \
211 _head->self = _thrdescr; \
212 /* New syscall handling support. */ \
215 /* The 'entry_number' field. Let the kernel pick a value. */ \
217 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
219 _segdescr.vals[0] = -1; \
220 /* The 'base_addr' field. Pointer to the TCB. */ \
221 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
222 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
223 _segdescr.vals[2] = 0xfffff; \
224 /* Collapsed value of the bitfield: \
227 .read_exec_only = 0 \
228 .limit_in_pages = 1 \
229 .seg_not_present = 0 \
231 _segdescr.vals[3] = 0x51; \
233 /* Install the TLS. */ \
234 __asm__ __volatile__ (TLS_LOAD_EBX \
237 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
238 : "0" (__NR_set_thread_area), \
239 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
242 /* We know the index in the GDT, now load the segment register. \
243 The use of the GDT is described by the value 3 in the lower \
244 three bits of the segment descriptor value. \
246 Note that we have to do this even if the numeric value of \
247 the descriptor does not change. Loading the segment register \
248 causes the segment information from the GDT to be loaded \
249 which is necessary since we have changed it. */ \
250 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
252 _result == 0 ? NULL \
253 : "set_thread_area failed when setting up thread-local storage\n"; })
256 /* Return the address of the dtv for the current thread. */
257 # define THREAD_DTV() \
258 ({ struct pthread *__pd; \
259 THREAD_GETMEM (__pd, header.dtv); })
262 /* Return the thread descriptor for the current thread.
264 The contained asm must *not* be marked __volatile__ since otherwise
266 pthread_descr self = thread_self();
267 do not get optimized away. */
268 # define THREAD_SELF \
269 ({ struct pthread *__self; \
270 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
271 : "i" (offsetof (struct pthread, header.self))); \
274 /* Magic for libthread_db to know how to do THREAD_SELF. */
275 # define DB_THREAD_SELF \
276 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
277 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
280 /* Read member of the thread descriptor directly. */
281 # define THREAD_GETMEM(descr, member) \
282 ({ __typeof (descr->member) __value; \
283 if (sizeof (__value) == 1) \
284 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
286 : "0" (0), "i" (offsetof (struct pthread, member))); \
287 else if (sizeof (__value) == 4) \
288 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
290 : "i" (offsetof (struct pthread, member))); \
293 if (sizeof (__value) != 8) \
294 /* There should not be any value with a size other than 1, \
298 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
299 "movl %%gs:%P2,%%edx" \
301 : "i" (offsetof (struct pthread, member)), \
302 "i" (offsetof (struct pthread, member) + 4)); \
307 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
308 # define THREAD_GETMEM_NC(descr, member, idx) \
309 ({ __typeof (descr->member[0]) __value; \
310 if (sizeof (__value) == 1) \
311 __asm__ __volatile__ ("movb %%gs:%P2(%3),%b0" \
313 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
315 else if (sizeof (__value) == 4) \
316 __asm__ __volatile__ ("movl %%gs:%P1(,%2,4),%0" \
318 : "i" (offsetof (struct pthread, member[0])), \
322 if (sizeof (__value) != 8) \
323 /* There should not be any value with a size other than 1, \
327 __asm__ __volatile__ ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
328 "movl %%gs:4+%P1(,%2,8),%%edx" \
330 : "i" (offsetof (struct pthread, member[0])), \
336 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
337 # define THREAD_SETMEM(descr, member, value) \
338 ({ if (sizeof (descr->member) == 1) \
339 __asm__ __volatile__ ("movb %b0,%%gs:%P1" : \
341 "i" (offsetof (struct pthread, member))); \
342 else if (sizeof (descr->member) == 4) \
343 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
345 "i" (offsetof (struct pthread, member))); \
348 if (sizeof (descr->member) != 8) \
349 /* There should not be any value with a size other than 1, \
353 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\t" \
354 "movl %%edx,%%gs:%P2" : \
356 "i" (offsetof (struct pthread, member)), \
357 "i" (offsetof (struct pthread, member) + 4)); \
361 /* Set member of the thread descriptor directly. */
362 # define THREAD_SETMEM_NC(descr, member, idx, value) \
363 ({ if (sizeof (descr->member[0]) == 1) \
364 __asm__ __volatile__ ("movb %b0,%%gs:%P1(%2)" : \
366 "i" (offsetof (struct pthread, member)), \
368 else if (sizeof (descr->member[0]) == 4) \
369 __asm__ __volatile__ ("movl %0,%%gs:%P1(,%2,4)" : \
371 "i" (offsetof (struct pthread, member)), \
375 if (sizeof (descr->member[0]) != 8) \
376 /* There should not be any value with a size other than 1, \
380 __asm__ __volatile__ ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
381 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
383 "i" (offsetof (struct pthread, member)), \
388 /* Atomic compare and exchange on TLS, returning old value. */
389 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
390 ({ __typeof (descr->member) __ret; \
391 __typeof (oldval) __old = (oldval); \
392 if (sizeof (descr->member) == 4) \
393 __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
395 : "0" (__old), "r" (newval), \
396 "i" (offsetof (struct pthread, member))); \
398 /* Not necessary for other sizes in the moment. */ \
403 /* Atomic logical and. */
404 #define THREAD_ATOMIC_AND(descr, member, val) \
405 (void) ({ if (sizeof ((descr)->member) == 4) \
406 __asm__ __volatile__ (LOCK_PREFIX "andl %1, %%gs:%P0" \
407 :: "i" (offsetof (struct pthread, member)), \
410 /* Not necessary for other sizes in the moment. */ \
414 /* Atomic set bit. */
415 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
416 (void) ({ if (sizeof ((descr)->member) == 4) \
417 __asm__ __volatile__ (LOCK_PREFIX "orl %1, %%gs:%P0" \
418 :: "i" (offsetof (struct pthread, member)), \
419 "ir" (1 << (bit))); \
421 /* Not necessary for other sizes in the moment. */ \
425 /* Call the user-provided thread function. */
426 #define CALL_THREAD_FCT(descr) \
428 int __ignore1, __ignore2; \
429 __asm__ __volatile__ ("pushl %%eax\n\t" \
432 "pushl %%gs:%P4\n\t" \
433 "call *%%gs:%P3\n\t" \
435 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
436 : "i" (offsetof (struct pthread, start_routine)), \
437 "i" (offsetof (struct pthread, arg))); \
441 /* Set the stack guard field in TCB head. */
442 #define THREAD_SET_STACK_GUARD(value) \
443 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
444 #define THREAD_COPY_STACK_GUARD(descr) \
445 ((descr)->header.stack_guard \
446 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
449 /* Set the pointer guard field in the TCB head. */
450 #define THREAD_SET_POINTER_GUARD(value) \
451 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
452 #define THREAD_COPY_POINTER_GUARD(descr) \
453 ((descr)->header.pointer_guard \
454 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
457 /* Get and set the global scope generation counter in the TCB head. */
458 #define THREAD_GSCOPE_FLAG_UNUSED 0
459 #define THREAD_GSCOPE_FLAG_USED 1
460 #define THREAD_GSCOPE_FLAG_WAIT 2
461 #define THREAD_GSCOPE_RESET_FLAG() \
464 __asm__ __volatile__ ("xchgl %0, %%gs:%P1" \
466 : "i" (offsetof (struct pthread, header.gscope_flag)), \
467 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
468 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
469 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
472 #define THREAD_GSCOPE_SET_FLAG() \
473 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
474 #define THREAD_GSCOPE_WAIT() \
475 GL(dl_wait_lookup_done) ()
477 #endif /* __ASSEMBLER__ */