Updated to fedora-glibc-20090509T1828
[glibc.git] / nptl / sysdeps / i386 / tls.h
blobf23977e73f24201829ee685ff96ea558d3c8f100
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _TLS_H
21 #define _TLS_H 1
23 #include <dl-sysdep.h>
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28 # include <stdlib.h>
29 # include <list.h>
30 # include <sysdep.h>
31 # include <kernel-features.h>
34 /* Type for the dtv. */
35 typedef union dtv
37 size_t counter;
38 struct
40 void *val;
41 bool is_static;
42 } pointer;
43 } dtv_t;
46 typedef struct
48 void *tcb; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
50 dtv_t *dtv;
51 void *self; /* Pointer to the thread descriptor. */
52 int multiple_threads;
53 uintptr_t sysinfo;
54 uintptr_t stack_guard;
55 uintptr_t pointer_guard;
56 int gscope_flag;
57 #ifndef __ASSUME_PRIVATE_FUTEX
58 int private_futex;
59 #else
60 int __unused1;
61 #endif
62 /* Reservation of some values for the TM ABI. */
63 void *__private_tm[5];
64 } tcbhead_t;
66 # define TLS_MULTIPLE_THREADS_IN_TCB 1
68 #else /* __ASSEMBLER__ */
69 # include <tcb-offsets.h>
70 #endif
73 /* We require TLS support in the tools. */
74 #ifndef HAVE_TLS_SUPPORT
75 # error "TLS support is required."
76 #endif
78 /* Alignment requirement for the stack. For IA-32 this is governed by
79 the SSE memory functions. */
80 #define STACK_ALIGN 16
82 #ifndef __ASSEMBLER__
83 /* Get system call information. */
84 # include <sysdep.h>
86 /* The old way: using LDT. */
88 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
89 struct user_desc
91 unsigned int entry_number;
92 unsigned long int base_addr;
93 unsigned int limit;
94 unsigned int seg_32bit:1;
95 unsigned int contents:2;
96 unsigned int read_exec_only:1;
97 unsigned int limit_in_pages:1;
98 unsigned int seg_not_present:1;
99 unsigned int useable:1;
100 unsigned int empty:25;
103 /* Initializing bit fields is slow. We speed it up by using a union. */
104 union user_desc_init
106 struct user_desc desc;
107 unsigned int vals[4];
111 /* Get the thread descriptor definition. */
112 # include <nptl/descr.h>
114 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
115 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
116 struct pthread even when not linked with -lpthread. */
117 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
119 /* Alignment requirements for the initial TCB. */
120 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
122 /* This is the size of the TCB. */
123 # define TLS_TCB_SIZE sizeof (struct pthread)
125 /* Alignment requirements for the TCB. */
126 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
128 /* The TCB can have any size and the memory following the address the
129 thread pointer points to is unspecified. Allocate the TCB there. */
130 # define TLS_TCB_AT_TP 1
133 /* Install the dtv pointer. The pointer passed is to the element with
134 index -1 which contain the length. */
135 # define INSTALL_DTV(descr, dtvp) \
136 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
138 /* Install new dtv for current thread. */
139 # define INSTALL_NEW_DTV(dtvp) \
140 ({ struct pthread *__pd; \
141 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
143 /* Return dtv of given thread descriptor. */
144 # define GET_DTV(descr) \
145 (((tcbhead_t *) (descr))->dtv)
147 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
148 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
150 /* Macros to load from and store into segment registers. */
151 # ifndef TLS_GET_GS
152 # define TLS_GET_GS() \
153 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
154 # endif
155 # ifndef TLS_SET_GS
156 # define TLS_SET_GS(val) \
157 __asm ("movw %w0, %%gs" :: "q" (val))
158 # endif
161 # ifndef __NR_set_thread_area
162 # define __NR_set_thread_area 243
163 # endif
164 # ifndef TLS_FLAG_WRITABLE
165 # define TLS_FLAG_WRITABLE 0x00000001
166 # endif
168 // XXX Enable for the real world.
169 #if 0
170 # ifndef __ASSUME_SET_THREAD_AREA
171 # error "we need set_thread_area"
172 # endif
173 #endif
175 # ifdef __PIC__
176 # define TLS_EBX_ARG "r"
177 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
178 # else
179 # define TLS_EBX_ARG "b"
180 # define TLS_LOAD_EBX
181 # endif
183 #if defined NEED_DL_SYSINFO
184 # define INIT_SYSINFO \
185 _head->sysinfo = GLRO(dl_sysinfo)
186 #else
187 # define INIT_SYSINFO
188 #endif
190 #ifndef LOCK_PREFIX
191 # ifdef UP
192 # define LOCK_PREFIX /* nothing */
193 # else
194 # define LOCK_PREFIX "lock;"
195 # endif
196 #endif
198 /* Code to initially initialize the thread pointer. This might need
199 special attention since 'errno' is not yet available and if the
200 operation can cause a failure 'errno' must not be touched. */
201 # define TLS_INIT_TP(thrdescr, secondcall) \
202 ({ void *_thrdescr = (thrdescr); \
203 tcbhead_t *_head = _thrdescr; \
204 union user_desc_init _segdescr; \
205 int _result; \
207 _head->tcb = _thrdescr; \
208 /* For now the thread descriptor is at the same address. */ \
209 _head->self = _thrdescr; \
210 /* New syscall handling support. */ \
211 INIT_SYSINFO; \
213 /* The 'entry_number' field. Let the kernel pick a value. */ \
214 if (secondcall) \
215 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
216 else \
217 _segdescr.vals[0] = -1; \
218 /* The 'base_addr' field. Pointer to the TCB. */ \
219 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
220 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
221 _segdescr.vals[2] = 0xfffff; \
222 /* Collapsed value of the bitfield: \
223 .seg_32bit = 1 \
224 .contents = 0 \
225 .read_exec_only = 0 \
226 .limit_in_pages = 1 \
227 .seg_not_present = 0 \
228 .useable = 1 */ \
229 _segdescr.vals[3] = 0x51; \
231 /* Install the TLS. */ \
232 asm volatile (TLS_LOAD_EBX \
233 "int $0x80\n\t" \
234 TLS_LOAD_EBX \
235 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
236 : "0" (__NR_set_thread_area), \
237 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
239 if (_result == 0) \
240 /* We know the index in the GDT, now load the segment register. \
241 The use of the GDT is described by the value 3 in the lower \
242 three bits of the segment descriptor value. \
244 Note that we have to do this even if the numeric value of \
245 the descriptor does not change. Loading the segment register \
246 causes the segment information from the GDT to be loaded \
247 which is necessary since we have changed it. */ \
248 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
250 _result == 0 ? NULL \
251 : "set_thread_area failed when setting up thread-local storage\n"; })
254 /* Return the address of the dtv for the current thread. */
255 # define THREAD_DTV() \
256 ({ struct pthread *__pd; \
257 THREAD_GETMEM (__pd, header.dtv); })
260 /* Return the thread descriptor for the current thread.
262 The contained asm must *not* be marked volatile since otherwise
263 assignments like
264 pthread_descr self = thread_self();
265 do not get optimized away. */
266 # define THREAD_SELF \
267 ({ struct pthread *__self; \
268 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
269 : "i" (offsetof (struct pthread, header.self))); \
270 __self;})
272 /* Magic for libthread_db to know how to do THREAD_SELF. */
273 # define DB_THREAD_SELF \
274 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
275 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
278 /* Read member of the thread descriptor directly. */
279 # define THREAD_GETMEM(descr, member) \
280 ({ __typeof (descr->member) __value; \
281 if (sizeof (__value) == 1) \
282 asm volatile ("movb %%gs:%P2,%b0" \
283 : "=q" (__value) \
284 : "0" (0), "i" (offsetof (struct pthread, member))); \
285 else if (sizeof (__value) == 4) \
286 asm volatile ("movl %%gs:%P1,%0" \
287 : "=r" (__value) \
288 : "i" (offsetof (struct pthread, member))); \
289 else \
291 if (sizeof (__value) != 8) \
292 /* There should not be any value with a size other than 1, \
293 4 or 8. */ \
294 abort (); \
296 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
297 "movl %%gs:%P2,%%edx" \
298 : "=A" (__value) \
299 : "i" (offsetof (struct pthread, member)), \
300 "i" (offsetof (struct pthread, member) + 4)); \
302 __value; })
305 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
306 # define THREAD_GETMEM_NC(descr, member, idx) \
307 ({ __typeof (descr->member[0]) __value; \
308 if (sizeof (__value) == 1) \
309 asm volatile ("movb %%gs:%P2(%3),%b0" \
310 : "=q" (__value) \
311 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
312 "r" (idx)); \
313 else if (sizeof (__value) == 4) \
314 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
315 : "=r" (__value) \
316 : "i" (offsetof (struct pthread, member[0])), \
317 "r" (idx)); \
318 else \
320 if (sizeof (__value) != 8) \
321 /* There should not be any value with a size other than 1, \
322 4 or 8. */ \
323 abort (); \
325 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
326 "movl %%gs:4+%P1(,%2,8),%%edx" \
327 : "=&A" (__value) \
328 : "i" (offsetof (struct pthread, member[0])), \
329 "r" (idx)); \
331 __value; })
334 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
335 # define THREAD_SETMEM(descr, member, value) \
336 ({ if (sizeof (descr->member) == 1) \
337 asm volatile ("movb %b0,%%gs:%P1" : \
338 : "iq" (value), \
339 "i" (offsetof (struct pthread, member))); \
340 else if (sizeof (descr->member) == 4) \
341 asm volatile ("movl %0,%%gs:%P1" : \
342 : "ir" (value), \
343 "i" (offsetof (struct pthread, member))); \
344 else \
346 if (sizeof (descr->member) != 8) \
347 /* There should not be any value with a size other than 1, \
348 4 or 8. */ \
349 abort (); \
351 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
352 "movl %%edx,%%gs:%P2" : \
353 : "A" (value), \
354 "i" (offsetof (struct pthread, member)), \
355 "i" (offsetof (struct pthread, member) + 4)); \
359 /* Set member of the thread descriptor directly. */
360 # define THREAD_SETMEM_NC(descr, member, idx, value) \
361 ({ if (sizeof (descr->member[0]) == 1) \
362 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
363 : "iq" (value), \
364 "i" (offsetof (struct pthread, member)), \
365 "r" (idx)); \
366 else if (sizeof (descr->member[0]) == 4) \
367 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
368 : "ir" (value), \
369 "i" (offsetof (struct pthread, member)), \
370 "r" (idx)); \
371 else \
373 if (sizeof (descr->member[0]) != 8) \
374 /* There should not be any value with a size other than 1, \
375 4 or 8. */ \
376 abort (); \
378 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
379 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
380 : "A" (value), \
381 "i" (offsetof (struct pthread, member)), \
382 "r" (idx)); \
386 /* Atomic compare and exchange on TLS, returning old value. */
387 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
388 ({ __typeof (descr->member) __ret; \
389 __typeof (oldval) __old = (oldval); \
390 if (sizeof (descr->member) == 4) \
391 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
392 : "=a" (__ret) \
393 : "0" (__old), "r" (newval), \
394 "i" (offsetof (struct pthread, member))); \
395 else \
396 /* Not necessary for other sizes in the moment. */ \
397 abort (); \
398 __ret; })
401 /* Atomic logical and. */
402 #define THREAD_ATOMIC_AND(descr, member, val) \
403 (void) ({ if (sizeof ((descr)->member) == 4) \
404 asm volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
405 :: "i" (offsetof (struct pthread, member)), \
406 "ir" (val)); \
407 else \
408 /* Not necessary for other sizes in the moment. */ \
409 abort (); })
412 /* Atomic set bit. */
413 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
414 (void) ({ if (sizeof ((descr)->member) == 4) \
415 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
416 :: "i" (offsetof (struct pthread, member)), \
417 "ir" (1 << (bit))); \
418 else \
419 /* Not necessary for other sizes in the moment. */ \
420 abort (); })
423 /* Call the user-provided thread function. */
424 #define CALL_THREAD_FCT(descr) \
425 ({ void *__res; \
426 int __ignore1, __ignore2; \
427 asm volatile ("pushl %%eax\n\t" \
428 "pushl %%eax\n\t" \
429 "pushl %%eax\n\t" \
430 "pushl %%gs:%P4\n\t" \
431 "call *%%gs:%P3\n\t" \
432 "addl $16, %%esp" \
433 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
434 : "i" (offsetof (struct pthread, start_routine)), \
435 "i" (offsetof (struct pthread, arg))); \
436 __res; })
439 /* Set the stack guard field in TCB head. */
440 #define THREAD_SET_STACK_GUARD(value) \
441 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
442 #define THREAD_COPY_STACK_GUARD(descr) \
443 ((descr)->header.stack_guard \
444 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
447 /* Set the pointer guard field in the TCB head. */
448 #define THREAD_SET_POINTER_GUARD(value) \
449 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
450 #define THREAD_COPY_POINTER_GUARD(descr) \
451 ((descr)->header.pointer_guard \
452 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
455 /* Get and set the global scope generation counter in the TCB head. */
456 #define THREAD_GSCOPE_FLAG_UNUSED 0
457 #define THREAD_GSCOPE_FLAG_USED 1
458 #define THREAD_GSCOPE_FLAG_WAIT 2
459 #define THREAD_GSCOPE_RESET_FLAG() \
460 do \
461 { int __res; \
462 asm volatile ("xchgl %0, %%gs:%P1" \
463 : "=r" (__res) \
464 : "i" (offsetof (struct pthread, header.gscope_flag)), \
465 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
466 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
467 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
469 while (0)
470 #define THREAD_GSCOPE_SET_FLAG() \
471 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
472 #define THREAD_GSCOPE_WAIT() \
473 GL(dl_wait_lookup_done) ()
475 #endif /* __ASSEMBLER__ */
477 #endif /* tls.h */