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