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
30 # include <bits/kernel-features.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;
56 #ifndef __ASSUME_PRIVATE_FUTEX
61 /* Reservation of some values for the TM ABI. */
62 void *__private_tm[5];
65 # define TLS_MULTIPLE_THREADS_IN_TCB 1
67 #else /* __ASSEMBLER__ */
68 # include <tcb-offsets.h>
72 /* We require TLS support in the tools. */
73 #define HAVE_TLS_SUPPORT
74 #define HAVE___THREAD 1
75 #define HAVE_TLS_MODEL_ATTRIBUTE 1
77 /* Signal that TLS support is available. */
81 /* Alignment requirement for the stack. For IA-32 this is governed by
82 the SSE memory functions. */
83 #define STACK_ALIGN 16
86 /* Get system call information. */
89 /* The old way: using LDT. */
91 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
94 unsigned int entry_number;
95 unsigned long int base_addr;
97 unsigned int seg_32bit:1;
98 unsigned int contents:2;
99 unsigned int read_exec_only:1;
100 unsigned int limit_in_pages:1;
101 unsigned int seg_not_present:1;
102 unsigned int useable:1;
103 unsigned int empty:25;
106 /* Initializing bit fields is slow. We speed it up by using a union. */
109 struct user_desc desc;
110 unsigned int vals[4];
114 /* Get the thread descriptor definition. */
117 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
118 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
119 struct pthread even when not linked with -lpthread. */
120 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
122 /* Alignment requirements for the initial TCB. */
123 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
125 /* This is the size of the TCB. */
126 # define TLS_TCB_SIZE sizeof (struct pthread)
128 /* Alignment requirements for the TCB. */
129 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
131 /* The TCB can have any size and the memory following the address the
132 thread pointer points to is unspecified. Allocate the TCB there. */
133 # define TLS_TCB_AT_TP 1
136 /* Install the dtv pointer. The pointer passed is to the element with
137 index -1 which contain the length. */
138 # define INSTALL_DTV(descr, dtvp) \
139 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
141 /* Install new dtv for current thread. */
142 # define INSTALL_NEW_DTV(dtvp) \
143 ({ struct pthread *__pd; \
144 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
146 /* Return dtv of given thread descriptor. */
147 # define GET_DTV(descr) \
148 (((tcbhead_t *) (descr))->dtv)
150 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
151 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
153 /* Macros to load from and store into segment registers. */
155 # define TLS_GET_GS() \
156 ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
159 # define TLS_SET_GS(val) \
160 __asm__ ("movw %w0, %%gs" :: "q" (val))
164 # ifndef __NR_set_thread_area
165 # define __NR_set_thread_area 243
167 # ifndef TLS_FLAG_WRITABLE
168 # define TLS_FLAG_WRITABLE 0x00000001
171 // XXX Enable for the real world.
173 # ifndef __ASSUME_SET_THREAD_AREA
174 # error "we need set_thread_area"
179 # define TLS_EBX_ARG "r"
180 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
182 # define TLS_EBX_ARG "b"
183 # define TLS_LOAD_EBX
186 #if defined NEED_DL_SYSINFO
187 # define INIT_SYSINFO \
188 _head->sysinfo = GLRO(dl_sysinfo)
190 # define INIT_SYSINFO
195 # define LOCK_PREFIX /* nothing */
197 # define LOCK_PREFIX "lock;"
201 /* Code to initially initialize the thread pointer. This might need
202 special attention since 'errno' is not yet available and if the
203 operation can cause a failure 'errno' must not be touched. */
204 # define TLS_INIT_TP(thrdescr, secondcall) \
205 ({ void *_thrdescr = (thrdescr); \
206 tcbhead_t *_head = _thrdescr; \
207 union user_desc_init _segdescr; \
210 _head->tcb = _thrdescr; \
211 /* For now the thread descriptor is at the same address. */ \
212 _head->self = _thrdescr; \
213 /* New syscall handling support. */ \
216 /* The 'entry_number' field. Let the kernel pick a value. */ \
218 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
220 _segdescr.vals[0] = -1; \
221 /* The 'base_addr' field. Pointer to the TCB. */ \
222 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
223 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
224 _segdescr.vals[2] = 0xfffff; \
225 /* Collapsed value of the bitfield: \
228 .read_exec_only = 0 \
229 .limit_in_pages = 1 \
230 .seg_not_present = 0 \
232 _segdescr.vals[3] = 0x51; \
234 /* Install the TLS. */ \
235 __asm__ volatile (TLS_LOAD_EBX \
238 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
239 : "0" (__NR_set_thread_area), \
240 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
243 /* We know the index in the GDT, now load the segment register. \
244 The use of the GDT is described by the value 3 in the lower \
245 three bits of the segment descriptor value. \
247 Note that we have to do this even if the numeric value of \
248 the descriptor does not change. Loading the segment register \
249 causes the segment information from the GDT to be loaded \
250 which is necessary since we have changed it. */ \
251 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
253 _result == 0 ? NULL \
254 : "set_thread_area failed when setting up thread-local storage\n"; })
257 /* Return the address of the dtv for the current thread. */
258 # define THREAD_DTV() \
259 ({ struct pthread *__pd; \
260 THREAD_GETMEM (__pd, header.dtv); })
263 /* Return the thread descriptor for the current thread.
265 The contained asm must *not* be marked volatile since otherwise
267 pthread_descr self = thread_self();
268 do not get optimized away. */
269 # define THREAD_SELF \
270 ({ struct pthread *__self; \
271 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
272 : "i" (offsetof (struct pthread, header.self))); \
275 /* Magic for libthread_db to know how to do THREAD_SELF. */
276 # define DB_THREAD_SELF \
277 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
278 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
281 /* Read member of the thread descriptor directly. */
282 # define THREAD_GETMEM(descr, member) \
283 ({ __typeof (descr->member) __value; \
284 if (sizeof (__value) == 1) \
285 __asm__ volatile ("movb %%gs:%P2,%b0" \
287 : "0" (0), "i" (offsetof (struct pthread, member))); \
288 else if (sizeof (__value) == 4) \
289 __asm__ volatile ("movl %%gs:%P1,%0" \
291 : "i" (offsetof (struct pthread, member))); \
294 if (sizeof (__value) != 8) \
295 /* There should not be any value with a size other than 1, \
299 __asm__ volatile ("movl %%gs:%P1,%%eax\n\t" \
300 "movl %%gs:%P2,%%edx" \
302 : "i" (offsetof (struct pthread, member)), \
303 "i" (offsetof (struct pthread, member) + 4)); \
308 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
309 # define THREAD_GETMEM_NC(descr, member, idx) \
310 ({ __typeof (descr->member[0]) __value; \
311 if (sizeof (__value) == 1) \
312 __asm__ volatile ("movb %%gs:%P2(%3),%b0" \
314 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
316 else if (sizeof (__value) == 4) \
317 __asm__ volatile ("movl %%gs:%P1(,%2,4),%0" \
319 : "i" (offsetof (struct pthread, member[0])), \
323 if (sizeof (__value) != 8) \
324 /* There should not be any value with a size other than 1, \
328 __asm__ volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
329 "movl %%gs:4+%P1(,%2,8),%%edx" \
331 : "i" (offsetof (struct pthread, member[0])), \
337 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
338 # define THREAD_SETMEM(descr, member, value) \
339 ({ if (sizeof (descr->member) == 1) \
340 __asm__ volatile ("movb %b0,%%gs:%P1" : \
342 "i" (offsetof (struct pthread, member))); \
343 else if (sizeof (descr->member) == 4) \
344 __asm__ volatile ("movl %0,%%gs:%P1" : \
346 "i" (offsetof (struct pthread, member))); \
349 if (sizeof (descr->member) != 8) \
350 /* There should not be any value with a size other than 1, \
354 __asm__ volatile ("movl %%eax,%%gs:%P1\n\t" \
355 "movl %%edx,%%gs:%P2" : \
357 "i" (offsetof (struct pthread, member)), \
358 "i" (offsetof (struct pthread, member) + 4)); \
362 /* Set member of the thread descriptor directly. */
363 # define THREAD_SETMEM_NC(descr, member, idx, value) \
364 ({ if (sizeof (descr->member[0]) == 1) \
365 __asm__ volatile ("movb %b0,%%gs:%P1(%2)" : \
367 "i" (offsetof (struct pthread, member)), \
369 else if (sizeof (descr->member[0]) == 4) \
370 __asm__ volatile ("movl %0,%%gs:%P1(,%2,4)" : \
372 "i" (offsetof (struct pthread, member)), \
376 if (sizeof (descr->member[0]) != 8) \
377 /* There should not be any value with a size other than 1, \
381 __asm__ volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
382 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
384 "i" (offsetof (struct pthread, member)), \
389 /* Atomic compare and exchange on TLS, returning old value. */
390 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
391 ({ __typeof (descr->member) __ret; \
392 __typeof (oldval) __old = (oldval); \
393 if (sizeof (descr->member) == 4) \
394 __asm__ volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
396 : "0" (__old), "r" (newval), \
397 "i" (offsetof (struct pthread, member))); \
399 /* Not necessary for other sizes in the moment. */ \
404 /* Atomic logical and. */
405 #define THREAD_ATOMIC_AND(descr, member, val) \
406 (void) ({ if (sizeof ((descr)->member) == 4) \
407 __asm__ volatile (LOCK_PREFIX "andl %1, %%gs:%P0" \
408 :: "i" (offsetof (struct pthread, member)), \
411 /* Not necessary for other sizes in the moment. */ \
415 /* Atomic set bit. */
416 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
417 (void) ({ if (sizeof ((descr)->member) == 4) \
418 __asm__ volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
419 :: "i" (offsetof (struct pthread, member)), \
420 "ir" (1 << (bit))); \
422 /* Not necessary for other sizes in the moment. */ \
426 /* Call the user-provided thread function. */
427 #define CALL_THREAD_FCT(descr) \
429 int __ignore1, __ignore2; \
430 __asm__ volatile ("pushl %%eax\n\t" \
433 "pushl %%gs:%P4\n\t" \
434 "call *%%gs:%P3\n\t" \
436 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
437 : "i" (offsetof (struct pthread, start_routine)), \
438 "i" (offsetof (struct pthread, arg))); \
442 /* Set the stack guard field in TCB head. */
443 #define THREAD_SET_STACK_GUARD(value) \
444 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
445 #define THREAD_COPY_STACK_GUARD(descr) \
446 ((descr)->header.stack_guard \
447 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
450 /* Set the pointer guard field in the TCB head. */
451 #define THREAD_SET_POINTER_GUARD(value) \
452 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
453 #define THREAD_COPY_POINTER_GUARD(descr) \
454 ((descr)->header.pointer_guard \
455 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
458 /* Get and set the global scope generation counter in the TCB head. */
459 #define THREAD_GSCOPE_FLAG_UNUSED 0
460 #define THREAD_GSCOPE_FLAG_USED 1
461 #define THREAD_GSCOPE_FLAG_WAIT 2
462 #define THREAD_GSCOPE_RESET_FLAG() \
465 __asm__ volatile ("xchgl %0, %%gs:%P1" \
467 : "i" (offsetof (struct pthread, header.gscope_flag)), \
468 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
469 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
470 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
473 #define THREAD_GSCOPE_SET_FLAG() \
474 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
475 #define THREAD_GSCOPE_WAIT() \
476 GL(dl_wait_lookup_done) ()
478 #endif /* __ASSEMBLER__ */