Allocate a larger TLS segment, so that initialising libpthread.so works
[glibc/nacl-glibc.git] / nptl / sysdeps / i386 / tls.h
blob403748e64e8d0ceebe2af0212758fe7e601f3203
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
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>
30 # include <sysdep.h>
31 # include <kernel-features.h>
34 /* Type for the dtv. */
35 typedef union dtv
37 size_t counter;
38 struct
40 void *val;
41 bool is_static;
42 } pointer;
43 } dtv_t;
46 typedef struct
48 void *tcb; /* Pointer to the TCB. Not necessarily the
49 thread descriptor used by libpthread. */
50 dtv_t *dtv;
51 void *self; /* Pointer to the thread descriptor. */
52 int multiple_threads;
53 uintptr_t sysinfo;
54 uintptr_t stack_guard;
55 uintptr_t pointer_guard;
56 int gscope_flag;
57 #ifndef __ASSUME_PRIVATE_FUTEX
58 int private_futex;
59 #endif
60 } tcbhead_t;
62 # define TLS_MULTIPLE_THREADS_IN_TCB 1
64 #else /* __ASSEMBLER__ */
65 # include <tcb-offsets.h>
66 #endif
69 /* We require TLS support in the tools. */
70 #ifndef HAVE_TLS_SUPPORT
71 # error "TLS support is required."
72 #endif
74 /* Alignment requirement for the stack. For IA-32 this is governed by
75 the SSE memory functions. */
76 #define STACK_ALIGN 16
78 #ifndef __ASSEMBLER__
79 /* Get system call information. */
80 # include <sysdep.h>
82 /* The old way: using LDT. */
84 /* Structure passed to `modify_ldt', 'set_thread_area', and 'clone' calls. */
85 struct user_desc
87 unsigned int entry_number;
88 unsigned long int base_addr;
89 unsigned int limit;
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. */
100 union user_desc_init
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. */
147 # ifndef TLS_GET_GS
148 # define TLS_GET_GS() \
149 ({ int __seg; __asm ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
150 # endif
151 # ifndef TLS_SET_GS
152 # define TLS_SET_GS(val) \
153 __asm ("movw %w0, %%gs" :: "q" (val))
154 # endif
157 # ifndef __NR_set_thread_area
158 # define __NR_set_thread_area 243
159 # endif
160 # ifndef TLS_FLAG_WRITABLE
161 # define TLS_FLAG_WRITABLE 0x00000001
162 # endif
164 // XXX Enable for the real world.
165 #if 0
166 # ifndef __ASSUME_SET_THREAD_AREA
167 # error "we need set_thread_area"
168 # endif
169 #endif
171 # ifdef __PIC__
172 # define TLS_EBX_ARG "r"
173 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
174 # else
175 # define TLS_EBX_ARG "b"
176 # define TLS_LOAD_EBX
177 # endif
179 #if defined NEED_DL_SYSINFO
180 # define INIT_SYSINFO \
181 _head->sysinfo = GLRO(dl_sysinfo)
182 #else
183 # define INIT_SYSINFO
184 #endif
186 #ifndef LOCK_PREFIX
187 # ifdef UP
188 # define LOCK_PREFIX /* nothing */
189 # else
190 # define LOCK_PREFIX "lock;"
191 # endif
192 #endif
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; \
202 int _result; \
204 _head->tcb = _thrdescr; \
205 /* For now the thread descriptor is at the same address. */ \
206 _head->self = _thrdescr; \
207 /* New syscall handling support. */ \
208 INIT_SYSINFO; \
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
225 assignments like
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))); \
232 __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" \
245 : "=q" (__value) \
246 : "0" (0), "i" (offsetof (struct pthread, member))); \
247 else if (sizeof (__value) == 4) \
248 asm volatile ("movl %%gs:%P1,%0" \
249 : "=r" (__value) \
250 : "i" (offsetof (struct pthread, member))); \
251 else \
253 if (sizeof (__value) != 8) \
254 /* There should not be any value with a size other than 1, \
255 4 or 8. */ \
256 abort (); \
258 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
259 "movl %%gs:%P2,%%edx" \
260 : "=A" (__value) \
261 : "i" (offsetof (struct pthread, member)), \
262 "i" (offsetof (struct pthread, member) + 4)); \
264 __value; })
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" \
272 : "=q" (__value) \
273 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
274 "r" (idx)); \
275 else if (sizeof (__value) == 4) \
276 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
277 : "=r" (__value) \
278 : "i" (offsetof (struct pthread, member[0])), \
279 "r" (idx)); \
280 else \
282 if (sizeof (__value) != 8) \
283 /* There should not be any value with a size other than 1, \
284 4 or 8. */ \
285 abort (); \
287 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
288 "movl %%gs:4+%P1(,%2,8),%%edx" \
289 : "=&A" (__value) \
290 : "i" (offsetof (struct pthread, member[0])), \
291 "r" (idx)); \
293 __value; })
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" : \
300 : "iq" (value), \
301 "i" (offsetof (struct pthread, member))); \
302 else if (sizeof (descr->member) == 4) \
303 asm volatile ("movl %0,%%gs:%P1" : \
304 : "ir" (value), \
305 "i" (offsetof (struct pthread, member))); \
306 else \
308 if (sizeof (descr->member) != 8) \
309 /* There should not be any value with a size other than 1, \
310 4 or 8. */ \
311 abort (); \
313 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
314 "movl %%edx,%%gs:%P2" : \
315 : "A" (value), \
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)" : \
325 : "iq" (value), \
326 "i" (offsetof (struct pthread, member)), \
327 "r" (idx)); \
328 else if (sizeof (descr->member[0]) == 4) \
329 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
330 : "ir" (value), \
331 "i" (offsetof (struct pthread, member)), \
332 "r" (idx)); \
333 else \
335 if (sizeof (descr->member[0]) != 8) \
336 /* There should not be any value with a size other than 1, \
337 4 or 8. */ \
338 abort (); \
340 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
341 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
342 : "A" (value), \
343 "i" (offsetof (struct pthread, member)), \
344 "r" (idx)); \
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" \
354 : "=a" (__ret) \
355 : "0" (__old), "r" (newval), \
356 "i" (offsetof (struct pthread, member))); \
357 else \
358 /* Not necessary for other sizes in the moment. */ \
359 abort (); \
360 __ret; })
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))); \
369 else \
370 /* Not necessary for other sizes in the moment. */ \
371 abort (); })
374 /* Call the user-provided thread function. */
375 #define CALL_THREAD_FCT(descr) \
376 ({ void *__res; \
377 int __ignore1, __ignore2; \
378 asm volatile ("pushl %%eax\n\t" \
379 "pushl %%eax\n\t" \
380 "pushl %%eax\n\t" \
381 "pushl %%gs:%P4\n\t" \
382 "call *%%gs:%P3\n\t" \
383 "addl $16, %%esp" \
384 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
385 : "i" (offsetof (struct pthread, start_routine)), \
386 "i" (offsetof (struct pthread, arg))); \
387 __res; })
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() \
411 do \
412 { int __res; \
413 asm volatile ("xchgl %0, %%gs:%P1" \
414 : "=r" (__res) \
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); \
420 while (0)
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__ */
428 #endif /* tls.h */