]>
Commit | Line | Data |
---|---|---|
a3931336 | 1 | /* Definition for thread-local data handling. nptl/x86_64 version. |
f7d78e18 | 2 | Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
a3931336 UD |
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 | ||
a334319f | 23 | #include <asm/prctl.h> /* For ARCH_SET_FS. */ |
a3931336 | 24 | #ifndef __ASSEMBLER__ |
9dcafc55 | 25 | # include <stdbool.h> |
a3931336 UD |
26 | # include <stddef.h> |
27 | # include <stdint.h> | |
33b5d0cc | 28 | # include <stdlib.h> |
a3931336 UD |
29 | |
30 | ||
31 | /* Type for the dtv. */ | |
32 | typedef union dtv | |
33 | { | |
34 | size_t counter; | |
9dcafc55 UD |
35 | struct |
36 | { | |
37 | void *val; | |
38 | bool is_static; | |
39 | } pointer; | |
a3931336 UD |
40 | } dtv_t; |
41 | ||
42 | ||
43 | typedef struct | |
44 | { | |
45 | void *tcb; /* Pointer to the TCB. Not necessary the | |
46 | thread descriptor used by libpthread. */ | |
47 | dtv_t *dtv; | |
48 | void *self; /* Pointer to the thread descriptor. */ | |
bbde8527 | 49 | int multiple_threads; |
35f1e827 UD |
50 | uintptr_t sysinfo; |
51 | uintptr_t stack_guard; | |
a3931336 | 52 | } tcbhead_t; |
bbde8527 | 53 | |
d0369fb8 UD |
54 | #else /* __ASSEMBLER__ */ |
55 | # include <tcb-offsets.h> | |
a3931336 UD |
56 | #endif |
57 | ||
58 | ||
59 | /* We require TLS support in the tools. */ | |
60 | #ifndef HAVE_TLS_SUPPORT | |
61 | # error "TLS support is required." | |
62 | #endif | |
63 | ||
64 | /* Signal that TLS support is available. */ | |
65 | #define USE_TLS 1 | |
66 | ||
67 | /* Alignment requirement for the stack. */ | |
68 | #define STACK_ALIGN 16 | |
69 | ||
70 | ||
71 | #ifndef __ASSEMBLER__ | |
72 | /* Get system call information. */ | |
73 | # include <sysdep.h> | |
74 | ||
a3931336 UD |
75 | |
76 | /* Get the thread descriptor definition. */ | |
77 | # include <nptl/descr.h> | |
78 | ||
bd4f43b4 UD |
79 | #ifndef LOCK_PREFIX |
80 | # ifdef UP | |
81 | # define LOCK_PREFIX /* nothing */ | |
82 | # else | |
83 | # define LOCK_PREFIX "lock;" | |
84 | # endif | |
85 | #endif | |
86 | ||
f7d78e18 UD |
87 | /* This is the size of the initial TCB. Can't be just sizeof (tcbhead_t), |
88 | because NPTL getpid, __libc_alloca_cutoff etc. need (almost) the whole | |
89 | struct pthread even when not linked with -lpthread. */ | |
90 | # define TLS_INIT_TCB_SIZE sizeof (struct pthread) | |
a3931336 UD |
91 | |
92 | /* Alignment requirements for the initial TCB. */ | |
f7d78e18 | 93 | # define TLS_INIT_TCB_ALIGN __alignof__ (struct pthread) |
a3931336 UD |
94 | |
95 | /* This is the size of the TCB. */ | |
96 | # define TLS_TCB_SIZE sizeof (struct pthread) | |
97 | ||
98 | /* Alignment requirements for the TCB. */ | |
99 | # define TLS_TCB_ALIGN __alignof__ (struct pthread) | |
100 | ||
101 | /* The TCB can have any size and the memory following the address the | |
102 | thread pointer points to is unspecified. Allocate the TCB there. */ | |
103 | # define TLS_TCB_AT_TP 1 | |
104 | ||
105 | ||
106 | /* Install the dtv pointer. The pointer passed is to the element with | |
107 | index -1 which contain the length. */ | |
108 | # define INSTALL_DTV(descr, dtvp) \ | |
d4f64e1a | 109 | ((tcbhead_t *) (descr))->dtv = (dtvp) + 1 |
a3931336 UD |
110 | |
111 | /* Install new dtv for current thread. */ | |
d4f64e1a | 112 | # define INSTALL_NEW_DTV(dtvp) \ |
a3931336 | 113 | ({ struct pthread *__pd; \ |
55c11fbd | 114 | THREAD_SETMEM (__pd, header.dtv, (dtvp)); }) |
a3931336 UD |
115 | |
116 | /* Return dtv of given thread descriptor. */ | |
117 | # define GET_DTV(descr) \ | |
118 | (((tcbhead_t *) (descr))->dtv) | |
119 | ||
120 | ||
c6180643 UD |
121 | /* Macros to load from and store into segment registers. */ |
122 | # define TLS_GET_FS() \ | |
123 | ({ int __seg; __asm ("movl %%fs, %0" : "=q" (__seg)); __seg; }) | |
124 | # define TLS_SET_FS(val) \ | |
125 | __asm ("movl %0, %%fs" :: "q" (val)) | |
126 | ||
127 | ||
a3931336 UD |
128 | /* Code to initially initialize the thread pointer. This might need |
129 | special attention since 'errno' is not yet available and if the | |
130 | operation can cause a failure 'errno' must not be touched. | |
131 | ||
132 | We have to make the syscall for both uses of the macro since the | |
133 | address might be (and probably is) different. */ | |
134 | # define TLS_INIT_TP(thrdescr, secondcall) \ | |
135 | ({ void *_thrdescr = (thrdescr); \ | |
136 | tcbhead_t *_head = _thrdescr; \ | |
137 | int _result; \ | |
138 | \ | |
139 | _head->tcb = _thrdescr; \ | |
140 | /* For now the thread descriptor is at the same address. */ \ | |
141 | _head->self = _thrdescr; \ | |
142 | \ | |
143 | /* It is a simple syscall to set the %fs value for the thread. */ \ | |
144 | asm volatile ("syscall" \ | |
145 | : "=a" (_result) \ | |
146 | : "0" ((unsigned long int) __NR_arch_prctl), \ | |
147 | "D" ((unsigned long int) ARCH_SET_FS), \ | |
3e976b96 | 148 | "S" (_thrdescr) \ |
a3931336 UD |
149 | : "memory", "cc", "r11", "cx"); \ |
150 | \ | |
fde89ad0 RM |
151 | _result ? "cannot set %fs base address for thread-local storage" : 0; \ |
152 | }) | |
a3931336 UD |
153 | |
154 | ||
155 | /* Return the address of the dtv for the current thread. */ | |
156 | # define THREAD_DTV() \ | |
157 | ({ struct pthread *__pd; \ | |
55c11fbd | 158 | THREAD_GETMEM (__pd, header.dtv); }) |
a3931336 UD |
159 | |
160 | ||
161 | /* Return the thread descriptor for the current thread. | |
162 | ||
163 | The contained asm must *not* be marked volatile since otherwise | |
164 | assignments like | |
165 | pthread_descr self = thread_self(); | |
166 | do not get optimized away. */ | |
167 | # define THREAD_SELF \ | |
168 | ({ struct pthread *__self; \ | |
5a03acfe | 169 | asm ("movq %%fs:%c1,%q0" : "=r" (__self) \ |
55c11fbd | 170 | : "i" (offsetof (struct pthread, header.self))); \ |
a3931336 UD |
171 | __self;}) |
172 | ||
7f08f55a RM |
173 | /* Magic for libthread_db to know how to do THREAD_SELF. */ |
174 | # define DB_THREAD_SELF_INCLUDE <sys/reg.h> /* For the FS constant. */ | |
175 | # define DB_THREAD_SELF CONST_THREAD_AREA (64, FS) | |
a3931336 UD |
176 | |
177 | /* Read member of the thread descriptor directly. */ | |
178 | # define THREAD_GETMEM(descr, member) \ | |
179 | ({ __typeof (descr->member) __value; \ | |
180 | if (sizeof (__value) == 1) \ | |
7f293686 UD |
181 | asm volatile ("movb %%fs:%P2,%b0" \ |
182 | : "=q" (__value) \ | |
183 | : "0" (0), "i" (offsetof (struct pthread, member))); \ | |
a3931336 | 184 | else if (sizeof (__value) == 4) \ |
7f293686 UD |
185 | asm volatile ("movl %%fs:%P1,%0" \ |
186 | : "=r" (__value) \ | |
187 | : "i" (offsetof (struct pthread, member))); \ | |
a3931336 UD |
188 | else \ |
189 | { \ | |
190 | if (sizeof (__value) != 8) \ | |
191 | /* There should not be any value with a size other than 1, \ | |
192 | 4 or 8. */ \ | |
193 | abort (); \ | |
194 | \ | |
7f293686 UD |
195 | asm volatile ("movq %%fs:%P1,%q0" \ |
196 | : "=r" (__value) \ | |
197 | : "i" (offsetof (struct pthread, member))); \ | |
a3931336 UD |
198 | } \ |
199 | __value; }) | |
200 | ||
201 | ||
202 | /* Same as THREAD_GETMEM, but the member offset can be non-constant. */ | |
203 | # define THREAD_GETMEM_NC(descr, member, idx) \ | |
204 | ({ __typeof (descr->member[0]) __value; \ | |
205 | if (sizeof (__value) == 1) \ | |
7f293686 UD |
206 | asm volatile ("movb %%fs:%P2(%q3),%b0" \ |
207 | : "=q" (__value) \ | |
208 | : "0" (0), "i" (offsetof (struct pthread, member[0])), \ | |
209 | "r" (idx)); \ | |
a3931336 | 210 | else if (sizeof (__value) == 4) \ |
7f293686 UD |
211 | asm volatile ("movl %%fs:%P1(,%q2,4),%0" \ |
212 | : "=r" (__value) \ | |
213 | : "i" (offsetof (struct pthread, member[0])), "r" (idx));\ | |
a3931336 UD |
214 | else \ |
215 | { \ | |
216 | if (sizeof (__value) != 8) \ | |
217 | /* There should not be any value with a size other than 1, \ | |
218 | 4 or 8. */ \ | |
219 | abort (); \ | |
220 | \ | |
7f293686 UD |
221 | asm volatile ("movq %%fs:%P1(,%q2,8),%q0" \ |
222 | : "=r" (__value) \ | |
223 | : "i" (offsetof (struct pthread, member[0])), \ | |
224 | "r" (idx)); \ | |
a3931336 UD |
225 | } \ |
226 | __value; }) | |
227 | ||
228 | ||
177d1ad3 UD |
229 | /* Loading addresses of objects on x86-64 needs to be treated special |
230 | when generating PIC code. */ | |
231 | #ifdef __pic__ | |
232 | # define IMM_MODE "nr" | |
233 | #else | |
234 | # define IMM_MODE "ir" | |
235 | #endif | |
236 | ||
237 | ||
a3931336 UD |
238 | /* Same as THREAD_SETMEM, but the member offset can be non-constant. */ |
239 | # define THREAD_SETMEM(descr, member, value) \ | |
240 | ({ if (sizeof (descr->member) == 1) \ | |
5a03acfe | 241 | asm volatile ("movb %b0,%%fs:%P1" : \ |
a3931336 UD |
242 | : "iq" (value), \ |
243 | "i" (offsetof (struct pthread, member))); \ | |
244 | else if (sizeof (descr->member) == 4) \ | |
245 | asm volatile ("movl %0,%%fs:%P1" : \ | |
177d1ad3 | 246 | : IMM_MODE (value), \ |
a3931336 UD |
247 | "i" (offsetof (struct pthread, member))); \ |
248 | else \ | |
249 | { \ | |
250 | if (sizeof (descr->member) != 8) \ | |
251 | /* There should not be any value with a size other than 1, \ | |
252 | 4 or 8. */ \ | |
253 | abort (); \ | |
254 | \ | |
5a03acfe | 255 | asm volatile ("movq %q0,%%fs:%P1" : \ |
177d1ad3 | 256 | : IMM_MODE ((unsigned long int) value), \ |
a3931336 UD |
257 | "i" (offsetof (struct pthread, member))); \ |
258 | }}) | |
259 | ||
260 | ||
261 | /* Set member of the thread descriptor directly. */ | |
262 | # define THREAD_SETMEM_NC(descr, member, idx, value) \ | |
263 | ({ if (sizeof (descr->member[0]) == 1) \ | |
5a03acfe | 264 | asm volatile ("movb %b0,%%fs:%P1(%q2)" : \ |
a3931336 UD |
265 | : "iq" (value), \ |
266 | "i" (offsetof (struct pthread, member[0])), \ | |
267 | "r" (idx)); \ | |
268 | else if (sizeof (descr->member[0]) == 4) \ | |
5a03acfe | 269 | asm volatile ("movl %0,%%fs:%P1(,%q2,4)" : \ |
177d1ad3 | 270 | : IMM_MODE (value), \ |
a3931336 UD |
271 | "i" (offsetof (struct pthread, member[0])), \ |
272 | "r" (idx)); \ | |
273 | else \ | |
274 | { \ | |
275 | if (sizeof (descr->member[0]) != 8) \ | |
276 | /* There should not be any value with a size other than 1, \ | |
277 | 4 or 8. */ \ | |
278 | abort (); \ | |
279 | \ | |
5a03acfe | 280 | asm volatile ("movq %q0,%%fs:%P1(,%q2,8)" : \ |
177d1ad3 | 281 | : IMM_MODE ((unsigned long int) value), \ |
a3931336 UD |
282 | "i" (offsetof (struct pthread, member[0])), \ |
283 | "r" (idx)); \ | |
284 | }}) | |
285 | ||
286 | ||
b22d701b UD |
287 | /* Atomic compare and exchange on TLS, returning old value. */ |
288 | #define THREAD_ATOMIC_CMPXCHG_VAL(descr, member, newval, oldval) \ | |
289 | ({ __typeof (descr->member) __ret; \ | |
290 | __typeof (oldval) __old = (oldval); \ | |
291 | if (sizeof (descr->member) == 4) \ | |
bd4f43b4 | 292 | asm volatile (LOCK_PREFIX "cmpxchgl %2, %%fs:%P3" \ |
b22d701b UD |
293 | : "=a" (__ret) \ |
294 | : "0" (__old), "r" (newval), \ | |
295 | "i" (offsetof (struct pthread, member))); \ | |
296 | else \ | |
297 | /* Not necessary for other sizes in the moment. */ \ | |
298 | abort (); \ | |
299 | __ret; }) | |
300 | ||
301 | ||
302 | /* Atomic set bit. */ | |
303 | #define THREAD_ATOMIC_BIT_SET(descr, member, bit) \ | |
304 | (void) ({ if (sizeof ((descr)->member) == 4) \ | |
bd4f43b4 | 305 | asm volatile (LOCK_PREFIX "orl %1, %%fs:%P0" \ |
b22d701b UD |
306 | :: "i" (offsetof (struct pthread, member)), \ |
307 | "ir" (1 << (bit))); \ | |
308 | else \ | |
309 | /* Not necessary for other sizes in the moment. */ \ | |
310 | abort (); }) | |
311 | ||
312 | ||
a87731e2 UD |
313 | #define CALL_THREAD_FCT(descr) \ |
314 | ({ void *__res; \ | |
315 | asm volatile ("movq %%fs:%P2, %%rdi\n\t" \ | |
316 | "callq *%%fs:%P1" \ | |
317 | : "=a" (__res) \ | |
318 | : "i" (offsetof (struct pthread, start_routine)), \ | |
319 | "i" (offsetof (struct pthread, arg)) \ | |
320 | : "di", "si", "cx", "dx", "r8", "r9", "r10", "r11", \ | |
321 | "memory", "cc"); \ | |
322 | __res; }) | |
323 | ||
324 | ||
35f1e827 UD |
325 | /* Set the stack guard field in TCB head. */ |
326 | # define THREAD_SET_STACK_GUARD(value) \ | |
327 | THREAD_SETMEM (THREAD_SELF, header.stack_guard, value) | |
328 | # define THREAD_COPY_STACK_GUARD(descr) \ | |
329 | ((descr)->header.stack_guard \ | |
330 | = THREAD_GETMEM (THREAD_SELF, header.stack_guard)) | |
331 | ||
a3931336 UD |
332 | #endif /* __ASSEMBLER__ */ |
333 | ||
334 | #endif /* tls.h */ |