1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002,2003,2004,2005,2006,2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <dl-sysdep.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
;
58 # define TLS_MULTIPLE_THREADS_IN_TCB 1
60 #else /* __ASSEMBLER__ */
61 # include <tcb-offsets.h>
65 /* We require TLS support in the tools. */
66 #ifndef HAVE_TLS_SUPPORT
67 # error "TLS support is required."
70 /* Alignment requirement for the stack. For IA-32 this is governed by
71 the SSE memory functions. */
72 #define STACK_ALIGN 16
75 /* Get system call information. */
78 /* The old way: using LDT. */
80 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
83 unsigned int entry_number
;
84 unsigned long int base_addr
;
86 unsigned int seg_32bit
:1;
87 unsigned int contents
:2;
88 unsigned int read_exec_only
:1;
89 unsigned int limit_in_pages
:1;
90 unsigned int seg_not_present
:1;
91 unsigned int useable
:1;
92 unsigned int empty
:25;
95 /* Initializing bit fields is slow. We speed it up by using a union. */
98 struct user_desc desc
;
103 /* Get the thread descriptor definition. */
104 # include <nptl/descr.h>
106 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
107 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
108 struct pthread even when not linked with -lpthread. */
109 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
111 /* Alignment requirements for the initial TCB. */
112 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
114 /* This is the size of the TCB. */
115 # define TLS_TCB_SIZE sizeof (struct pthread)
117 /* Alignment requirements for the TCB. */
118 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
120 /* The TCB can have any size and the memory following the address the
121 thread pointer points to is unspecified. Allocate the TCB there. */
122 # define TLS_TCB_AT_TP 1
125 /* Install the dtv pointer. The pointer passed is to the element with
126 index -1 which contain the length. */
127 # define INSTALL_DTV(descr, dtvp) \
128 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
130 /* Install new dtv for current thread. */
131 # define INSTALL_NEW_DTV(dtvp) \
132 ({ struct pthread *__pd; \
133 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
135 /* Return dtv of given thread descriptor. */
136 # define GET_DTV(descr) \
137 (((tcbhead_t *) (descr))->dtv)
139 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
140 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
142 /* Macros to load from and store into segment registers. */
144 # define TLS_GET_GS() \
145 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
148 # define TLS_SET_GS(val) \
149 __asm ("movw %w0, %%gs" :: "q" (val))
153 # ifndef __NR_set_thread_area
154 # define __NR_set_thread_area 243
156 # ifndef TLS_FLAG_WRITABLE
157 # define TLS_FLAG_WRITABLE 0x00000001
160 // XXX Enable for the real world.
162 # ifndef __ASSUME_SET_THREAD_AREA
163 # error "we need set_thread_area"
168 # define TLS_EBX_ARG "r"
169 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
171 # define TLS_EBX_ARG "b"
172 # define TLS_LOAD_EBX
175 #if defined NEED_DL_SYSINFO
176 # define INIT_SYSINFO \
177 _head->sysinfo = GLRO(dl_sysinfo)
179 # define INIT_SYSINFO
184 # define LOCK_PREFIX /* nothing */
186 # define LOCK_PREFIX "lock;"
190 /* Code to initially initialize the thread pointer. This might need
191 special attention since 'errno' is not yet available and if the
192 operation can cause a failure 'errno' must not be touched. */
193 # define TLS_INIT_TP(thrdescr, secondcall) \
194 ({ void *_thrdescr = (thrdescr); \
195 tcbhead_t *_head = _thrdescr; \
196 union user_desc_init _segdescr; \
199 _head->tcb = _thrdescr; \
200 /* For now the thread descriptor is at the same address. */ \
201 _head->self = _thrdescr; \
202 /* New syscall handling support. */ \
205 /* The 'entry_number' field. Let the kernel pick a value. */ \
207 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
209 _segdescr.vals[0] = -1; \
210 /* The 'base_addr' field. Pointer to the TCB. */ \
211 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
212 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
213 _segdescr.vals[2] = 0xfffff; \
214 /* Collapsed value of the bitfield: \
217 .read_exec_only = 0 \
218 .limit_in_pages = 1 \
219 .seg_not_present = 0 \
221 _segdescr.vals[3] = 0x51; \
223 /* Install the TLS. */ \
224 asm volatile (TLS_LOAD_EBX \
227 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
228 : "0" (__NR_set_thread_area), \
229 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
232 /* We know the index in the GDT, now load the segment register. \
233 The use of the GDT is described by the value 3 in the lower \
234 three bits of the segment descriptor value. \
236 Note that we have to do this even if the numeric value of \
237 the descriptor does not change. Loading the segment register \
238 causes the segment information from the GDT to be loaded \
239 which is necessary since we have changed it. */ \
240 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
242 _result == 0 ? NULL \
243 : "set_thread_area failed when setting up thread-local storage\n"; })
246 /* Return the address of the dtv for the current thread. */
247 # define THREAD_DTV() \
248 ({ struct pthread *__pd; \
249 THREAD_GETMEM (__pd, header.dtv); })
252 /* Return the thread descriptor for the current thread.
254 The contained asm must *not* be marked volatile since otherwise
256 pthread_descr self = thread_self();
257 do not get optimized away. */
258 # define THREAD_SELF \
259 ({ struct pthread *__self; \
260 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
261 : "i" (offsetof (struct pthread, header.self))); \
264 /* Magic for libthread_db to know how to do THREAD_SELF. */
265 # define DB_THREAD_SELF \
266 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
267 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
270 /* Read member of the thread descriptor directly. */
271 # define THREAD_GETMEM(descr, member) \
272 ({ __typeof (descr->member) __value; \
273 if (sizeof (__value) == 1) \
274 asm volatile ("movb %%gs:%P2,%b0" \
276 : "0" (0), "i" (offsetof (struct pthread, member))); \
277 else if (sizeof (__value) == 4) \
278 asm volatile ("movl %%gs:%P1,%0" \
280 : "i" (offsetof (struct pthread, member))); \
283 if (sizeof (__value) != 8) \
284 /* There should not be any value with a size other than 1, \
288 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
289 "movl %%gs:%P2,%%edx" \
291 : "i" (offsetof (struct pthread, member)), \
292 "i" (offsetof (struct pthread, member) + 4)); \
297 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
298 # define THREAD_GETMEM_NC(descr, member, idx) \
299 ({ __typeof (descr->member[0]) __value; \
300 if (sizeof (__value) == 1) \
301 asm volatile ("movb %%gs:%P2(%3),%b0" \
303 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
305 else if (sizeof (__value) == 4) \
306 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
308 : "i" (offsetof (struct pthread, member[0])), \
312 if (sizeof (__value) != 8) \
313 /* There should not be any value with a size other than 1, \
317 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
318 "movl %%gs:4+%P1(,%2,8),%%edx" \
320 : "i" (offsetof (struct pthread, member[0])), \
326 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
327 # define THREAD_SETMEM(descr, member, value) \
328 ({ if (sizeof (descr->member) == 1) \
329 asm volatile ("movb %b0,%%gs:%P1" : \
331 "i" (offsetof (struct pthread, member))); \
332 else if (sizeof (descr->member) == 4) \
333 asm volatile ("movl %0,%%gs:%P1" : \
335 "i" (offsetof (struct pthread, member))); \
338 if (sizeof (descr->member) != 8) \
339 /* There should not be any value with a size other than 1, \
343 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
344 "movl %%edx,%%gs:%P2" : \
346 "i" (offsetof (struct pthread, member)), \
347 "i" (offsetof (struct pthread, member) + 4)); \
351 /* Set member of the thread descriptor directly. */
352 # define THREAD_SETMEM_NC(descr, member, idx, value) \
353 ({ if (sizeof (descr->member[0]) == 1) \
354 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
356 "i" (offsetof (struct pthread, member)), \
358 else if (sizeof (descr->member[0]) == 4) \
359 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
361 "i" (offsetof (struct pthread, member)), \
365 if (sizeof (descr->member[0]) != 8) \
366 /* There should not be any value with a size other than 1, \
370 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
371 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
373 "i" (offsetof (struct pthread, member)), \
378 /* Atomic compare and exchange on TLS, returning old value. */
379 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
380 ({ __typeof (descr->member) __ret; \
381 __typeof (oldval) __old = (oldval); \
382 if (sizeof (descr->member) == 4) \
383 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
385 : "0" (__old), "r" (newval), \
386 "i" (offsetof (struct pthread, member))); \
388 /* Not necessary for other sizes in the moment. */ \
393 /* Atomic set bit. */
394 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
395 (void) ({ if (sizeof ((descr)->member) == 4) \
396 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
397 :: "i" (offsetof (struct pthread, member)), \
398 "ir" (1 << (bit))); \
400 /* Not necessary for other sizes in the moment. */ \
404 /* Call the user-provided thread function. */
405 #define CALL_THREAD_FCT(descr) \
407 int __ignore1, __ignore2; \
408 asm volatile ("pushl %%eax\n\t" \
411 "pushl %%gs:%P4\n\t" \
412 "call *%%gs:%P3\n\t" \
414 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
415 : "i" (offsetof (struct pthread, start_routine)), \
416 "i" (offsetof (struct pthread, arg))); \
420 /* Set the stack guard field in TCB head. */
421 #define THREAD_SET_STACK_GUARD(value) \
422 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
423 #define THREAD_COPY_STACK_GUARD(descr) \
424 ((descr)->header.stack_guard \
425 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
428 /* Set the pointer guard field in the TCB head. */
429 #define THREAD_SET_POINTER_GUARD(value) \
430 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
431 #define THREAD_COPY_POINTER_GUARD(descr) \
432 ((descr)->header.pointer_guard \
433 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
436 /* Get and set the global scope generation counter in the TCB head. */
437 #define THREAD_GSCOPE_FLAG_UNUSED 0
438 #define THREAD_GSCOPE_FLAG_USED 1
439 #define THREAD_GSCOPE_FLAG_WAIT 2
440 #define THREAD_GSCOPE_RESET_FLAG() \
443 asm volatile ("xchg %0, %%gs:%P1" \
445 : "i" (offsetof (struct pthread, header.gscope_flag)), \
446 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
447 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
448 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
451 #define THREAD_GSCOPE_SET_FLAG() \
452 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
454 # define THREAD_GSCOPE_WAIT() \
455 do { void (*ptr) (void) = GL(dl_wait_lookup_done); \
456 PTR_DEMANGLE (ptr); \
460 # define THREAD_GSCOPE_WAIT() \
461 GL(dl_wait_lookup_done) ()
465 #endif /* __ASSEMBLER__ */