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
31 /* Type for the dtv. */
45 void *tcb; /* Pointer to the TCB. Not necessarily the
46 thread descriptor used by libpthread. */
48 void *self; /* Pointer to the thread descriptor. */
51 uintptr_t stack_guard;
54 # define TLS_MULTIPLE_THREADS_IN_TCB 1
56 #else /* __ASSEMBLER__ */
57 # include <tcb-offsets.h>
61 /* We require TLS support in the tools. */
62 #define HAVE_TLS_SUPPORT
63 #define HAVE___THREAD 1
64 #define HAVE_TLS_MODEL_ATTRIBUTE 1
66 /* Signal that TLS support is available. */
69 /* Alignment requirement for the stack. For IA-32 this is governed by
70 the SSE memory functions. */
71 #define STACK_ALIGN 16
74 /* Get system call information. */
77 /* The old way: using LDT. */
79 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
82 unsigned int entry_number;
83 unsigned long int base_addr;
85 unsigned int seg_32bit:1;
86 unsigned int contents:2;
87 unsigned int read_exec_only:1;
88 unsigned int limit_in_pages:1;
89 unsigned int seg_not_present:1;
90 unsigned int useable:1;
91 unsigned int empty:25;
94 /* Initializing bit fields is slow. We speed it up by using a union. */
97 struct user_desc desc;
102 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
103 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
104 struct pthread even when not linked with -lpthread. */
105 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
107 /* Alignment requirements for the initial TCB. */
108 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
110 /* This is the size of the TCB. */
111 # define TLS_TCB_SIZE sizeof (struct pthread)
113 /* Alignment requirements for the TCB. */
114 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
116 /* Get the thread descriptor definition. */
119 /* The TCB can have any size and the memory following the address the
120 thread pointer points to is unspecified. Allocate the TCB there. */
121 # define TLS_TCB_AT_TP 1
124 /* Install the dtv pointer. The pointer passed is to the element with
125 index -1 which contain the length. */
126 # define INSTALL_DTV(descr, dtvp) \
127 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
129 /* Install new dtv for current thread. */
130 # define INSTALL_NEW_DTV(dtvp) \
131 ({ struct pthread *__pd; \
132 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
134 /* Return dtv of given thread descriptor. */
135 # define GET_DTV(descr) \
136 (((tcbhead_t *) (descr))->dtv)
138 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
139 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
141 /* Macros to load from and store into segment registers. */
143 # define TLS_GET_GS() \
144 ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
147 # define TLS_SET_GS(val) \
148 __asm__ ("movw %w0, %%gs" :: "q" (val))
152 # ifndef __NR_set_thread_area
153 # define __NR_set_thread_area 243
155 # ifndef TLS_FLAG_WRITABLE
156 # define TLS_FLAG_WRITABLE 0x00000001
159 // XXX Enable for the real world.
161 # ifndef __ASSUME_SET_THREAD_AREA
162 # error "we need set_thread_area"
167 # define TLS_EBX_ARG "r"
168 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
170 # define TLS_EBX_ARG "b"
171 # define TLS_LOAD_EBX
174 #if defined NEED_DL_SYSINFO
175 # define INIT_SYSINFO \
176 _head->sysinfo = GLRO(dl_sysinfo)
178 # define INIT_SYSINFO
183 # define LOCK_PREFIX /* nothing */
185 # define LOCK_PREFIX "lock;"
189 /* Code to initially initialize the thread pointer. This might need
190 special attention since 'errno' is not yet available and if the
191 operation can cause a failure 'errno' must not be touched. */
192 # define TLS_INIT_TP(thrdescr, secondcall) \
193 ({ void *_thrdescr = (thrdescr); \
194 tcbhead_t *_head = _thrdescr; \
195 union user_desc_init _segdescr; \
198 _head->tcb = _thrdescr; \
199 /* For now the thread descriptor is at the same address. */ \
200 _head->self = _thrdescr; \
201 /* New syscall handling support. */ \
204 /* The 'entry_number' field. Let the kernel pick a value. */ \
206 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
208 _segdescr.vals[0] = -1; \
209 /* The 'base_addr' field. Pointer to the TCB. */ \
210 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
211 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
212 _segdescr.vals[2] = 0xfffff; \
213 /* Collapsed value of the bitfield: \
216 .read_exec_only = 0 \
217 .limit_in_pages = 1 \
218 .seg_not_present = 0 \
220 _segdescr.vals[3] = 0x51; \
222 /* Install the TLS. */ \
223 __asm__ volatile (TLS_LOAD_EBX \
226 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
227 : "0" (__NR_set_thread_area), \
228 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
231 /* We know the index in the GDT, now load the segment register. \
232 The use of the GDT is described by the value 3 in the lower \
233 three bits of the segment descriptor value. \
235 Note that we have to do this even if the numeric value of \
236 the descriptor does not change. Loading the segment register \
237 causes the segment information from the GDT to be loaded \
238 which is necessary since we have changed it. */ \
239 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
241 _result == 0 ? NULL \
242 : "set_thread_area failed when setting up thread-local storage\n"; })
245 /* Return the address of the dtv for the current thread. */
246 # define THREAD_DTV() \
247 ({ struct pthread *__pd; \
248 THREAD_GETMEM (__pd, header.dtv); })
251 /* Return the thread descriptor for the current thread.
253 The contained asm must *not* be marked volatile since otherwise
255 pthread_descr self = thread_self();
256 do not get optimized away. */
257 # define THREAD_SELF \
258 ({ struct pthread *__self; \
259 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
260 : "i" (offsetof (struct pthread, header.self))); \
263 /* Magic for libthread_db to know how to do THREAD_SELF. */
264 # define DB_THREAD_SELF \
265 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
266 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
269 /* Read member of the thread descriptor directly. */
270 # define THREAD_GETMEM(descr, member) \
271 ({ __typeof (descr->member) __value; \
272 if (sizeof (__value) == 1) \
273 __asm__ volatile ("movb %%gs:%P2,%b0" \
275 : "0" (0), "i" (offsetof (struct pthread, member))); \
276 else if (sizeof (__value) == 4) \
277 __asm__ volatile ("movl %%gs:%P1,%0" \
279 : "i" (offsetof (struct pthread, member))); \
282 if (sizeof (__value) != 8) \
283 /* There should not be any value with a size other than 1, \
287 __asm__ volatile ("movl %%gs:%P1,%%eax\n\t" \
288 "movl %%gs:%P2,%%edx" \
290 : "i" (offsetof (struct pthread, member)), \
291 "i" (offsetof (struct pthread, member) + 4)); \
296 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
297 # define THREAD_GETMEM_NC(descr, member, idx) \
298 ({ __typeof (descr->member[0]) __value; \
299 if (sizeof (__value) == 1) \
300 __asm__ volatile ("movb %%gs:%P2(%3),%b0" \
302 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
304 else if (sizeof (__value) == 4) \
305 __asm__ volatile ("movl %%gs:%P1(,%2,4),%0" \
307 : "i" (offsetof (struct pthread, member[0])), \
311 if (sizeof (__value) != 8) \
312 /* There should not be any value with a size other than 1, \
316 __asm__ volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
317 "movl %%gs:4+%P1(,%2,8),%%edx" \
319 : "i" (offsetof (struct pthread, member[0])), \
325 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
326 # define THREAD_SETMEM(descr, member, value) \
327 ({ if (sizeof (descr->member) == 1) \
328 __asm__ volatile ("movb %b0,%%gs:%P1" : \
330 "i" (offsetof (struct pthread, member))); \
331 else if (sizeof (descr->member) == 4) \
332 __asm__ volatile ("movl %0,%%gs:%P1" : \
334 "i" (offsetof (struct pthread, member))); \
337 if (sizeof (descr->member) != 8) \
338 /* There should not be any value with a size other than 1, \
342 __asm__ volatile ("movl %%eax,%%gs:%P1\n\t" \
343 "movl %%edx,%%gs:%P2" : \
345 "i" (offsetof (struct pthread, member)), \
346 "i" (offsetof (struct pthread, member) + 4)); \
350 /* Set member of the thread descriptor directly. */
351 # define THREAD_SETMEM_NC(descr, member, idx, value) \
352 ({ if (sizeof (descr->member[0]) == 1) \
353 __asm__ volatile ("movb %b0,%%gs:%P1(%2)" : \
355 "i" (offsetof (struct pthread, member)), \
357 else if (sizeof (descr->member[0]) == 4) \
358 __asm__ volatile ("movl %0,%%gs:%P1(,%2,4)" : \
360 "i" (offsetof (struct pthread, member)), \
364 if (sizeof (descr->member[0]) != 8) \
365 /* There should not be any value with a size other than 1, \
369 __asm__ volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
370 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
372 "i" (offsetof (struct pthread, member)), \
377 /* Atomic compare and exchange on TLS, returning old value. */
378 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
379 ({ __typeof (descr->member) __ret; \
380 __typeof (oldval) __old = (oldval); \
381 if (sizeof (descr->member) == 4) \
382 __asm__ volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
384 : "0" (__old), "r" (newval), \
385 "i" (offsetof (struct pthread, member))); \
387 /* Not necessary for other sizes in the moment. */ \
392 /* Atomic set bit. */
393 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
394 (void) ({ if (sizeof ((descr)->member) == 4) \
395 __asm__ volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
396 :: "i" (offsetof (struct pthread, member)), \
397 "ir" (1 << (bit))); \
399 /* Not necessary for other sizes in the moment. */ \
403 /* Call the user-provided thread function. */
404 #define CALL_THREAD_FCT(descr) \
406 int __ignore1, __ignore2; \
407 __asm__ volatile ("pushl %%eax\n\t" \
410 "pushl %%gs:%P4\n\t" \
411 "call *%%gs:%P3\n\t" \
413 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
414 : "i" (offsetof (struct pthread, start_routine)), \
415 "i" (offsetof (struct pthread, arg))); \
419 /* Set the stack guard field in TCB head. */
420 #define THREAD_SET_STACK_GUARD(value) \
421 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
422 #define THREAD_COPY_STACK_GUARD(descr) \
423 ((descr)->header.stack_guard \
424 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
427 #endif /* __ASSEMBLER__ */