1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002, 2003, 2004 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
23 #include <dl-sysdep.h>
31 /* Type for the dtv. */
41 void *tcb
; /* Pointer to the TCB. Not necessary the
42 thread descriptor used by libpthread. */
44 void *self
; /* Pointer to the thread descriptor. */
49 # define TLS_MULTIPLE_THREADS_IN_TCB 1
51 #else /* __ASSEMBLER__ */
52 # include <tcb-offsets.h>
56 /* We require TLS support in the tools. */
57 #ifndef HAVE_TLS_SUPPORT
58 # error "TLS support is required."
61 /* Signal that TLS support is available. */
64 /* Alignment requirement for the stack. For IA-32 this is governed by
65 the SSE memory functions. */
66 #define STACK_ALIGN 16
69 /* Get system call information. */
72 /* The old way: using LDT. */
74 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
77 unsigned int entry_number
;
78 unsigned long int base_addr
;
80 unsigned int seg_32bit
:1;
81 unsigned int contents
:2;
82 unsigned int read_exec_only
:1;
83 unsigned int limit_in_pages
:1;
84 unsigned int seg_not_present
:1;
85 unsigned int useable
:1;
86 unsigned int empty
:25;
89 /* Initializing bit fields is slow. We speed it up by using a union. */
92 struct user_desc desc
;
97 /* Get the thread descriptor definition. */
98 # include <nptl/descr.h>
100 /* This is the size of the initial TCB. */
101 # define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
103 /* Alignment requirements for the initial TCB. */
104 # define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
106 /* This is the size of the TCB. */
107 # define TLS_TCB_SIZE sizeof (struct pthread)
109 /* Alignment requirements for the TCB. */
110 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
112 /* The TCB can have any size and the memory following the address the
113 thread pointer points to is unspecified. Allocate the TCB there. */
114 # define TLS_TCB_AT_TP 1
117 /* Install the dtv pointer. The pointer passed is to the element with
118 index -1 which contain the length. */
119 # define INSTALL_DTV(descr, dtvp) \
120 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
122 /* Install new dtv for current thread. */
123 # define INSTALL_NEW_DTV(dtvp) \
124 ({ struct pthread *__pd; \
125 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
127 /* Return dtv of given thread descriptor. */
128 # define GET_DTV(descr) \
129 (((tcbhead_t *) (descr))->dtv)
131 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
132 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
134 /* Macros to load from and store into segment registers. */
136 # define TLS_GET_GS() \
137 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
140 # define TLS_SET_GS(val) \
141 __asm ("movw %w0, %%gs" :: "q" (val))
145 # ifndef __NR_set_thread_area
146 # define __NR_set_thread_area 243
148 # ifndef TLS_FLAG_WRITABLE
149 # define TLS_FLAG_WRITABLE 0x00000001
152 // XXX Enable for the real world.
154 # ifndef __ASSUME_SET_THREAD_AREA
155 # error "we need set_thread_area"
160 # define TLS_EBX_ARG "r"
161 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
163 # define TLS_EBX_ARG "b"
164 # define TLS_LOAD_EBX
167 #if defined NEED_DL_SYSINFO
168 # define INIT_SYSINFO \
169 _head->sysinfo = GLRO(dl_sysinfo)
171 # define INIT_SYSINFO
176 # define LOCK_PREFIX /* nothing */
178 # define LOCK_PREFIX "lock;"
182 /* Code to initially initialize the thread pointer. This might need
183 special attention since 'errno' is not yet available and if the
184 operation can cause a failure 'errno' must not be touched. */
185 # define TLS_INIT_TP(thrdescr, secondcall) \
186 ({ void *_thrdescr = (thrdescr); \
187 tcbhead_t *_head = _thrdescr; \
188 union user_desc_init _segdescr; \
191 _head->tcb = _thrdescr; \
192 /* For now the thread descriptor is at the same address. */ \
193 _head->self = _thrdescr; \
194 /* New syscall handling support. */ \
197 /* The 'entry_number' field. Let the kernel pick a value. */ \
199 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
201 _segdescr.vals[0] = -1; \
202 /* The 'base_addr' field. Pointer to the TCB. */ \
203 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
204 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
205 _segdescr.vals[2] = 0xfffff; \
206 /* Collapsed value of the bitfield: \
209 .read_exec_only = 0 \
210 .limit_in_pages = 1 \
211 .seg_not_present = 0 \
213 _segdescr.vals[3] = 0x51; \
215 /* Install the TLS. */ \
216 asm volatile (TLS_LOAD_EBX \
219 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
220 : "0" (__NR_set_thread_area), \
221 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
224 /* We know the index in the GDT, now load the segment register. \
225 The use of the GDT is described by the value 3 in the lower \
226 three bits of the segment descriptor value. \
228 Note that we have to do this even if the numeric value of \
229 the descriptor does not change. Loading the segment register \
230 causes the segment information from the GDT to be loaded \
231 which is necessary since we have changed it. */ \
232 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
234 _result == 0 ? NULL \
235 : "set_thread_area failed when setting up thread-local storage\n"; })
238 /* Return the address of the dtv for the current thread. */
239 # define THREAD_DTV() \
240 ({ struct pthread *__pd; \
241 THREAD_GETMEM (__pd, header.dtv); })
244 /* Return the thread descriptor for the current thread.
246 The contained asm must *not* be marked volatile since otherwise
248 pthread_descr self = thread_self();
249 do not get optimized away. */
250 # define THREAD_SELF \
251 ({ struct pthread *__self; \
252 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
253 : "i" (offsetof (struct pthread, header.self))); \
256 /* Magic for libthread_db to know how to do THREAD_SELF. */
257 # define DB_THREAD_SELF \
258 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
259 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
262 /* Read member of the thread descriptor directly. */
263 # define THREAD_GETMEM(descr, member) \
264 ({ __typeof (descr->member) __value; \
265 if (sizeof (__value) == 1) \
266 asm volatile ("movb %%gs:%P2,%b0" \
268 : "0" (0), "i" (offsetof (struct pthread, member))); \
269 else if (sizeof (__value) == 4) \
270 asm volatile ("movl %%gs:%P1,%0" \
272 : "i" (offsetof (struct pthread, member))); \
275 if (sizeof (__value) != 8) \
276 /* There should not be any value with a size other than 1, \
280 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
281 "movl %%gs:%P2,%%edx" \
283 : "i" (offsetof (struct pthread, member)), \
284 "i" (offsetof (struct pthread, member) + 4)); \
289 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
290 # define THREAD_GETMEM_NC(descr, member, idx) \
291 ({ __typeof (descr->member[0]) __value; \
292 if (sizeof (__value) == 1) \
293 asm volatile ("movb %%gs:%P2(%3),%b0" \
295 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
297 else if (sizeof (__value) == 4) \
298 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
300 : "i" (offsetof (struct pthread, member[0])), \
304 if (sizeof (__value) != 8) \
305 /* There should not be any value with a size other than 1, \
309 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
310 "movl %%gs:4+%P1(,%2,8),%%edx" \
312 : "i" (offsetof (struct pthread, member[0])), \
318 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
319 # define THREAD_SETMEM(descr, member, value) \
320 ({ if (sizeof (descr->member) == 1) \
321 asm volatile ("movb %b0,%%gs:%P1" : \
323 "i" (offsetof (struct pthread, member))); \
324 else if (sizeof (descr->member) == 4) \
325 asm volatile ("movl %0,%%gs:%P1" : \
327 "i" (offsetof (struct pthread, member))); \
330 if (sizeof (descr->member) != 8) \
331 /* There should not be any value with a size other than 1, \
335 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
336 "movl %%edx,%%gs:%P2" : \
338 "i" (offsetof (struct pthread, member)), \
339 "i" (offsetof (struct pthread, member) + 4)); \
343 /* Set member of the thread descriptor directly. */
344 # define THREAD_SETMEM_NC(descr, member, idx, value) \
345 ({ if (sizeof (descr->member[0]) == 1) \
346 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
348 "i" (offsetof (struct pthread, member)), \
350 else if (sizeof (descr->member[0]) == 4) \
351 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
353 "i" (offsetof (struct pthread, member)), \
357 if (sizeof (descr->member[0]) != 8) \
358 /* There should not be any value with a size other than 1, \
362 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
363 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
365 "i" (offsetof (struct pthread, member)), \
370 /* Atomic compare and exchange on TLS, returning old value. */
371 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
372 ({ __typeof (descr->member) __ret; \
373 __typeof (oldval) __old = (oldval); \
374 if (sizeof (descr->member) == 4) \
375 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
377 : "0" (__old), "r" (newval), \
378 "i" (offsetof (struct pthread, member))); \
380 /* Not necessary for other sizes in the moment. */ \
385 /* Atomic set bit. */
386 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
387 (void) ({ if (sizeof ((descr)->member) == 4) \
388 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
389 :: "i" (offsetof (struct pthread, member)), \
390 "ir" (1 << (bit))); \
392 /* Not necessary for other sizes in the moment. */ \
396 /* Call the user-provided thread function. */
397 #define CALL_THREAD_FCT(descr) \
399 int __ignore1, __ignore2; \
400 asm volatile ("pushl %%eax\n\t" \
403 "pushl %%gs:%P4\n\t" \
404 "call *%%gs:%P3\n\t" \
406 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
407 : "i" (offsetof (struct pthread, start_routine)), \
408 "i" (offsetof (struct pthread, arg))); \
412 #endif /* __ASSEMBLER__ */