2.5-18.1
[glibc.git] / nptl / sysdeps / i386 / tls.h
blobb3d9142fc5114de50cb1ca39a093b5c996125e45
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002,2003,2004,2005,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
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>
32 /* Type for the dtv. */
33 typedef union dtv
35 size_t counter;
36 struct
38 void *val;
39 bool is_static;
40 } pointer;
41 } dtv_t;
44 typedef struct
46 void *tcb; /* Pointer to the TCB. Not necessarily the
47 thread descriptor used by libpthread. */
48 dtv_t *dtv;
49 void *self; /* Pointer to the thread descriptor. */
50 int multiple_threads;
51 uintptr_t sysinfo;
52 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
54 int gscope_flag;
55 } tcbhead_t;
57 # define TLS_MULTIPLE_THREADS_IN_TCB 1
59 #else /* __ASSEMBLER__ */
60 # include <tcb-offsets.h>
61 #endif
64 /* We require TLS support in the tools. */
65 #ifndef HAVE_TLS_SUPPORT
66 # error "TLS support is required."
67 #endif
69 /* Signal that TLS support is available. */
70 #define USE_TLS 1
72 /* Alignment requirement for the stack. For IA-32 this is governed by
73 the SSE memory functions. */
74 #define STACK_ALIGN 16
76 #ifndef __ASSEMBLER__
77 /* Get system call information. */
78 # include <sysdep.h>
80 /* The old way: using LDT. */
82 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
83 struct user_desc
85 unsigned int entry_number;
86 unsigned long int base_addr;
87 unsigned int limit;
88 unsigned int seg_32bit:1;
89 unsigned int contents:2;
90 unsigned int read_exec_only:1;
91 unsigned int limit_in_pages:1;
92 unsigned int seg_not_present:1;
93 unsigned int useable:1;
94 unsigned int empty:25;
97 /* Initializing bit fields is slow. We speed it up by using a union. */
98 union user_desc_init
100 struct user_desc desc;
101 unsigned int vals[4];
105 /* Get the thread descriptor definition. */
106 # include <nptl/descr.h>
108 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
109 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
110 struct pthread even when not linked with -lpthread. */
111 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
113 /* Alignment requirements for the initial TCB. */
114 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
116 /* This is the size of the TCB. */
117 # define TLS_TCB_SIZE sizeof (struct pthread)
119 /* Alignment requirements for the TCB. */
120 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
122 /* The TCB can have any size and the memory following the address the
123 thread pointer points to is unspecified. Allocate the TCB there. */
124 # define TLS_TCB_AT_TP 1
127 /* Install the dtv pointer. The pointer passed is to the element with
128 index -1 which contain the length. */
129 # define INSTALL_DTV(descr, dtvp) \
130 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
132 /* Install new dtv for current thread. */
133 # define INSTALL_NEW_DTV(dtvp) \
134 ({ struct pthread *__pd; \
135 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
137 /* Return dtv of given thread descriptor. */
138 # define GET_DTV(descr) \
139 (((tcbhead_t *) (descr))->dtv)
141 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
142 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
144 /* Macros to load from and store into segment registers. */
145 # ifndef TLS_GET_GS
146 # define TLS_GET_GS() \
147 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
148 # endif
149 # ifndef TLS_SET_GS
150 # define TLS_SET_GS(val) \
151 __asm ("movw %w0, %%gs" :: "q" (val))
152 # endif
155 # ifndef __NR_set_thread_area
156 # define __NR_set_thread_area 243
157 # endif
158 # ifndef TLS_FLAG_WRITABLE
159 # define TLS_FLAG_WRITABLE 0x00000001
160 # endif
162 // XXX Enable for the real world.
163 #if 0
164 # ifndef __ASSUME_SET_THREAD_AREA
165 # error "we need set_thread_area"
166 # endif
167 #endif
169 # ifdef __PIC__
170 # define TLS_EBX_ARG "r"
171 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
172 # else
173 # define TLS_EBX_ARG "b"
174 # define TLS_LOAD_EBX
175 # endif
177 #if defined NEED_DL_SYSINFO
178 # define INIT_SYSINFO \
179 _head->sysinfo = GLRO(dl_sysinfo)
180 #else
181 # define INIT_SYSINFO
182 #endif
184 #ifndef LOCK_PREFIX
185 # ifdef UP
186 # define LOCK_PREFIX /* nothing */
187 # else
188 # define LOCK_PREFIX "lock;"
189 # endif
190 #endif
192 /* Code to initially initialize the thread pointer. This might need
193 special attention since 'errno' is not yet available and if the
194 operation can cause a failure 'errno' must not be touched. */
195 # define TLS_INIT_TP(thrdescr, secondcall) \
196 ({ void *_thrdescr = (thrdescr); \
197 tcbhead_t *_head = _thrdescr; \
198 union user_desc_init _segdescr; \
199 int _result; \
201 _head->tcb = _thrdescr; \
202 /* For now the thread descriptor is at the same address. */ \
203 _head->self = _thrdescr; \
204 /* New syscall handling support. */ \
205 INIT_SYSINFO; \
207 /* The 'entry_number' field. Let the kernel pick a value. */ \
208 if (secondcall) \
209 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
210 else \
211 _segdescr.vals[0] = -1; \
212 /* The 'base_addr' field. Pointer to the TCB. */ \
213 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
214 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
215 _segdescr.vals[2] = 0xfffff; \
216 /* Collapsed value of the bitfield: \
217 .seg_32bit = 1 \
218 .contents = 0 \
219 .read_exec_only = 0 \
220 .limit_in_pages = 1 \
221 .seg_not_present = 0 \
222 .useable = 1 */ \
223 _segdescr.vals[3] = 0x51; \
225 /* Install the TLS. */ \
226 asm volatile (TLS_LOAD_EBX \
227 "int $0x80\n\t" \
228 TLS_LOAD_EBX \
229 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
230 : "0" (__NR_set_thread_area), \
231 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
233 if (_result == 0) \
234 /* We know the index in the GDT, now load the segment register. \
235 The use of the GDT is described by the value 3 in the lower \
236 three bits of the segment descriptor value. \
238 Note that we have to do this even if the numeric value of \
239 the descriptor does not change. Loading the segment register \
240 causes the segment information from the GDT to be loaded \
241 which is necessary since we have changed it. */ \
242 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
244 _result == 0 ? NULL \
245 : "set_thread_area failed when setting up thread-local storage\n"; })
248 /* Return the address of the dtv for the current thread. */
249 # define THREAD_DTV() \
250 ({ struct pthread *__pd; \
251 THREAD_GETMEM (__pd, header.dtv); })
254 /* Return the thread descriptor for the current thread.
256 The contained asm must *not* be marked volatile since otherwise
257 assignments like
258 pthread_descr self = thread_self();
259 do not get optimized away. */
260 # define THREAD_SELF \
261 ({ struct pthread *__self; \
262 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
263 : "i" (offsetof (struct pthread, header.self))); \
264 __self;})
266 /* Magic for libthread_db to know how to do THREAD_SELF. */
267 # define DB_THREAD_SELF \
268 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
269 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
272 /* Read member of the thread descriptor directly. */
273 # define THREAD_GETMEM(descr, member) \
274 ({ __typeof (descr->member) __value; \
275 if (sizeof (__value) == 1) \
276 asm volatile ("movb %%gs:%P2,%b0" \
277 : "=q" (__value) \
278 : "0" (0), "i" (offsetof (struct pthread, member))); \
279 else if (sizeof (__value) == 4) \
280 asm volatile ("movl %%gs:%P1,%0" \
281 : "=r" (__value) \
282 : "i" (offsetof (struct pthread, member))); \
283 else \
285 if (sizeof (__value) != 8) \
286 /* There should not be any value with a size other than 1, \
287 4 or 8. */ \
288 abort (); \
290 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
291 "movl %%gs:%P2,%%edx" \
292 : "=A" (__value) \
293 : "i" (offsetof (struct pthread, member)), \
294 "i" (offsetof (struct pthread, member) + 4)); \
296 __value; })
299 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
300 # define THREAD_GETMEM_NC(descr, member, idx) \
301 ({ __typeof (descr->member[0]) __value; \
302 if (sizeof (__value) == 1) \
303 asm volatile ("movb %%gs:%P2(%3),%b0" \
304 : "=q" (__value) \
305 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
306 "r" (idx)); \
307 else if (sizeof (__value) == 4) \
308 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
309 : "=r" (__value) \
310 : "i" (offsetof (struct pthread, member[0])), \
311 "r" (idx)); \
312 else \
314 if (sizeof (__value) != 8) \
315 /* There should not be any value with a size other than 1, \
316 4 or 8. */ \
317 abort (); \
319 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
320 "movl %%gs:4+%P1(,%2,8),%%edx" \
321 : "=&A" (__value) \
322 : "i" (offsetof (struct pthread, member[0])), \
323 "r" (idx)); \
325 __value; })
328 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
329 # define THREAD_SETMEM(descr, member, value) \
330 ({ if (sizeof (descr->member) == 1) \
331 asm volatile ("movb %b0,%%gs:%P1" : \
332 : "iq" (value), \
333 "i" (offsetof (struct pthread, member))); \
334 else if (sizeof (descr->member) == 4) \
335 asm volatile ("movl %0,%%gs:%P1" : \
336 : "ir" (value), \
337 "i" (offsetof (struct pthread, member))); \
338 else \
340 if (sizeof (descr->member) != 8) \
341 /* There should not be any value with a size other than 1, \
342 4 or 8. */ \
343 abort (); \
345 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
346 "movl %%edx,%%gs:%P2" : \
347 : "A" (value), \
348 "i" (offsetof (struct pthread, member)), \
349 "i" (offsetof (struct pthread, member) + 4)); \
353 /* Set member of the thread descriptor directly. */
354 # define THREAD_SETMEM_NC(descr, member, idx, value) \
355 ({ if (sizeof (descr->member[0]) == 1) \
356 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
357 : "iq" (value), \
358 "i" (offsetof (struct pthread, member)), \
359 "r" (idx)); \
360 else if (sizeof (descr->member[0]) == 4) \
361 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
362 : "ir" (value), \
363 "i" (offsetof (struct pthread, member)), \
364 "r" (idx)); \
365 else \
367 if (sizeof (descr->member[0]) != 8) \
368 /* There should not be any value with a size other than 1, \
369 4 or 8. */ \
370 abort (); \
372 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
373 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
374 : "A" (value), \
375 "i" (offsetof (struct pthread, member)), \
376 "r" (idx)); \
380 /* Atomic compare and exchange on TLS, returning old value. */
381 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
382 ({ __typeof (descr->member) __ret; \
383 __typeof (oldval) __old = (oldval); \
384 if (sizeof (descr->member) == 4) \
385 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
386 : "=a" (__ret) \
387 : "0" (__old), "r" (newval), \
388 "i" (offsetof (struct pthread, member))); \
389 else \
390 /* Not necessary for other sizes in the moment. */ \
391 abort (); \
392 __ret; })
395 /* Atomic set bit. */
396 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
397 (void) ({ if (sizeof ((descr)->member) == 4) \
398 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
399 :: "i" (offsetof (struct pthread, member)), \
400 "ir" (1 << (bit))); \
401 else \
402 /* Not necessary for other sizes in the moment. */ \
403 abort (); })
406 /* Call the user-provided thread function. */
407 #define CALL_THREAD_FCT(descr) \
408 ({ void *__res; \
409 int __ignore1, __ignore2; \
410 asm volatile ("pushl %%eax\n\t" \
411 "pushl %%eax\n\t" \
412 "pushl %%eax\n\t" \
413 "pushl %%gs:%P4\n\t" \
414 "call *%%gs:%P3\n\t" \
415 "addl $16, %%esp" \
416 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
417 : "i" (offsetof (struct pthread, start_routine)), \
418 "i" (offsetof (struct pthread, arg))); \
419 __res; })
422 /* Set the stack guard field in TCB head. */
423 #define THREAD_SET_STACK_GUARD(value) \
424 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
425 #define THREAD_COPY_STACK_GUARD(descr) \
426 ((descr)->header.stack_guard \
427 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
430 /* Set the pointer guard field in the TCB head. */
431 #define THREAD_SET_POINTER_GUARD(value) \
432 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
433 #define THREAD_COPY_POINTER_GUARD(descr) \
434 ((descr)->header.pointer_guard \
435 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
438 /* Get and set the global scope generation counter in the TCB head. */
439 #define THREAD_GSCOPE_FLAG_UNUSED 0
440 #define THREAD_GSCOPE_FLAG_USED 1
441 #define THREAD_GSCOPE_FLAG_WAIT 2
442 #define THREAD_GSCOPE_RESET_FLAG() \
443 do \
444 { int __res; \
445 asm volatile ("xchgl %0, %%gs:%P1" \
446 : "=r" (__res) \
447 : "i" (offsetof (struct pthread, header.gscope_flag)), \
448 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
449 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
450 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1); \
452 while (0)
453 #define THREAD_GSCOPE_SET_FLAG() \
454 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
455 #define THREAD_GSCOPE_WAIT() \
456 GL(dl_wait_lookup_done) ()
458 #endif /* __ASSEMBLER__ */
460 #endif /* tls.h */