]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/i386/tls.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / 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.
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 this field last, so fields above can continue being compatible with
48 the 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))
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 /* 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); \
175 else \
176 __err = __i386_get_gdt ((thread), __sel, &__desc); \
177 assert_perror (__err); \
178 assert (__count == 1); \
179 HURD_DESC_TLS (___desc);})
180
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))); })
185
186 /* Return the address of the dtv for the current thread. */
187 # define THREAD_DTV() \
188 ({ dtv_t *_dtv; \
189 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
190 _dtv; })
191
192 # include <mach/machine/thread_status.h>
193
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)
197 {
198 /* Fetch the selector set by _hurd_tls_init. */
199 int sel;
200 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
201 if (sel == state->ds) /* _hurd_tls_init was never called. */
202 return 0;
203
204 struct descriptor desc, *_desc = &desc;
205 error_t err;
206 unsigned int count = 1;
207
208 if (HURD_SEL_LDT (sel))
209 err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
210 else
211 err = __i386_get_gdt (orig, sel, &desc);
212
213 assert_perror (err);
214 if (err)
215 return err;
216
217 if (HURD_SEL_LDT (sel))
218 err = __i386_set_ldt (child, sel, &desc, 1);
219 else
220 err = __i386_set_gdt (child, &sel, desc);
221
222 state->gs = sel;
223 return err;
224 }
225
226 static inline kern_return_t __attribute__ ((unused))
227 _hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
228 {
229 /* Fetch the selector set by _hurd_tls_init. */
230 int sel;
231 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
232 if (sel == state->ds) /* _hurd_tls_init was never called. */
233 return 0;
234
235 HURD_TLS_DESC_DECL (desc, tcb);
236 error_t err;
237
238 tcb->tcb = tcb;
239 tcb->self = child;
240
241 if (HURD_SEL_LDT (sel))
242 err = __i386_set_ldt (child, sel, &desc, 1);
243 else
244 err = __i386_set_gdt (child, &sel, desc);
245
246 state->gs = sel;
247 return err;
248 }
249
250 #endif /* !__ASSEMBLER__ */
251
252 #endif /* i386/tls.h */