1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2023 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, see
17 <https://www.gnu.org/licenses/>. */
23 /* Some things really need not be machine-dependent. */
24 #include <sysdeps/mach/hurd/tls.h>
30 /* Type of the TCB. */
33 void *tcb
; /* Points to this structure. */
34 dtv_t
*dtv
; /* Vector of pointers to TLS data. */
35 thread_t self
; /* This thread's control port. */
38 uintptr_t stack_guard
;
39 uintptr_t pointer_guard
;
42 /* Reservation of some values for the TM ABI. */
43 void *__private_tm
[4];
44 /* GCC split stack support. */
47 /* Keep these fields last, so offsets of fields above can continue being
48 compatible with the i386 Linux version. */
49 mach_port_t reply_port
; /* This thread's reply port. */
50 struct hurd_sigstate
*_hurd_sigstate
;
52 /* Used by the exception handling implementation in the dynamic loader. */
53 struct rtld_catch
*rtld_catch
;
56 /* Return tcbhead_t from a TLS segment descriptor. */
57 # define HURD_DESC_TLS(desc) \
59 (tcbhead_t *) ( (desc->low_word >> 16) \
60 | ((desc->high_word & 0xff) << 16) \
61 | (desc->high_word & 0xff000000)); \
64 /* Return 1 if TLS is not initialized yet. */
66 extern unsigned short __init1_desc
;
67 #define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds) || (gs) == __init1_desc)
69 #define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds))
72 #define __LIBC_NO_TLS() \
73 ({ unsigned short ds, gs; \
74 asm ("movw %%ds,%w0; movw %%gs,%w1" : "=q" (ds), "=q" (gs)); \
75 __builtin_expect(__HURD_DESC_INITIAL(gs, ds), 0); })
78 /* The TCB can have any size and the memory following the address the
79 thread pointer points to is unspecified. Allocate the TCB there. */
80 #define TLS_TCB_AT_TP 1
81 #define TLS_DTV_AT_TP 0
83 /* Alignment requirement for TCB.
85 Some processors such as Intel Atom pay a big penalty on every
86 access using a segment override if that segment's base is not
87 aligned to the size of a cache line. (See Intel 64 and IA-32
88 Architectures Optimization Reference Manual, section 13.3.3.3,
89 "Segment Base".) On such machines, a cache line is 64 bytes. */
90 #define TCB_ALIGNMENT 64
94 /* Use i386-specific RPCs to arrange that %gs segment register prefix
95 addresses the TCB in each thread. */
96 # include <mach/i386/mach_i386.h>
98 # ifndef HAVE_I386_SET_GDT
99 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
105 # define HURD_TLS_DESC_DECL(desc, tcb) \
106 struct descriptor desc = \
108 0xffff /* limit 0..15 */ \
109 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
111 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
112 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
113 | (0xf << 16) /* limit 16..19 */ \
114 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
115 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
118 # define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
120 static inline bool __attribute__ ((unused
))
121 _hurd_tls_init (tcbhead_t
*tcb
)
123 HURD_TLS_DESC_DECL (desc
, tcb
);
124 thread_t self
= __mach_thread_self ();
127 /* This field is used by TLS accesses to get our "thread pointer"
128 from the TLS point of view. */
130 /* We always at least start the sigthread anyway. */
131 tcb
->multiple_threads
= 1;
133 /* Get the first available selector. */
135 error_t err
= __i386_set_gdt (self
, &sel
, desc
);
136 if (err
== MIG_BAD_ID
)
138 /* Old kernel, use a per-thread LDT. */
140 err
= __i386_set_ldt (self
, sel
, &desc
, 1);
150 assert_perror (err
); /* Separate from above with different line #. */
155 /* Now install the new selector. */
156 asm volatile ("mov %w0, %%gs" :: "q" (sel
));
159 __mach_port_deallocate (__mach_task_self (), self
);
163 /* Code to initially initialize the thread pointer. This might need
164 special attention since 'errno' is not yet available and if the
165 operation can cause a failure 'errno' must not be touched. */
166 # define TLS_INIT_TP(descr) \
167 _hurd_tls_init ((tcbhead_t *) (descr))
169 /* Return the TCB address of the current thread. */
170 # define THREAD_SELF \
171 ({ tcbhead_t *__tcb; \
172 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
173 : "i" (offsetof (tcbhead_t, tcb))); \
176 /* Read member of the thread descriptor directly. */
177 # define THREAD_GETMEM(descr, member) \
178 ({ __typeof (descr->member) __value; \
179 _Static_assert (sizeof (__value) == 1 \
180 || sizeof (__value) == 4 \
181 || sizeof (__value) == 8, \
182 "size of per-thread data"); \
183 if (sizeof (__value) == 1) \
184 asm volatile ("movb %%gs:%P2,%b0" \
186 : "0" (0), "i" (offsetof (tcbhead_t, member))); \
187 else if (sizeof (__value) == 4) \
188 asm volatile ("movl %%gs:%P1,%0" \
190 : "i" (offsetof (tcbhead_t, member))); \
193 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
194 "movl %%gs:%P2,%%edx" \
196 : "i" (offsetof (tcbhead_t, member)), \
197 "i" (offsetof (tcbhead_t, member) + 4)); \
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 _Static_assert (sizeof (__value) == 1 \
206 || sizeof (__value) == 4 \
207 || sizeof (__value) == 8, \
208 "size of per-thread data"); \
209 if (sizeof (__value) == 1) \
210 asm volatile ("movb %%gs:%P2(%3),%b0" \
212 : "0" (0), "i" (offsetof (tcbhead_t, member[0])), \
214 else if (sizeof (__value) == 4) \
215 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
217 : "i" (offsetof (tcbhead_t, member[0])), \
221 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
222 "movl %%gs:4+%P1(,%2,8),%%edx" \
224 : "i" (offsetof (tcbhead_t, member[0])), \
231 /* Set member of the thread descriptor directly. */
232 # define THREAD_SETMEM(descr, member, value) \
234 _Static_assert (sizeof (descr->member) == 1 \
235 || sizeof (descr->member) == 4 \
236 || sizeof (descr->member) == 8, \
237 "size of per-thread data"); \
238 if (sizeof (descr->member) == 1) \
239 asm volatile ("movb %b0,%%gs:%P1" : \
241 "i" (offsetof (tcbhead_t, member))); \
242 else if (sizeof (descr->member) == 4) \
243 asm volatile ("movl %0,%%gs:%P1" : \
245 "i" (offsetof (tcbhead_t, member))); \
248 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
249 "movl %%edx,%%gs:%P2" : \
250 : "A" ((uint64_t) cast_to_integer (value)), \
251 "i" (offsetof (tcbhead_t, member)), \
252 "i" (offsetof (tcbhead_t, member) + 4)); \
256 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
257 # define THREAD_SETMEM_NC(descr, member, idx, value) \
259 _Static_assert (sizeof (descr->member[0]) == 1 \
260 || sizeof (descr->member[0]) == 4 \
261 || sizeof (descr->member[0]) == 8, \
262 "size of per-thread data"); \
263 if (sizeof (descr->member[0]) == 1) \
264 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
266 "i" (offsetof (tcbhead_t, member)), \
268 else if (sizeof (descr->member[0]) == 4) \
269 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
271 "i" (offsetof (tcbhead_t, member)), \
275 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
276 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
277 : "A" ((uint64_t) cast_to_integer (value)), \
278 "i" (offsetof (tcbhead_t, member)), \
282 /* Return the TCB address of a thread given its state.
283 Note: this is expensive. */
284 # define THREAD_TCB(thread, thread_state) \
285 ({ int __sel = (thread_state)->basic.gs; \
286 struct descriptor __desc, *___desc = &__desc; \
287 unsigned int __count = 1; \
288 kern_return_t __err; \
289 if (HURD_SEL_LDT (__sel)) \
290 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
292 __err = __i386_get_gdt ((thread), __sel, &__desc); \
293 assert_perror (__err); \
294 assert (__count == 1); \
295 HURD_DESC_TLS (___desc);})
297 /* Install new dtv for current thread. */
298 # define INSTALL_NEW_DTV(dtvp) \
299 ({ asm volatile ("movl %0,%%gs:%P1" \
300 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
302 /* Return the address of the dtv for the current thread. */
303 # define THREAD_DTV() \
305 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
309 /* Set the stack guard field in TCB head. */
310 #define THREAD_SET_STACK_GUARD(value) \
311 THREAD_SETMEM (THREAD_SELF, stack_guard, value)
312 #define THREAD_COPY_STACK_GUARD(descr) \
313 ((descr)->stack_guard \
314 = THREAD_GETMEM (THREAD_SELF, stack_guard))
316 /* Set the pointer guard field in the TCB head. */
317 #define THREAD_SET_POINTER_GUARD(value) \
318 THREAD_SETMEM (THREAD_SELF, pointer_guard, value)
319 #define THREAD_COPY_POINTER_GUARD(descr) \
320 ((descr)->pointer_guard \
321 = THREAD_GETMEM (THREAD_SELF, pointer_guard))
324 # include <mach/machine/thread_status.h>
326 /* Set up TLS in the new thread of a fork child, copying from the original. */
327 static inline kern_return_t
__attribute__ ((unused
))
328 _hurd_tls_fork (thread_t child
, thread_t orig
, struct i386_thread_state
*state
)
330 /* Fetch the selector set by _hurd_tls_init. */
332 asm ("mov %%gs, %w0" : "=q" (sel
) : "0" (0));
333 if (sel
== state
->ds
) /* _hurd_tls_init was never called. */
336 struct descriptor desc
, *_desc
= &desc
;
338 unsigned int count
= 1;
340 if (HURD_SEL_LDT (sel
))
341 err
= __i386_get_ldt (orig
, sel
, 1, &_desc
, &count
);
343 err
= __i386_get_gdt (orig
, sel
, &desc
);
349 if (HURD_SEL_LDT (sel
))
350 err
= __i386_set_ldt (child
, sel
, &desc
, 1);
352 err
= __i386_set_gdt (child
, &sel
, desc
);
358 static inline kern_return_t
__attribute__ ((unused
))
359 _hurd_tls_new (thread_t child
, struct i386_thread_state
*state
, tcbhead_t
*tcb
)
361 /* Fetch the selector set by _hurd_tls_init. */
363 asm ("mov %%gs, %w0" : "=q" (sel
) : "0" (0));
364 if (sel
== state
->ds
) /* _hurd_tls_init was never called. */
367 HURD_TLS_DESC_DECL (desc
, tcb
);
373 if (HURD_SEL_LDT (sel
))
374 err
= __i386_set_ldt (child
, sel
, &desc
, 1);
376 err
= __i386_set_gdt (child
, &sel
, desc
);
382 /* Global scope switch support. */
383 # define THREAD_GSCOPE_FLAG_UNUSED 0
384 # define THREAD_GSCOPE_FLAG_USED 1
385 # define THREAD_GSCOPE_FLAG_WAIT 2
387 # define THREAD_GSCOPE_SET_FLAG() \
388 THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED)
390 # define THREAD_GSCOPE_RESET_FLAG() \
393 asm volatile ("xchgl %0, %%gs:%P1" \
395 : "i" (offsetof (tcbhead_t, gscope_flag)), \
396 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
397 if (__flag == THREAD_GSCOPE_FLAG_WAIT) \
398 lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE); \
401 #endif /* !__ASSEMBLER__ */
403 #endif /* i386/tls.h */