1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2022 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 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _I386_TLS_H
20 #define _I386_TLS_H
21
22
23 /* Some things really need not be machine-dependent. */
24 #include <sysdeps/mach/hurd/tls.h>
25
26
27 #ifndef __ASSEMBLER__
28 # include <dl-dtv.h>
29
30 /* Type of the TCB. */
31 typedef struct
32 {
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. */
36 int multiple_threads;
37 uintptr_t sysinfo;
38 uintptr_t stack_guard;
39 uintptr_t pointer_guard;
40 int gscope_flag;
41 int private_futex;
42 /* Reservation of some values for the TM ABI. */
43 void *__private_tm[4];
44 /* GCC split stack support. */
45 void *__private_ss;
46
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;
51 } tcbhead_t;
52 #endif
53
54 /* Return tcbhead_t from a TLS segment descriptor. */
55 # define HURD_DESC_TLS(desc) \
56 ({ \
57 (tcbhead_t *) ( (desc->low_word >> 16) \
58 | ((desc->high_word & 0xff) << 16) \
59 | (desc->high_word & 0xff000000)); \
60 })
61
62 /* Return 1 if TLS is not initialized yet. */
63 #define __LIBC_NO_TLS() \
64 ({ unsigned short ds, gs; \
65 asm ("movw %%ds,%w0; movw %%gs,%w1" : "=q" (ds), "=q" (gs)); \
66 __builtin_expect (ds == gs, 0); })
67
68 /* The TCB can have any size and the memory following the address the
69 thread pointer points to is unspecified. Allocate the TCB there. */
70 #define TLS_TCB_AT_TP 1
71 #define TLS_DTV_AT_TP 0
72
73 /* Alignment requirement for TCB.
74
75 Some processors such as Intel Atom pay a big penalty on every
76 access using a segment override if that segment's base is not
77 aligned to the size of a cache line. (See Intel 64 and IA-32
78 Architectures Optimization Reference Manual, section 13.3.3.3,
79 "Segment Base".) On such machines, a cache line is 64 bytes. */
80 #define TCB_ALIGNMENT 64
81
82 #ifndef __ASSEMBLER__
83
84 /* Use i386-specific RPCs to arrange that %gs segment register prefix
85 addresses the TCB in each thread. */
86 # include <mach/i386/mach_i386.h>
87
88 # ifndef HAVE_I386_SET_GDT
89 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
90 # endif
91
92 # include <errno.h>
93 # include <assert.h>
94
95 # define HURD_TLS_DESC_DECL(desc, tcb) \
96 struct descriptor desc = \
97 { /* low word: */ \
98 0xffff /* limit 0..15 */ \
99 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
100 , /* high word: */ \
101 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
102 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
103 | (0xf << 16) /* limit 16..19 */ \
104 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
105 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
106 }
107
108 # define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
109
110 static inline const char * __attribute__ ((unused))
_hurd_tls_init(tcbhead_t * tcb)111 _hurd_tls_init (tcbhead_t *tcb)
112 {
113 HURD_TLS_DESC_DECL (desc, tcb);
114 thread_t self = __mach_thread_self ();
115 const char *msg = NULL;
116
117 /* This field is used by TLS accesses to get our "thread pointer"
118 from the TLS point of view. */
119 tcb->tcb = tcb;
120 /* We always at least start the sigthread anyway. */
121 tcb->multiple_threads = 1;
122
123 /* Get the first available selector. */
124 int sel = -1;
125 error_t err = __i386_set_gdt (self, &sel, desc);
126 if (err == MIG_BAD_ID)
127 {
128 /* Old kernel, use a per-thread LDT. */
129 sel = 0x27;
130 err = __i386_set_ldt (self, sel, &desc, 1);
131 assert_perror (err);
132 if (err)
133 {
134 msg = "i386_set_ldt failed";
135 goto out;
136 }
137 }
138 else if (err)
139 {
140 assert_perror (err); /* Separate from above with different line #. */
141 msg = "i386_set_gdt failed";
142 goto out;
143 }
144
145 /* Now install the new selector. */
146 asm volatile ("mov %w0, %%gs" :: "q" (sel));
147
148 out:
149 __mach_port_deallocate (__mach_task_self (), self);
150 return msg;
151 }
152
153 /* Code to initially initialize the thread pointer. This might need
154 special attention since 'errno' is not yet available and if the
155 operation can cause a failure 'errno' must not be touched. */
156 # define TLS_INIT_TP(descr) \
157 _hurd_tls_init ((tcbhead_t *) (descr))
158
159 /* Return the TCB address of the current thread. */
160 # define THREAD_SELF \
161 ({ tcbhead_t *__tcb; \
162 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
163 : "i" (offsetof (tcbhead_t, tcb))); \
164 __tcb;})
165
166 /* Read member of the thread descriptor directly. */
167 # define THREAD_GETMEM(descr, member) \
168 ({ __typeof (descr->member) __value; \
169 _Static_assert (sizeof (__value) == 1 \
170 || sizeof (__value) == 4 \
171 || sizeof (__value) == 8, \
172 "size of per-thread data"); \
173 if (sizeof (__value) == 1) \
174 asm volatile ("movb %%gs:%P2,%b0" \
175 : "=q" (__value) \
176 : "0" (0), "i" (offsetof (tcbhead_t, member))); \
177 else if (sizeof (__value) == 4) \
178 asm volatile ("movl %%gs:%P1,%0" \
179 : "=r" (__value) \
180 : "i" (offsetof (tcbhead_t, member))); \
181 else /* 8 */ \
182 { \
183 asm volatile ("movl %%gs:%P1,%%eax\n\t" \
184 "movl %%gs:%P2,%%edx" \
185 : "=A" (__value) \
186 : "i" (offsetof (tcbhead_t, member)), \
187 "i" (offsetof (tcbhead_t, member) + 4)); \
188 } \
189 __value; })
190
191
192 /* Same as THREAD_GETMEM, but the member offset can be non-constant. */
193 # define THREAD_GETMEM_NC(descr, member, idx) \
194 ({ __typeof (descr->member[0]) __value; \
195 _Static_assert (sizeof (__value) == 1 \
196 || sizeof (__value) == 4 \
197 || sizeof (__value) == 8, \
198 "size of per-thread data"); \
199 if (sizeof (__value) == 1) \
200 asm volatile ("movb %%gs:%P2(%3),%b0" \
201 : "=q" (__value) \
202 : "0" (0), "i" (offsetof (tcbhead_t, member[0])), \
203 "r" (idx)); \
204 else if (sizeof (__value) == 4) \
205 asm volatile ("movl %%gs:%P1(,%2,4),%0" \
206 : "=r" (__value) \
207 : "i" (offsetof (tcbhead_t, member[0])), \
208 "r" (idx)); \
209 else /* 8 */ \
210 { \
211 asm volatile ("movl %%gs:%P1(,%2,8),%%eax\n\t" \
212 "movl %%gs:4+%P1(,%2,8),%%edx" \
213 : "=&A" (__value) \
214 : "i" (offsetof (tcbhead_t, member[0])), \
215 "r" (idx)); \
216 } \
217 __value; })
218
219
220
221 /* Set member of the thread descriptor directly. */
222 # define THREAD_SETMEM(descr, member, value) \
223 ({ \
224 _Static_assert (sizeof (descr->member) == 1 \
225 || sizeof (descr->member) == 4 \
226 || sizeof (descr->member) == 8, \
227 "size of per-thread data"); \
228 if (sizeof (descr->member) == 1) \
229 asm volatile ("movb %b0,%%gs:%P1" : \
230 : "iq" (value), \
231 "i" (offsetof (tcbhead_t, member))); \
232 else if (sizeof (descr->member) == 4) \
233 asm volatile ("movl %0,%%gs:%P1" : \
234 : "ir" (value), \
235 "i" (offsetof (tcbhead_t, member))); \
236 else /* 8 */ \
237 { \
238 asm volatile ("movl %%eax,%%gs:%P1\n\t" \
239 "movl %%edx,%%gs:%P2" : \
240 : "A" ((uint64_t) cast_to_integer (value)), \
241 "i" (offsetof (tcbhead_t, member)), \
242 "i" (offsetof (tcbhead_t, member) + 4)); \
243 }})
244
245
246 /* Same as THREAD_SETMEM, but the member offset can be non-constant. */
247 # define THREAD_SETMEM_NC(descr, member, idx, value) \
248 ({ \
249 _Static_assert (sizeof (descr->member[0]) == 1 \
250 || sizeof (descr->member[0]) == 4 \
251 || sizeof (descr->member[0]) == 8, \
252 "size of per-thread data"); \
253 if (sizeof (descr->member[0]) == 1) \
254 asm volatile ("movb %b0,%%gs:%P1(%2)" : \
255 : "iq" (value), \
256 "i" (offsetof (tcbhead_t, member)), \
257 "r" (idx)); \
258 else if (sizeof (descr->member[0]) == 4) \
259 asm volatile ("movl %0,%%gs:%P1(,%2,4)" : \
260 : "ir" (value), \
261 "i" (offsetof (tcbhead_t, member)), \
262 "r" (idx)); \
263 else /* 8 */ \
264 { \
265 asm volatile ("movl %%eax,%%gs:%P1(,%2,8)\n\t" \
266 "movl %%edx,%%gs:4+%P1(,%2,8)" : \
267 : "A" ((uint64_t) cast_to_integer (value)), \
268 "i" (offsetof (tcbhead_t, member)), \
269 "r" (idx)); \
270 }})
271
272 /* Return the TCB address of a thread given its state.
273 Note: this is expensive. */
274 # define THREAD_TCB(thread, thread_state) \
275 ({ int __sel = (thread_state)->basic.gs; \
276 struct descriptor __desc, *___desc = &__desc; \
277 unsigned int __count = 1; \
278 kern_return_t __err; \
279 if (HURD_SEL_LDT (__sel)) \
280 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
281 else \
282 __err = __i386_get_gdt ((thread), __sel, &__desc); \
283 assert_perror (__err); \
284 assert (__count == 1); \
285 HURD_DESC_TLS (___desc);})
286
287 /* Install new dtv for current thread. */
288 # define INSTALL_NEW_DTV(dtvp) \
289 ({ asm volatile ("movl %0,%%gs:%P1" \
290 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
291
292 /* Return the address of the dtv for the current thread. */
293 # define THREAD_DTV() \
294 ({ dtv_t *_dtv; \
295 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
296 _dtv; })
297
298
299 /* Set the stack guard field in TCB head. */
300 #define THREAD_SET_STACK_GUARD(value) \
301 THREAD_SETMEM (THREAD_SELF, stack_guard, value)
302 #define THREAD_COPY_STACK_GUARD(descr) \
303 ((descr)->stack_guard \
304 = THREAD_GETMEM (THREAD_SELF, stack_guard))
305
306 /* Set the pointer guard field in the TCB head. */
307 #define THREAD_SET_POINTER_GUARD(value) \
308 THREAD_SETMEM (THREAD_SELF, pointer_guard, value)
309 #define THREAD_COPY_POINTER_GUARD(descr) \
310 ((descr)->pointer_guard \
311 = THREAD_GETMEM (THREAD_SELF, pointer_guard))
312
313
314 # include <mach/machine/thread_status.h>
315
316 /* Set up TLS in the new thread of a fork child, copying from the original. */
317 static inline kern_return_t __attribute__ ((unused))
_hurd_tls_fork(thread_t child,thread_t orig,struct i386_thread_state * state)318 _hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
319 {
320 /* Fetch the selector set by _hurd_tls_init. */
321 int sel;
322 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
323 if (sel == state->ds) /* _hurd_tls_init was never called. */
324 return 0;
325
326 struct descriptor desc, *_desc = &desc;
327 error_t err;
328 unsigned int count = 1;
329
330 if (HURD_SEL_LDT (sel))
331 err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
332 else
333 err = __i386_get_gdt (orig, sel, &desc);
334
335 assert_perror (err);
336 if (err)
337 return err;
338
339 if (HURD_SEL_LDT (sel))
340 err = __i386_set_ldt (child, sel, &desc, 1);
341 else
342 err = __i386_set_gdt (child, &sel, desc);
343
344 state->gs = sel;
345 return err;
346 }
347
348 static inline kern_return_t __attribute__ ((unused))
_hurd_tls_new(thread_t child,struct i386_thread_state * state,tcbhead_t * tcb)349 _hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
350 {
351 /* Fetch the selector set by _hurd_tls_init. */
352 int sel;
353 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
354 if (sel == state->ds) /* _hurd_tls_init was never called. */
355 return 0;
356
357 HURD_TLS_DESC_DECL (desc, tcb);
358 error_t err;
359
360 tcb->tcb = tcb;
361 tcb->self = child;
362
363 if (HURD_SEL_LDT (sel))
364 err = __i386_set_ldt (child, sel, &desc, 1);
365 else
366 err = __i386_set_gdt (child, &sel, desc);
367
368 state->gs = sel;
369 return err;
370 }
371
372 /* Global scope switch support. */
373 # define THREAD_GSCOPE_FLAG_UNUSED 0
374 # define THREAD_GSCOPE_FLAG_USED 1
375 # define THREAD_GSCOPE_FLAG_WAIT 2
376
377 # define THREAD_GSCOPE_SET_FLAG() \
378 THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED)
379
380 # define THREAD_GSCOPE_RESET_FLAG() \
381 ({ \
382 int __flag; \
383 asm volatile ("xchgl %0, %%gs:%P1" \
384 : "=r" (__flag) \
385 : "i" (offsetof (tcbhead_t, gscope_flag)), \
386 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
387 if (__flag == THREAD_GSCOPE_FLAG_WAIT) \
388 lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE); \
389 })
390
391 #endif /* !__ASSEMBLER__ */
392
393 #endif /* i386/tls.h */
394