]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/linuxthreads/sysdeps/i386/tls.h
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / linuxthreads / sysdeps / i386 / tls.h
1 /* Definition for thread-local data handling.  linuxthreads/i386 version.
2    Copyright (C) 2002, 2003, 2004, 2005 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, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #ifndef _TLS_H
20 #define _TLS_H
21
22 # include <pt-machine.h>
23
24 #ifndef __ASSEMBLER__
25 # include <stdbool.h>
26 # include <stddef.h>
27 # include <stdint.h>
28
29 /* Type for the dtv.  */
30 typedef union dtv
31 {
32   size_t counter;
33   struct
34   {
35     void *val;
36     bool is_static;
37   } pointer;
38 } dtv_t;
39
40
41 typedef struct
42 {
43   void *tcb;            /* Pointer to the TCB.  Not necessary the
44                            thread descriptor used by libpthread.  */
45   dtv_t *dtv;
46   void *self;           /* Pointer to the thread descriptor.  */
47   int multiple_threads;
48   uintptr_t sysinfo;
49 } tcbhead_t;
50
51 #else /* __ASSEMBLER__ */
52 # include <tcb-offsets.h>
53 #endif
54
55 /* We can support TLS only if the floating-stack support is available.
56    However, we want to compile in the support and test at runtime whether
57    the running kernel can support it or not.  To avoid bothering with the
58    TLS support code at all, use configure --without-tls.
59
60    We need USE_TLS to be consistently defined, for ldsodefs.h conditionals.
61    But some of the code below can cause problems in building libpthread
62    (e.g. useldt.h will defined FLOATING_STACKS when it shouldn't).  */
63
64 #if defined HAVE_TLS_SUPPORT \
65     && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
66
67 /* Signal that TLS support is available.  */
68 # define USE_TLS        1
69
70 # ifndef __ASSEMBLER__
71 /* Get system call information.  */
72 #  include <sysdep.h>
73
74
75 /* Get the thread descriptor definition.  */
76 #  include <linuxthreads/descr.h>
77
78 /* This is the size of the initial TCB.  */
79 #  define TLS_INIT_TCB_SIZE sizeof (tcbhead_t)
80
81 /* Alignment requirements for the initial TCB.  */
82 #  define TLS_INIT_TCB_ALIGN __alignof__ (tcbhead_t)
83
84 /* This is the size of the TCB.  */
85 #  define TLS_TCB_SIZE sizeof (struct _pthread_descr_struct)
86
87 /* Alignment requirements for the TCB.  */
88 #  define TLS_TCB_ALIGN __alignof__ (struct _pthread_descr_struct)
89
90 /* The TCB can have any size and the memory following the address the
91    thread pointer points to is unspecified.  Allocate the TCB there.  */
92 #  define TLS_TCB_AT_TP 1
93
94
95 /* Install the dtv pointer.  The pointer passed is to the element with
96    index -1 which contain the length.  */
97 #  define INSTALL_DTV(descr, dtvp) \
98   ((tcbhead_t *) (descr))->dtv = (dtvp) + 1
99
100 /* Install new dtv for current thread.  */
101 #  define INSTALL_NEW_DTV(dtv) \
102   ({ struct _pthread_descr_struct *__descr;                                   \
103      THREAD_SETMEM (__descr, p_header.data.dtvp, (dtv)); })
104
105 /* Return dtv of given thread descriptor.  */
106 #  define GET_DTV(descr) \
107   (((tcbhead_t *) (descr))->dtv)
108
109 #  ifdef __PIC__
110 #   define TLS_EBX_ARG "r"
111 #   define TLS_LOAD_EBX "xchgl %3, %%ebx\n\t"
112 #  else
113 #   define TLS_EBX_ARG "b"
114 #   define TLS_LOAD_EBX
115 #  endif
116
117 #  if !defined IS_IN_libpthread && !defined DO_MODIFY_LDT
118 #   include "useldt.h"          /* For the structure.  */
119 #  endif
120 #  if __ASSUME_LDT_WORKS > 0
121 #   define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit) (doit) /* Nothing to check.  */
122 #  else
123 #   define TLS_DO_MODIFY_LDT_KERNEL_CHECK(doit)                               \
124   (__builtin_expect (GLRO(dl_osversion) < 131939, 0)                          \
125    ? "kernel too old for thread-local storage support\n"                              \
126    : (doit))
127 #  endif
128
129 #  define TLS_DO_MODIFY_LDT(descr, nr)                                        \
130 TLS_DO_MODIFY_LDT_KERNEL_CHECK(                                               \
131 ({                                                                            \
132   struct modify_ldt_ldt_s ldt_entry =                                         \
133     { nr, (unsigned long int) (descr), 0xfffff /* 4GB in pages */,            \
134       1, 0, 0, 1, 0, 1, 0 };                                                  \
135   int result;                                                                 \
136   __asm__ __volatile__ (TLS_LOAD_EBX                                                  \
137                 "int $0x80\n\t"                                               \
138                 TLS_LOAD_EBX                                                  \
139                 : "=a" (result)                                               \
140                 : "0" (__NR_modify_ldt),                                      \
141                 /* The extra argument with the "m" constraint is necessary    \
142                    to let the compiler know that we are accessing LDT_ENTRY   \
143                    here.  */                                                  \
144                 "m" (ldt_entry), TLS_EBX_ARG (1), "c" (&ldt_entry),           \
145                 "d" (sizeof (ldt_entry)));                                    \
146   __builtin_expect (result, 0) == 0                                           \
147   ? ({ __asm__ ("movw %w0, %%gs" : : "q" ((nr) * 8 + 7)); NULL; })                    \
148   : "cannot set up LDT for thread-local storage\n";                           \
149 }))
150
151 #  define TLS_DO_SET_THREAD_AREA(descr, secondcall)                           \
152 ({                                                                            \
153   struct modify_ldt_ldt_s ldt_entry =                                         \
154     { -1, (unsigned long int) (descr), 0xfffff /* 4GB in pages */,            \
155       1, 0, 0, 1, 0, 1, 0 };                                                  \
156   int result;                                                                 \
157   if (secondcall)                                                             \
158     ldt_entry.entry_number = ({ int _gs;                                      \
159                                 __asm__ ("movw %%gs, %w0" : "=q" (_gs));              \
160                                 (_gs & 0xffff) >> 3; });                      \
161   __asm__ __volatile__ (TLS_LOAD_EBX                                                  \
162                 "int $0x80\n\t"                                               \
163                 TLS_LOAD_EBX                                                  \
164                 : "=a" (result), "=m" (ldt_entry.entry_number)                \
165                 : "0" (__NR_set_thread_area),                                 \
166                 /* The extra argument with the "m" constraint is necessary    \
167                    to let the compiler know that we are accessing LDT_ENTRY   \
168                    here.  */                                                  \
169                 TLS_EBX_ARG (&ldt_entry), "m" (ldt_entry));                   \
170   if (__builtin_expect (result, 0) == 0)                                      \
171     __asm__ ("movw %w0, %%gs" : : "q" (ldt_entry.entry_number * 8 + 3));              \
172   result;                                                                     \
173 })
174
175 #  ifdef __ASSUME_SET_THREAD_AREA_SYSCALL
176 #   define TLS_SETUP_GS_SEGMENT(descr, secondcall)                            \
177   (TLS_DO_SET_THREAD_AREA (descr, secondcall)                                 \
178    ? "set_thread_area failed when setting up thread-local storage\n" : NULL)
179 #  elif defined __NR_set_thread_area
180 #   define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
181   (TLS_DO_SET_THREAD_AREA (descr, secondcall)                                 \
182    ? TLS_DO_MODIFY_LDT (descr, 0) : NULL)
183 #  else
184 #   define TLS_SETUP_GS_SEGMENT(descr, secondcall) \
185   TLS_DO_MODIFY_LDT ((descr), 0)
186 #  endif
187
188 #if defined NEED_DL_SYSINFO
189 # define INIT_SYSINFO \
190   head->sysinfo = GLRO(dl_sysinfo)
191 #else
192 # define INIT_SYSINFO
193 #endif
194
195 /* Code to initially initialize the thread pointer.  This might need
196    special attention since 'errno' is not yet available and if the
197    operation can cause a failure 'errno' must not be touched.
198
199    The value of this macro is null if successful, or an error string.  */
200 #  define TLS_INIT_TP(descr, secondcall)                                      \
201   ({                                                                          \
202     void *_descr = (descr);                                                   \
203     tcbhead_t *head = _descr;                                                 \
204                                                                               \
205     head->tcb = _descr;                                                       \
206     /* For now the thread descriptor is at the same address.  */              \
207     head->self = _descr;                                                      \
208                                                                               \
209     INIT_SYSINFO;                                                             \
210     TLS_SETUP_GS_SEGMENT (_descr, secondcall);                                \
211   })
212
213 /* Indicate that dynamic linker shouldn't try to initialize TLS even
214    when no PT_TLS segments are found in the program and libraries
215    it is linked against.  */
216 #  define TLS_INIT_TP_EXPENSIVE 1
217
218 /* Return the address of the dtv for the current thread.  */
219 #  define THREAD_DTV() \
220   ({ struct _pthread_descr_struct *__descr;                                   \
221      THREAD_GETMEM (__descr, p_header.data.dtvp); })
222
223 # endif /* HAVE_TLS_SUPPORT && (FLOATING_STACKS || !IS_IN_libpthread) */
224 #endif /* __ASSEMBLER__ */
225
226 #endif  /* tls.h */