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.
4
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.
9
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.
14
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. */
19
20 #ifndef _TLS_H
21 #define _TLS_H 1
22
23 #ifndef __ASSEMBLER__
24 # include <stdbool.h>
25 # include <stddef.h>
26 # include <stdint.h>
27 # include <stdlib.h>
28 //# include <sysdep.h>
29 # include <bits/kernel-features.h>
30
31
32 /* Type for the dtv. */
33 typedef union dtv
34 {
35 size_t counter;
36 struct
37 {
38 void *val;
39 bool is_static;
40 } pointer;
41 } dtv_t;
42
43
44 typedef struct
45 {
46 void *tcb; /* Pointer to the TCB. Not necessarily the
47 thread descriptor used by libpthread. */
48 dtv_t *dtv;
49 void *self; /* Pointer to the thread descriptor. */
50 int multiple_threads;
51 uintptr_t sysinfo;
52 uintptr_t stack_guard;
53 uintptr_t pointer_guard;
54 int gscope_flag;
55 #ifndef __ASSUME_PRIVATE_FUTEX
56 int private_futex;
57 #else
58 int __unused1;
59 #endif
60 /* Reservation of some values for the TM ABI. */
61 void *__private_tm[5];
62 } tcbhead_t;
63
64 # define TLS_MULTIPLE_THREADS_IN_TCB 1
65
66 #else /* __ASSEMBLER__ */
67 # include <tcb-offsets.h>
68 #endif
69
70
71 /* We require TLS support in the tools. */
72 #define HAVE_TLS_SUPPORT
73 #define HAVE___THREAD 1
74 #define HAVE_TLS_MODEL_ATTRIBUTE 1
75
76 /* Signal that TLS support is available. */
77 #define USE_TLS 1
78
79
80 /* Alignment requirement for the stack. For IA-32 this is governed by
81 the SSE memory functions. */
82 #define STACK_ALIGN 16
83
84 #ifndef __ASSEMBLER__
85 /* Get system call information. */
86 //# include <sysdep.h>
87
88 /* The old way: using LDT. */
89
90 struct x86_descr {
91 union {
92 struct {
93 unsigned int a;
94 unsigned int b;
95 };
96 struct {
97 uint16_t limit0;
98 uint16_t base0;
99 unsigned base1: 8, type: 4, s: 1, dpl: 2, p: 1;
100 unsigned limit: 4, avl: 1, l: 1, d: 1, g: 1, base2: 8;
101 };
102 };
103 } __attribute__((packed));
104
105
106
107 /* Get the thread descriptor definition. */
108 # include <descr.h>
109 # include <l4/sys/utcb.h>
110 # include <l4/sys/segment.h>
111
112 /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t),
113 because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole
114 struct pthread even when not linked with -lpthread. */
115 # define TLS_INIT_TCB_SIZE sizeof (struct pthread)
116
117 /* Alignment requirements for the initial TCB. */
118 # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread)
119
120 /* This is the size of the TCB. */
121 # define TLS_TCB_SIZE sizeof (struct pthread)
122
123 /* Alignment requirements for the TCB. */
124 # define TLS_TCB_ALIGN __alignof__ (struct pthread)
125
126 /* The TCB can have any size and the memory following the address the
127 thread pointer points to is unspecified. Allocate the TCB there. */
128 # define TLS_TCB_AT_TP 1
129
130
131 /* Install the dtv pointer. The pointer passed is to the element with
132 index -1 which contain the length. */
133 # define INSTALL_DTV(descr, dtvp) \
134 ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
135
136 /* Install new dtv for current thread. */
137 # define INSTALL_NEW_DTV(dtvp) \
138 ({ struct pthread *__pd; \
139 THREAD_SETMEM (__pd, header.dtv, (dtvp)); })
140
141 /* Return dtv of given thread descriptor. */
142 # define GET_DTV(descr) \
143 (((tcbhead_t *) (descr))->dtv)
144
145 #define THREAD_SELF_SYSINFO THREAD_GETMEM (THREAD_SELF, header.sysinfo)
146 #define THREAD_SYSINFO(pd) ((pd)->header.sysinfo)
147
148 /* Macros to load from and store into segment registers. */
149 # ifndef TLS_GET_GS
150 # define TLS_GET_GS() \
151 ({ int __seg; __asm__ ("movw %%gs, %w0" : "=q" (__seg)); __seg & 0xffff; })
152 # endif
153 # ifndef TLS_SET_GS
154 # define TLS_SET_GS(val) \
155 __asm__ ("movw %w0, %%gs" :: "q" (val))
156 # endif
157
158
159 # ifndef __NR_set_thread_area
160 # define __NR_set_thread_area 243
161 # endif
162 # ifndef TLS_FLAG_WRITABLE
163 # define TLS_FLAG_WRITABLE 0x00000001
164 # endif
165
166 // XXX Enable for the real world.
167 #if 0
168 # ifndef __ASSUME_SET_THREAD_AREA
169 # error "we need set_thread_area"
170 # endif
171 #endif
172
173 # ifdef __PIC__
174 # define TLS_EBX_ARG "r"
175 # define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
176 # else
177 # define TLS_EBX_ARG "b"
178 # define TLS_LOAD_EBX
179 # endif
180
181 #if defined NEED_DL_SYSINFO
182 # define INIT_SYSINFO \
183 _head->sysinfo = GLRO(dl_sysinfo)
184 #else
185 # define INIT_SYSINFO
186 #endif
187
188 #ifndef LOCK_PREFIX
189 # ifdef UP
190 # define LOCK_PREFIX /* nothing */
191 # else
192 # define LOCK_PREFIX "lock;"
193 # endif
194 #endif
195
TLS_INIT_TP(void * thrdescr,int secondcall)196 static inline char const *TLS_INIT_TP(void *thrdescr, int secondcall)
197 {
198 (void)secondcall;
199 tcbhead_t *_head = (tcbhead_t *)thrdescr;
200 struct x86_descr segdesc;
201 long seg;
202
203 _head->tcb = thrdescr;
204 _head->self = thrdescr;
205 INIT_SYSINFO;
206 segdesc.limit0 = 0xffff;
207 segdesc.base0 = ((unsigned long)thrdescr) & 0x0ffff;
208 segdesc.base1 = (((unsigned long)thrdescr) & 0x0ff0000) >> 16;
209 segdesc.type = 2;
210 segdesc.s = 1;
211 segdesc.dpl = 3;
212 segdesc.p = 1;
213 segdesc.limit = 0xf;
214 segdesc.avl = 1;
215 segdesc.d = 1; // 32bit
216 segdesc.g = 1; // pages
217 segdesc.base2 = (((unsigned long)thrdescr) & 0xff000000) >> 24;
218 segdesc.l = 0;
219 seg = fiasco_gdt_set(L4_INVALID_CAP, &segdesc, 8, 0, l4_utcb());
220 if (seg < 0)
221 return "ERROR";
222
223 TLS_SET_GS(seg);
224
225 return NULL;
226 }
227 #if 0
228 /* Code to initially initialize the thread pointer. This might need
229 special attention since 'errno' is not yet available and if the
230 operation can cause a failure 'errno' must not be touched. */
231 # define TLS_INIT_TP(thrdescr, secondcall) \
232 ({ void *_thrdescr = (thrdescr); \
233 tcbhead_t *_head = _thrdescr; \
234 union user_desc_init _segdescr; \
235 int _result; \
236 \
237 _head->tcb = _thrdescr; \
238 /* For now the thread descriptor is at the same address. */ \
239 _head->self = _thrdescr; \
240 /* New syscall handling support. */ \
241 INIT_SYSINFO; \
242 \
243 /* The 'entry_number' field. Let the kernel pick a value. */ \
244 if (secondcall) \
245 _segdescr.vals[0] = TLS_GET_GS () >> 3; \
246 else \
247 _segdescr.vals[0] = -1; \
248 /* The 'base_addr' field. Pointer to the TCB. */ \
249 _segdescr.vals[1] = (unsigned long int) _thrdescr; \
250 /* The 'limit' field. We use 4GB which is 0xfffff pages. */ \
251 _segdescr.vals[2] = 0xfffff; \
252 /* Collapsed value of the bitfield: \
253 .seg_32bit = 1 \
254 .contents = 0 \
255 .read_exec_only = 0 \
256 .limit_in_pages = 1 \
257 .seg_not_present = 0 \
258 .useable = 1 */ \
259 _segdescr.vals[3] = 0x51; \
260 \
261 /* Install the TLS. */ \
262 __asm__ __volatile__ (TLS_LOAD_EBX \
263 "int $0x80\n\t" \
264 TLS_LOAD_EBX \
265 : "=a" (_result), "=m" (_segdescr.desc.entry_number) \
266 : "0" (__NR_set_thread_area), \
267 TLS_EBX_ARG (&_segdescr.desc), "m" (_segdescr.desc)); \
268 \
269 if (_result == 0) \
270 /* We know the index in the GDT, now load the segment register. \
271 The use of the GDT is described by the value 3 in the lower \
272 three bits of the segment descriptor value. \
273 \
274 Note that we have to do this even if the numeric value of \
275 the descriptor does not change. Loading the segment register \
276 causes the segment information from the GDT to be loaded \
277 which is necessary since we have changed it. */ \
278 TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3); \
279 \
280 _result == 0 ? NULL \
281 : "set_thread_area failed when setting up thread-local storage\n"; })
282
283 #endif
284 /* Return the address of the dtv for the current thread. */
285 # define THREAD_DTV() \
286 ({ struct pthread *__pd; \
287 THREAD_GETMEM (__pd, header.dtv); })
288
289
290 /* Return the thread descriptor for the current thread.
291
292 The contained asm must *not* be marked __volatile__ since otherwise
293 assignments like
294 pthread_descr self = thread_self();
295 do not get optimized away. */
296 # define THREAD_SELF \
297 ({ struct pthread *__self; \
298 __asm__ ("movl %%gs:%c1,%0" : "=r" (__self) \
299 : "i" (offsetof (struct pthread, header.self))); \
300 __self;})
301
302 /* Magic for libthread_db to know how to do THREAD_SELF. */
303 # define DB_THREAD_SELF \
304 REGISTER_THREAD_AREA (32, offsetof (struct user_regs_struct, xgs), 3) \
305 REGISTER_THREAD_AREA (64, 26 * 8, 3) /* x86-64's user_regs_struct->gs */
306
307
308 /* Read member of the thread descriptor directly. */
309 # define THREAD_GETMEM(descr, member) \
310 ({ __typeof (descr->member) __value; \
311 if (sizeof (__value) == 1) \
312 __asm__ __volatile__ ("movb %%gs:%P2,%b0" \
313 : "=q" (__value) \
314 : "0" (0), "i" (offsetof (struct pthread, member))); \
315 else if (sizeof (__value) == 4) \
316 __asm__ __volatile__ ("movl %%gs:%P1,%0" \
317 : "=r" (__value) \
318 : "i" (offsetof (struct pthread, member))); \
319 else \
320 { \
321 if (sizeof (__value) != 8) \
322 /* There should not be any value with a size other than 1, \
323 4 or 8. */ \
324 abort (); \
325 \
326 __asm__ __volatile__ ("movl %%gs:%P1,%%eax\n\t" \
327 "movl %%gs:%P2,%%edx" \
328 : "=A" (__value) \
329 : "i" (offsetof (struct pthread, member)), \
330 "i" (offsetof (struct pthread, member) + 4)); \
331 } \
332 __value; })
333
334
335 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
336 # define THREAD_GETMEM_NC(descr, member, idx) \
337 ({ __typeof (descr->member[0]) __value; \
338 if (sizeof (__value) == 1) \
339 __asm__ __volatile__ ("movb %%gs:%P2(%3),%b0" \
340 : "=q" (__value) \
341 : "0" (0), "i" (offsetof (struct pthread, member[0])), \
342 "r" (idx)); \
343 else if (sizeof (__value) == 4) \
344 __asm__ __volatile__ ("movl %%gs:%P1(,%2,4),%0" \
345 : "=r" (__value) \
346 : "i" (offsetof (struct pthread, member[0])), \
347 "r" (idx)); \
348 else \
349 { \
350 if (sizeof (__value) != 8) \
351 /* There should not be any value with a size other than 1, \
352 4 or 8. */ \
353 abort (); \
354 \
355 __asm__ __volatile__ ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
356 "movl %%gs:4+%P1(,%2,8),%%edx" \
357 : "=&A" (__value) \
358 : "i" (offsetof (struct pthread, member[0])), \
359 "r" (idx)); \
360 } \
361 __value; })
362
363
364 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
365 # define THREAD_SETMEM(descr, member, value) \
366 ({ if (sizeof (descr->member) == 1) \
367 __asm__ __volatile__ ("movb %b0,%%gs:%P1" : \
368 : "iq" (value), \
369 "i" (offsetof (struct pthread, member))); \
370 else if (sizeof (descr->member) == 4) \
371 __asm__ __volatile__ ("movl %0,%%gs:%P1" : \
372 : "ir" (value), \
373 "i" (offsetof (struct pthread, member))); \
374 else \
375 { \
376 if (sizeof (descr->member) != 8) \
377 /* There should not be any value with a size other than 1, \
378 4 or 8. */ \
379 abort (); \
380 \
381 __asm__ __volatile__ ("movl %%eax,%%gs:%P1\n\t" \
382 "movl %%edx,%%gs:%P2" : \
383 : "A" (value), \
384 "i" (offsetof (struct pthread, member)), \
385 "i" (offsetof (struct pthread, member) + 4)); \
386 }})
387
388
389 /* Set member of the thread descriptor directly. */
390 # define THREAD_SETMEM_NC(descr, member, idx, value) \
391 ({ if (sizeof (descr->member[0]) == 1) \
392 __asm__ __volatile__ ("movb %b0,%%gs:%P1(%2)" : \
393 : "iq" (value), \
394 "i" (offsetof (struct pthread, member)), \
395 "r" (idx)); \
396 else if (sizeof (descr->member[0]) == 4) \
397 __asm__ __volatile__ ("movl %0,%%gs:%P1(,%2,4)" : \
398 : "ir" (value), \
399 "i" (offsetof (struct pthread, member)), \
400 "r" (idx)); \
401 else \
402 { \
403 if (sizeof (descr->member[0]) != 8) \
404 /* There should not be any value with a size other than 1, \
405 4 or 8. */ \
406 abort (); \
407 \
408 __asm__ __volatile__ ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
409 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
410 : "A" (value), \
411 "i" (offsetof (struct pthread, member)), \
412 "r" (idx)); \
413 }})
414
415
416 /* Atomic compare and exchange on TLS, returning old value. */
417 #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \
418 ({ __typeof (descr->member) __ret; \
419 __typeof (oldval) __old = (oldval); \
420 if (sizeof (descr->member) == 4) \
421 __asm__ __volatile__ (LOCK_PREFIX "cmpxchgl %2, %%gs:%P3" \
422 : "=a" (__ret) \
423 : "0" (__old), "r" (newval), \
424 "i" (offsetof (struct pthread, member))); \
425 else \
426 /* Not necessary for other sizes in the moment. */ \
427 abort (); \
428 __ret; })
429
430
431 /* Atomic logical and. */
432 #define THREAD_ATOMIC_AND(descr, member, val) \
433 (void) ({ if (sizeof ((descr)->member) == 4) \
434 __asm__ __volatile__ (LOCK_PREFIX "andl %1, %%gs:%P0" \
435 :: "i" (offsetof (struct pthread, member)), \
436 "ir" (val)); \
437 else \
438 /* Not necessary for other sizes in the moment. */ \
439 abort (); })
440
441
442 /* Atomic set bit. */
443 #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \
444 (void) ({ if (sizeof ((descr)->member) == 4) \
445 __asm__ __volatile__ (LOCK_PREFIX "orl %1, %%gs:%P0" \
446 :: "i" (offsetof (struct pthread, member)), \
447 "ir" (1 << (bit))); \
448 else \
449 /* Not necessary for other sizes in the moment. */ \
450 abort (); })
451
452
453 /* Call the user-provided thread function. */
454 #define CALL_THREAD_FCT(descr) \
455 ({ void *__res; \
456 int __ignore1, __ignore2; \
457 __asm__ __volatile__ ("pushl %%eax\n\t" \
458 "pushl %%eax\n\t" \
459 "pushl %%eax\n\t" \
460 "pushl %%gs:%P4\n\t" \
461 "call *%%gs:%P3\n\t" \
462 "addl $16, %%esp" \
463 : "=a" (__res), "=c" (__ignore1), "=d" (__ignore2) \
464 : "i" (offsetof (struct pthread, start_routine)), \
465 "i" (offsetof (struct pthread, arg))); \
466 __res; })
467
468
469 /* Set the stack guard field in TCB head. */
470 #define THREAD_SET_STACK_GUARD(value) \
471 THREAD_SETMEM (THREAD_SELF, header.stack_guard, value)
472 #define THREAD_COPY_STACK_GUARD(descr) \
473 ((descr)->header.stack_guard \
474 = THREAD_GETMEM (THREAD_SELF, header.stack_guard))
475
476
477 /* Set the pointer guard field in the TCB head. */
478 #define THREAD_SET_POINTER_GUARD(value) \
479 THREAD_SETMEM (THREAD_SELF, header.pointer_guard, value)
480 #define THREAD_COPY_POINTER_GUARD(descr) \
481 ((descr)->header.pointer_guard \
482 = THREAD_GETMEM (THREAD_SELF, header.pointer_guard))
483
484
485 /* Get and set the global scope generation counter in the TCB head. */
486 #define THREAD_GSCOPE_FLAG_UNUSED 0
487 #define THREAD_GSCOPE_FLAG_USED 1
488 #define THREAD_GSCOPE_FLAG_WAIT 2
489 #define THREAD_GSCOPE_RESET_FLAG() \
490 do \
491 { int __res; \
492 __asm__ __volatile__ ("xchgl %0, %%gs:%P1" \
493 : "=r" (__res) \
494 : "i" (offsetof (struct pthread, header.gscope_flag)), \
495 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
496 if (__res == THREAD_GSCOPE_FLAG_WAIT) \
497 lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE); \
498 } \
499 while (0)
500 #define THREAD_GSCOPE_SET_FLAG() \
501 THREAD_SETMEM (THREAD_SELF, header.gscope_flag, THREAD_GSCOPE_FLAG_USED)
502 #define THREAD_GSCOPE_WAIT() \
503 GL(dl_wait_lookup_done) ()
504
505 #endif /* __ASSEMBLER__ */
506
507 #endif /* tls.h */
508