1 /* Definition for thread-local data handling. nptl/i386 version.
2 Copyright (C) 2002,2003,2004,2005,2006,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
23 #include <dl-sysdep.h>
31 # include <kernel-features.h>
34 /* Type for the dtv. */
48 void *tcb
; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
51 void *self
; /* Pointer to the thread descriptor. */
54 uintptr_t stack_guard
;
55 uintptr_t pointer_guard
;
57 #ifndef __ASSUME_PRIVATE_FUTEX
62 # define TLS_MULTIPLE_THREADS_IN_TCB 1
64 #else /* __ASSEMBLER__ */
65 # include <tcb-offsets.h>
69 /* We require TLS support in the tools. */
70 #ifndef HAVE_TLS_SUPPORT
71 # error "TLS support is required."
74 /* Alignment requirement for the stack. For IA-32 this is governed by
75 the SSE memory functions. */
76 #define STACK_ALIGN 16
79 /* Get system call information. */
82 /* The old way: using LDT. */
84 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
87 unsigned int entry_number
;
88 unsigned long int base_addr
;
90 unsigned int seg_32bit
:1;
91 unsigned int contents
:2;
92 unsigned int read_exec_only
:1;
93 unsigned int limit_in_pages
:1;
94 unsigned int seg_not_present
:1;
95 unsigned int useable
:1;
96 unsigned int empty
:25;
99 /* Initializing bit fields is slow. We speed it up by using a union. */
102 struct user_desc desc
;
103 unsigned int vals
[4];
107 /* Get the thread descriptor definition. */
108 # include <nptl/descr.h>
110 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
111 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
112 struct pthread even when not linked with -lpthread. */
113 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
115 /* Alignment requirements for the initial TCB. */
116 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
118 /* This is the size of the TCB. */
119 # define TLS_TCB_SIZE sizeof (struct pthread)
121 /* Alignment requirements for the TCB. */
122 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
124 /* The TCB can have any size and the memory following the address the
125 thread pointer points to is unspecified. Allocate the TCB there. */
126 # define TLS_TCB_AT_TP 1
129 /* Install the dtv pointer. The pointer passed is to the element with
130 index -1 which contain the length. */
131 # define INSTALL_DTV(descr, dtvp) \
132 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
134 /* Install new dtv for current thread. */
135 # define INSTALL_NEW_DTV(dtvp) \
136 ({ struct pthread *__pd; \
137 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
139 /* Return dtv of given thread descriptor. */
140 # define GET_DTV(descr) \
141 (((tcbhead_t *) (descr))->dtv)
143 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
144 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
146 /* Macros to load from and store into segment registers. */
148 # define TLS_GET_GS() \
149 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
152 # define TLS_SET_GS(val) \
153 __asm ("movw %w0, %%gs" :: "q" (val))
157 # ifndef __NR_set_thread_area
158 # define __NR_set_thread_area 243
160 # ifndef TLS_FLAG_WRITABLE
161 # define TLS_FLAG_WRITABLE 0x00000001
164 // XXX Enable for the real world.
166 # ifndef __ASSUME_SET_THREAD_AREA
167 # error "we need set_thread_area"
172 # define TLS_EBX_ARG "r"
173 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
175 # define TLS_EBX_ARG "b"
176 # define TLS_LOAD_EBX
179 #if defined NEED_DL_SYSINFO
180 # define INIT_SYSINFO \
181 _head->sysinfo = GLRO(dl_sysinfo)
183 # define INIT_SYSINFO
188 # define LOCK_PREFIX /* nothing */
190 # define LOCK_PREFIX "lock;"
194 #include <nacl_syscalls.h>
196 /* Code to initially initialize the thread pointer. This might need
197 special attention since 'errno' is not yet available and if the
198 operation can cause a failure 'errno' must not be touched. */
199 # define TLS_INIT_TP(thrdescr, secondcall) \
200 ({ void *_thrdescr = (thrdescr); \
201 tcbhead_t *_head = _thrdescr; \
204 _head->tcb = _thrdescr; \
205 /* For now the thread descriptor is at the same address. */ \
206 _head->self = _thrdescr; \
207 /* New syscall handling support. */ \
209 static int (*nacl_tls_init)(void const *buf, size_t size) = \
210 NACL_SYSCALL_ADDR(NACL_sys_tls_init); \
211 _result = nacl_tls_init (_thrdescr, 0x1000); \
212 _result == 0 ? NULL \
213 : "tls_init failed when setting up thread-local storage\n"; })
216 /* Return the address of the dtv for the current thread. */
217 # define THREAD_DTV() \
218 ({ struct pthread *__pd; \
219 THREAD_GETMEM (__pd, header.dtv); })
222 /* Return the thread descriptor for the current thread.
224 The contained asm must *not* be marked volatile since otherwise
226 pthread_descr self = thread_self();
227 do not get optimized away. */
228 # define THREAD_SELF \
229 ({ struct pthread *__self; \
230 asm ("movl %%gs:%c1,%0" : "=r" (__self) \
231 : "i" (offsetof (struct pthread, header.self))); \
234 /* Magic for libthread_db to know how to do THREAD_SELF. */
235 # define DB_THREAD_SELF \
236 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
237 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
240 /* Read member of the thread descriptor directly. */
241 # define THREAD_GETMEM(descr, member) \
242 ({ __typeof (descr->member) __value; \
243 if (sizeof (__value) == 1) \
244 asm volatile ("movb %%gs:%P2,%b0" \
246 : "0" (0), "i" (offsetof (struct pthread, member))); \
247 else if (sizeof (__value) == 4) \
248 asm volatile ("movl %%gs:%P1,%0" \
250 : "i" (offsetof (struct pthread, member))); \
253 if (sizeof (__value) != 8) \
254 /* There should not be any value with a size other than 1, \
258 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
259 "movl %%gs:%P2,%%edx" \
261 : "i" (offsetof (struct pthread, member)), \
262 "i" (offsetof (struct pthread, member) + 4)); \
267 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
268 # define THREAD_GETMEM_NC(descr, member, idx) \
269 ({ __typeof (descr->member[0]) __value; \
270 if (sizeof (__value) == 1) \
271 asm volatile ("movb %%gs:%P2(%3),%b0" \
273 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
275 else if (sizeof (__value) == 4) \
276 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
278 : "i" (offsetof (struct pthread, member[0])), \
282 if (sizeof (__value) != 8) \
283 /* There should not be any value with a size other than 1, \
287 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
288 "movl %%gs:4+%P1(,%2,8),%%edx" \
290 : "i" (offsetof (struct pthread, member[0])), \
296 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
297 # define THREAD_SETMEM(descr, member, value) \
298 ({ if (sizeof (descr->member) == 1) \
299 asm volatile ("movb %b0,%%gs:%P1" : \
301 "i" (offsetof (struct pthread, member))); \
302 else if (sizeof (descr->member) == 4) \
303 asm volatile ("movl %0,%%gs:%P1" : \
305 "i" (offsetof (struct pthread, member))); \
308 if (sizeof (descr->member) != 8) \
309 /* There should not be any value with a size other than 1, \
313 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
314 "movl %%edx,%%gs:%P2" : \
316 "i" (offsetof (struct pthread, member)), \
317 "i" (offsetof (struct pthread, member) + 4)); \
321 /* Set member of the thread descriptor directly. */
322 # define THREAD_SETMEM_NC(descr, member, idx, value) \
323 ({ if (sizeof (descr->member[0]) == 1) \
324 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
326 "i" (offsetof (struct pthread, member)), \
328 else if (sizeof (descr->member[0]) == 4) \
329 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
331 "i" (offsetof (struct pthread, member)), \
335 if (sizeof (descr->member[0]) != 8) \
336 /* There should not be any value with a size other than 1, \
340 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
341 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
343 "i" (offsetof (struct pthread, member)), \
348 /* Atomic compare and exchange on TLS, returning old value. */
349 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
350 ({ __typeof (descr->member) __ret; \
351 __typeof (oldval) __old = (oldval); \
352 if (sizeof (descr->member) == 4) \
353 asm volatile (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
355 : "0" (__old), "r" (newval), \
356 "i" (offsetof (struct pthread, member))); \
358 /* Not necessary for other sizes in the moment. */ \
363 /* Atomic set bit. */
364 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
365 (void) ({ if (sizeof ((descr)->member) == 4) \
366 asm volatile (LOCK_PREFIX "orl %1, %%gs:%P0" \
367 :: "i" (offsetof (struct pthread, member)), \
368 "ir" (1 << (bit))); \
370 /* Not necessary for other sizes in the moment. */ \
374 /* Call the user-provided thread function. */
375 #define CALL_THREAD_FCT(descr) \
377 int __ignore1, __ignore2; \
378 asm volatile ("pushl %%eax\n\t" \
381 "pushl %%gs:%P4\n\t" \
382 "call *%%gs:%P3\n\t" \
384 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
385 : "i" (offsetof (struct pthread, start_routine)), \
386 "i" (offsetof (struct pthread, arg))); \
390 /* Set the stack guard field in TCB head. */
391 #define THREAD_SET_STACK_GUARD(value) \
392 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
393 #define THREAD_COPY_STACK_GUARD(descr) \
394 ((descr)->header.stack_guard \
395 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
398 /* Set the pointer guard field in the TCB head. */
399 #define THREAD_SET_POINTER_GUARD(value) \
400 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
401 #define THREAD_COPY_POINTER_GUARD(descr) \
402 ((descr)->header.pointer_guard \
403 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
406 /* Get and set the global scope generation counter in the TCB head. */
407 #define THREAD_GSCOPE_FLAG_UNUSED 0
408 #define THREAD_GSCOPE_FLAG_USED 1
409 #define THREAD_GSCOPE_FLAG_WAIT 2
410 #define THREAD_GSCOPE_RESET_FLAG() \
413 asm volatile ("xchgl %0, %%gs:%P1" \
415 : "i" (offsetof (struct pthread, header.gscope_flag)), \
416 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
417 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
418 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
421 #define THREAD_GSCOPE_SET_FLAG() \
422 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
423 #define THREAD_GSCOPE_WAIT() \
424 GL(dl_wait_lookup_done) ()
426 #endif /* __ASSEMBLER__ */