Debian Codegraph @

sysdeps/mach/hurd/i386/tls.h

raw symbols SCIP
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2026 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_do_not_use; /* 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 unsigned int feature_1;
42 /* Reservation of some values for the TM ABI. */
43 void *__private_tm[3];
44 /* GCC split stack support. */
45 void *__private_ss;
46 void *__glibc_padding1;
47
48 /* Keep these fields last, so offsets of fields above can continue being
49 compatible with the i386 Linux version. */
50 mach_port_t reply_port; /* This thread's reply port. */
51 struct hurd_sigstate *_hurd_sigstate;
52
53 /* Used by the exception handling implementation in the dynamic loader. */
54 struct rtld_catch *rtld_catch;
55 } tcbhead_t;
56
57 /* GCC generates %gs:0x14 to access the stack guard. */
58 _Static_assert (offsetof (tcbhead_t, stack_guard) == 0x14,
59 "stack guard offset");
60 /* libgcc uses %gs:0x30 to access the split stack pointer. */
61 _Static_assert (offsetof (tcbhead_t, __private_ss) == 0x30,
62 "split stack pointer offset");
63
64 /* Return tcbhead_t from a TLS segment descriptor. */
65 # define HURD_DESC_TLS(desc) \
66 ({ \
67 (tcbhead_t *) ( (desc->low_word >> 16) \
68 | ((desc->high_word & 0xff) << 16) \
69 | (desc->high_word & 0xff000000)); \
70 })
71
72 #endif
73
74 /* The TCB can have any size and the memory following the address the
75 thread pointer points to is unspecified. Allocate the TCB there. */
76 #define TLS_TCB_AT_TP 1
77 #define TLS_DTV_AT_TP 0
78
79 /* Alignment requirement for TCB.
80
81 Some processors such as Intel Atom pay a big penalty on every
82 access using a segment override if that segment's base is not
83 aligned to the size of a cache line. (See Intel 64 and IA-32
84 Architectures Optimization Reference Manual, section 13.3.3.3,
85 "Segment Base".) On such machines, a cache line is 64 bytes. */
86 #define TCB_ALIGNMENT 64
87
88 #ifndef __ASSEMBLER__
89
90 /* Use i386-specific RPCs to arrange that %gs segment register prefix
91 addresses the TCB in each thread. */
92 # include <mach/i386/mach_i386.h>
93
94 # ifndef HAVE_I386_SET_GDT
95 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
96 # endif
97
98 # include <errno.h>
99 # include <assert.h>
100
101 # define HURD_TLS_DESC_DECL(desc, tcb) \
102 struct descriptor desc = \
103 { /* low word: */ \
104 0xffff /* limit 0..15 */ \
105 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
106 , /* high word: */ \
107 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
108 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
109 | (0xf << 16) /* limit 16..19 */ \
110 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
111 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
112 }
113
114 # define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
115
116 #ifndef SHARED
117 extern unsigned short __init1_desc;
118 # define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds) || (gs) == __init1_desc)
119 #else
120 # define __HURD_DESC_INITIAL(gs, ds) ((gs) == (ds))
121 #endif
122
123 #if !defined (SHARED) || IS_IN (rtld)
124 /* Return 1 if TLS is not initialized yet. */
125 extern inline bool __attribute__ ((unused))
126 __LIBC_NO_TLS (void)
127 {
128 unsigned short ds, gs;
129 asm ("movw %%ds, %w0\n"
130 "movw %%gs, %w1"
131 : "=q" (ds), "=q" (gs));
132 return __glibc_unlikely (__HURD_DESC_INITIAL (gs, ds));
133 }
134
135 /* Code to initially initialize the thread pointer. This might need
136 special attention since 'errno' is not yet available and if the
137 operation can cause a failure 'errno' must not be touched. */
138 static inline bool __attribute__ ((unused))
139 _hurd_tls_init (tcbhead_t *tcb, bool full)
140 {
141 HURD_TLS_DESC_DECL (desc, tcb);
142 thread_t self = __mach_thread_self ();
143 bool success = true;
144 extern mach_port_t __hurd_reply_port0;
145
146 /* This field is used by TLS accesses to get our "thread pointer"
147 from the TLS point of view. */
148 tcb->tcb = tcb;
149 /* We always at least start the sigthread anyway. */
150 tcb->multiple_threads = 1;
151 if (full)
152 /* Take over the reply port we've been using. */
153 tcb->reply_port = __hurd_reply_port0;
154
155 /* Get the first available selector. */
156 int sel = -1;
157 error_t err = __i386_set_gdt (self, &sel, desc);
158 if (err == MIG_BAD_ID)
159 {
160 /* Old kernel, use a per-thread LDT. */
161 sel = 0x27;
162 err = __i386_set_ldt (self, sel, &desc, 1);
163 assert_perror (err);
164 if (err)
165 {
166 success = false;
167 goto out;
168 }
169 }
170 else if (err)
171 {
172 assert_perror (err); /* Separate from above with different line #. */
173 success = false;
174 goto out;
175 }
176
177 /* Now install the new selector. */
178 asm volatile ("mov %w0, %%gs" :: "q" (sel));
179 if (full)
180 /* This port is now owned by the TCB. */
181 __hurd_reply_port0 = MACH_PORT_NULL;
182 #ifndef SHARED
183 else
184 __init1_desc = sel;
185 #endif
186
187 out:
188 __mach_port_deallocate (__mach_task_self (), self);
189 return success;
190 }
191
192 # define TLS_INIT_TP(descr) _hurd_tls_init ((tcbhead_t *) (descr), 1)
193 #else /* defined (SHARED) && !IS_IN (rtld) */
194 # define __LIBC_NO_TLS() 0
195 #endif
196
197
198 # define THREAD_SELF \
199 (*(tcbhead_t * __seg_gs *) offsetof (tcbhead_t, tcb))
200 # define THREAD_GETMEM(descr, member) \
201 (*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member))
202 # define THREAD_GETMEM_NC(descr, member, idx) \
203 (*(__typeof (descr->member[0]) __seg_gs *) \
204 (offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0])))
205 # define THREAD_SETMEM(descr, member, value) \
206 (*(__typeof (descr->member) __seg_gs *) offsetof (tcbhead_t, member) = value)
207 # define THREAD_SETMEM_NC(descr, member, index, value) \
208 (*(__typeof (descr->member[0]) __seg_gs *) \
209 (offsetof (tcbhead_t, member) + (idx) * sizeof (descr->member[0])))
210
211
212 /* Return the TCB address of a thread given its state.
213 Note: this is expensive. */
214 # define THREAD_TCB(thread, thread_state) \
215 ({ int __sel = (thread_state)->basic.gs; \
216 struct descriptor __desc, *___desc = &__desc; \
217 unsigned int __count = 1; \
218 kern_return_t __err; \
219 if (HURD_SEL_LDT (__sel)) \
220 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
221 else \
222 __err = __i386_get_gdt ((thread), __sel, &__desc); \
223 assert_perror (__err); \
224 assert (__count == 1); \
225 HURD_DESC_TLS (___desc);})
226
227 /* Install new dtv for current thread. */
228 # define INSTALL_NEW_DTV(dtvp) THREAD_SETMEM (THREAD_SELF, dtv, dtvp)
229
230 /* Return the address of the dtv for the current thread. */
231 # define THREAD_DTV() THREAD_GETMEM (THREAD_SELF, dtv)
232
233
234 /* Set the stack guard field in TCB head. */
235 #define THREAD_SET_STACK_GUARD(value) \
236 THREAD_SETMEM (THREAD_SELF, stack_guard, value)
237 #define THREAD_COPY_STACK_GUARD(descr) \
238 ((descr)->stack_guard \
239 = THREAD_GETMEM (THREAD_SELF, stack_guard))
240
241 /* Set the pointer guard field in the TCB head. */
242 #define THREAD_SET_POINTER_GUARD(value) \
243 THREAD_SETMEM (THREAD_SELF, pointer_guard, value)
244 #define THREAD_COPY_POINTER_GUARD(descr) \
245 ((descr)->pointer_guard \
246 = THREAD_GETMEM (THREAD_SELF, pointer_guard))
247
248
249 # include <mach/machine/thread_status.h>
250
251 /* Set up TLS in the new thread of a fork child, copying from the original. */
252 static inline kern_return_t __attribute__ ((unused))
253 _hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
254 {
255 /* Fetch the selector set by _hurd_tls_init. */
256 int sel;
257 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
258 if (sel == state->ds) /* _hurd_tls_init was never called. */
259 return 0;
260
261 struct descriptor desc, *_desc = &desc;
262 error_t err;
263 unsigned int count = 1;
264
265 if (HURD_SEL_LDT (sel))
266 err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
267 else
268 err = __i386_get_gdt (orig, sel, &desc);
269
270 assert_perror (err);
271 if (err)
272 return err;
273
274 if (HURD_SEL_LDT (sel))
275 err = __i386_set_ldt (child, sel, &desc, 1);
276 else
277 err = __i386_set_gdt (child, &sel, desc);
278
279 state->gs = sel;
280 return err;
281 }
282
283 static inline kern_return_t __attribute__ ((unused))
284 _hurd_tls_new (thread_t child, tcbhead_t *tcb)
285 {
286 error_t err;
287 /* Fetch the target thread's state. */
288 struct i386_thread_state state;
289 mach_msg_type_number_t state_count = i386_THREAD_STATE_COUNT;
290 err = __thread_get_state (child, i386_REGS_SEGS_STATE,
291 (thread_state_t) &state,
292 &state_count);
293 if (err)
294 return err;
295 assert (state_count == i386_THREAD_STATE_COUNT);
296 /* Fetch the selector set by _hurd_tls_init. */
297 int sel;
298 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
299 if (sel == state.ds) /* _hurd_tls_init was never called. */
300 return 0;
301
302 HURD_TLS_DESC_DECL (desc, tcb);
303
304 tcb->tcb = tcb;
305
306 if (HURD_SEL_LDT (sel))
307 err = __i386_set_ldt (child, sel, &desc, 1);
308 else
309 err = __i386_set_gdt (child, &sel, desc);
310
311 if (err)
312 return err;
313
314 /* Update gs to use the selector. */
315 state.gs = sel;
316 return __thread_set_state (child, i386_REGS_SEGS_STATE,
317 (thread_state_t) &state,
318 state_count);
319 }
320
321 /* Global scope switch support. */
322 # define THREAD_GSCOPE_FLAG_UNUSED 0
323 # define THREAD_GSCOPE_FLAG_USED 1
324 # define THREAD_GSCOPE_FLAG_WAIT 2
325
326 # define THREAD_GSCOPE_SET_FLAG() \
327 THREAD_SETMEM (THREAD_SELF, gscope_flag, THREAD_GSCOPE_FLAG_USED)
328
329 # define THREAD_GSCOPE_RESET_FLAG() \
330 ({ \
331 int __flag; \
332 asm volatile ("xchgl %0, %%gs:%P1" \
333 : "=r" (__flag) \
334 : "i" (offsetof (tcbhead_t, gscope_flag)), \
335 "0" (THREAD_GSCOPE_FLAG_UNUSED)); \
336 if (__flag == THREAD_GSCOPE_FLAG_WAIT) \
337 lll_wake (THREAD_SELF->gscope_flag, LLL_PRIVATE); \
338 })
339
340 #endif /* !__ASSEMBLER__ */
341
342 #endif /* i386/tls.h */