]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/i386/tls.h
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2020 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 this field last, so fields above can continue being compatible with
49 mach_port_t reply_port
; /* This thread's reply port. */
50 struct hurd_sigstate
*_hurd_sigstate
;
54 /* Return tcbhead_t from a TLS segment descriptor. */
55 # define HURD_DESC_TLS(desc) \
57 (tcbhead_t *) ( (desc->low_word >> 16) \
58 | ((desc->high_word & 0xff) << 16) \
59 | (desc->high_word & 0xff000000)); \
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); })
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
73 /* Alignment requirement for TCB.
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
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>
88 # ifndef HAVE_I386_SET_GDT
89 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
95 # define HURD_TLS_DESC_DECL(desc, tcb) \
96 struct descriptor desc = \
98 0xffff /* limit 0..15 */ \
99 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
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 */ \
108 # define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
110 static inline const char * __attribute__ ((unused
))
111 _hurd_tls_init (tcbhead_t
*tcb
)
113 HURD_TLS_DESC_DECL (desc
, tcb
);
114 thread_t self
= __mach_thread_self ();
115 const char *msg
= NULL
;
117 /* This field is used by TLS accesses to get our "thread pointer"
118 from the TLS point of view. */
120 /* We always at least start the sigthread anyway. */
121 tcb
->multiple_threads
= 1;
123 /* Get the first available selector. */
125 error_t err
= __i386_set_gdt (self
, &sel
, desc
);
126 if (err
== MIG_BAD_ID
)
128 /* Old kernel, use a per-thread LDT. */
130 err
= __i386_set_ldt (self
, sel
, &desc
, 1);
134 msg
= "i386_set_ldt failed";
140 assert_perror (err
); /* Separate from above with different line #. */
141 msg
= "i386_set_gdt failed";
145 /* Now install the new selector. */
146 asm volatile ("mov %w0, %%gs" :: "q" (sel
));
149 __mach_port_deallocate (__mach_task_self (), self
);
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))
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))); \
166 /* Return the TCB address of a thread given its state.
167 Note: this is expensive. */
168 # define THREAD_TCB(thread, thread_state) \
169 ({ int __sel = (thread_state)->basic.gs; \
170 struct descriptor __desc, *___desc = &__desc; \
171 unsigned int __count = 1; \
172 kern_return_t __err; \
173 if (HURD_SEL_LDT (__sel)) \
174 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
176 __err = __i386_get_gdt ((thread), __sel, &__desc); \
177 assert_perror (__err); \
178 assert (__count == 1); \
179 HURD_DESC_TLS (___desc);})
181 /* Install new dtv for current thread. */
182 # define INSTALL_NEW_DTV(dtvp) \
183 ({ asm volatile ("movl %0,%%gs:%P1" \
184 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
186 /* Return the address of the dtv for the current thread. */
187 # define THREAD_DTV() \
189 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
192 # include <mach/machine/thread_status.h>
194 /* Set up TLS in the new thread of a fork child, copying from the original. */
195 static inline kern_return_t
__attribute__ ((unused
))
196 _hurd_tls_fork (thread_t child
, thread_t orig
, struct i386_thread_state
*state
)
198 /* Fetch the selector set by _hurd_tls_init. */
200 asm ("mov %%gs, %w0" : "=q" (sel
) : "0" (0));
201 if (sel
== state
->ds
) /* _hurd_tls_init was never called. */
204 struct descriptor desc
, *_desc
= &desc
;
206 unsigned int count
= 1;
208 if (HURD_SEL_LDT (sel
))
209 err
= __i386_get_ldt (orig
, sel
, 1, &_desc
, &count
);
211 err
= __i386_get_gdt (orig
, sel
, &desc
);
217 if (HURD_SEL_LDT (sel
))
218 err
= __i386_set_ldt (child
, sel
, &desc
, 1);
220 err
= __i386_set_gdt (child
, &sel
, desc
);
226 static inline kern_return_t
__attribute__ ((unused
))
227 _hurd_tls_new (thread_t child
, struct i386_thread_state
*state
, tcbhead_t
*tcb
)
229 /* Fetch the selector set by _hurd_tls_init. */
231 asm ("mov %%gs, %w0" : "=q" (sel
) : "0" (0));
232 if (sel
== state
->ds
) /* _hurd_tls_init was never called. */
235 HURD_TLS_DESC_DECL (desc
, tcb
);
241 if (HURD_SEL_LDT (sel
))
242 err
= __i386_set_ldt (child
, sel
, &desc
, 1);
244 err
= __i386_set_gdt (child
, &sel
, desc
);
250 #endif /* !__ASSEMBLER__ */
252 #endif /* i386/tls.h */