Updated to fedora-glibc-20050627T0850
[glibc.git] / nptl / sysdeps / i386 / tls.h
blob65566ff7a7a7d728c949503585b8c3967699f9dc
1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002, 2003, 2004, 2005 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 } tcbhead_t;
55 # define TLS_MULTIPLE_THREADS_IN_TCB 1
57 #else /* __ASSEMBLER__ */
58 # include <tcb-offsets.h>
59 #endif
62 /* We require TLS support in the tools. */
63 #ifndef HAVE_TLS_SUPPORT
64 # error "TLS support is required."
65 #endif
67 /* Signal that TLS support is available. */
68 #define USE_TLS 1
70 /* Alignment requirement for the stack. For IA-32 this is governed by
71 the SSE memory functions. */
72 #define STACK_ALIGN 16
74 #ifndef __ASSEMBLER__
75 /* Get system call information. */
76 # include <sysdep.h>
78 /* The old way: using LDT. */
80 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
81 struct user_desc
83 unsigned int entry_number;
84 unsigned long int base_addr;
85 unsigned int limit;
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. */
96 union user_desc_init
98 struct user_desc desc;
99 unsigned int vals[4];
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. */
143 # ifndef TLS_GET_GS
144 # define TLS_GET_GS() \
145 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
146 # endif
147 # ifndef TLS_SET_GS
148 # define TLS_SET_GS(val) \
149 __asm ("movw %w0, %%gs" :: "q" (val))
150 # endif
153 # ifndef __NR_set_thread_area
154 # define __NR_set_thread_area 243
155 # endif
156 # ifndef TLS_FLAG_WRITABLE
157 # define TLS_FLAG_WRITABLE 0x00000001
158 # endif
160 // XXX Enable for the real world.
161 #if 0
162 # ifndef __ASSUME_SET_THREAD_AREA
163 # error "we need set_thread_area"
164 # endif
165 #endif
167 # ifdef __PIC__
168 # define TLS_EBX_ARG "r"
169 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
170 # else
171 # define TLS_EBX_ARG "b"
172 # define TLS_LOAD_EBX
173 # endif
175 #if defined NEED_DL_SYSINFO
176 # define INIT_SYSINFO \
177 _head->sysinfo = GLRO(dl_sysinfo)
178 #else
179 # define INIT_SYSINFO
180 #endif
182 #ifndef LOCK_PREFIX
183 # ifdef UP
184 # define LOCK_PREFIX /* nothing */
185 # else
186 # define LOCK_PREFIX "lock;"
187 # endif
188 #endif
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; \
197 int _result; \
199 _head->tcb = _thrdescr; \
200 /* For now the thread descriptor is at the same address. */ \
201 _head->self = _thrdescr; \
202 /* New syscall handling support. */ \
203 INIT_SYSINFO; \
205 /* The 'entry_number' field. Let the kernel pick a value. */ \
206 if (secondcall) \
207 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
208 else \
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: \
215 .seg_32bit = 1 \
216 .contents = 0 \
217 .read_exec_only = 0 \
218 .limit_in_pages = 1 \
219 .seg_not_present = 0 \
220 .useable = 1 */ \
221 _segdescr.vals[3] = 0x51; \
223 /* Install the TLS. */ \
224 asm volatile (TLS_LOAD_EBX \
225 "int $0x80\n\t" \
226 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)); \
231 if (_result == 0) \
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
255 assignments like
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))); \
262 __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" \
275 : "=q" (__value) \
276 : "0" (0), "i" (offsetof (struct pthread, member))); \
277 else if (sizeof (__value) == 4) \
278 asm volatile ("movl %%gs:%P1,%0" \
279 : "=r" (__value) \
280 : "i" (offsetof (struct pthread, member))); \
281 else \
283 if (sizeof (__value) != 8) \
284 /* There should not be any value with a size other than 1, \
285 4 or 8. */ \
286 abort (); \
288 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
289 "movl %%gs:%P2,%%edx" \
290 : "=A" (__value) \
291 : "i" (offsetof (struct pthread, member)), \
292 "i" (offsetof (struct pthread, member) + 4)); \
294 __value; })
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" \
302 : "=q" (__value) \
303 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
304 "r" (idx)); \
305 else if (sizeof (__value) == 4) \
306 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
307 : "=r" (__value) \
308 : "i" (offsetof (struct pthread, member[0])), \
309 "r" (idx)); \
310 else \
312 if (sizeof (__value) != 8) \
313 /* There should not be any value with a size other than 1, \
314 4 or 8. */ \
315 abort (); \
317 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
318 "movl %%gs:4+%P1(,%2,8),%%edx" \
319 : "=&A" (__value) \
320 : "i" (offsetof (struct pthread, member[0])), \
321 "r" (idx)); \
323 __value; })
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" : \
330 : "iq" (value), \
331 "i" (offsetof (struct pthread, member))); \
332 else if (sizeof (descr->member) == 4) \
333 asm volatile ("movl %0,%%gs:%P1" : \
334 : "ir" (value), \
335 "i" (offsetof (struct pthread, member))); \
336 else \
338 if (sizeof (descr->member) != 8) \
339 /* There should not be any value with a size other than 1, \
340 4 or 8. */ \
341 abort (); \
343 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
344 "movl %%edx,%%gs:%P2" : \
345 : "A" (value), \
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)" : \
355 : "iq" (value), \
356 "i" (offsetof (struct pthread, member)), \
357 "r" (idx)); \
358 else if (sizeof (descr->member[0]) == 4) \
359 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
360 : "ir" (value), \
361 "i" (offsetof (struct pthread, member)), \
362 "r" (idx)); \
363 else \
365 if (sizeof (descr->member[0]) != 8) \
366 /* There should not be any value with a size other than 1, \
367 4 or 8. */ \
368 abort (); \
370 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
371 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
372 : "A" (value), \
373 "i" (offsetof (struct pthread, member)), \
374 "r" (idx)); \
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" \
384 : "=a" (__ret) \
385 : "0" (__old), "r" (newval), \
386 "i" (offsetof (struct pthread, member))); \
387 else \
388 /* Not necessary for other sizes in the moment. */ \
389 abort (); \
390 __ret; })
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))); \
399 else \
400 /* Not necessary for other sizes in the moment. */ \
401 abort (); })
404 /* Call the user-provided thread function. */
405 #define CALL_THREAD_FCT(descr) \
406 ({ void *__res; \
407 int __ignore1, __ignore2; \
408 asm volatile ("pushl %%eax\n\t" \
409 "pushl %%eax\n\t" \
410 "pushl %%eax\n\t" \
411 "pushl %%gs:%P4\n\t" \
412 "call *%%gs:%P3\n\t" \
413 "addl $16, %%esp" \
414 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
415 : "i" (offsetof (struct pthread, start_routine)), \
416 "i" (offsetof (struct pthread, arg))); \
417 __res; })
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 #endif /* __ASSEMBLER__ */
430 #endif /* tls.h */