hurd: Replace threadvars with TLS
[glibc.git] / sysdeps / mach / hurd / i386 / tls.h
blob6cd1730887f6a61b3599c0573169774ec6011a5b
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2018 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 <http://www.gnu.org/licenses/>. */
19 #ifndef _I386_TLS_H
20 #define _I386_TLS_H
23 /* Some things really need not be machine-dependent. */
24 #include <sysdeps/mach/hurd/tls.h>
27 #ifndef __ASSEMBLER__
28 # include <dl-dtv.h>
30 /* Type of the TCB. */
31 typedef struct
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;
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
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)); \
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
82 #ifndef __ASSEMBLER__
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)
90 # endif
92 # include <errno.h>
93 # include <assert.h>
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 */ \
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);
115 /* This field is used by TLS accesses to get our "thread pointer"
116 from the TLS point of view. */
117 tcb->tcb = tcb;
119 /* Cache our thread port. */
120 tcb->self = __mach_thread_self ();
122 /* Get the first available selector. */
123 int sel = -1;
124 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
125 if (err == MIG_BAD_ID)
127 /* Old kernel, use a per-thread LDT. */
128 sel = 0x27;
129 err = __i386_set_ldt (tcb->self, sel, &desc, 1);
130 assert_perror (err);
131 if (err)
132 return "i386_set_ldt failed";
134 else if (err)
136 assert_perror (err); /* Separate from above with different line #. */
137 return "i386_set_gdt failed";
140 /* Now install the new selector. */
141 asm volatile ("mov %w0, %%gs" :: "q" (sel));
143 return 0;
146 /* Code to initially initialize the thread pointer. This might need
147 special attention since 'errno' is not yet available and if the
148 operation can cause a failure 'errno' must not be touched. */
149 # define TLS_INIT_TP(descr) \
150 _hurd_tls_init ((tcbhead_t *) (descr))
152 /* Return the TCB address of the current thread. */
153 # define THREAD_SELF \
154 ({ tcbhead_t *__tcb; \
155 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
156 : "i" (offsetof (tcbhead_t, tcb))); \
157 __tcb;})
159 /* Return the TCB address of a thread given its state.
160 Note: this is expensive. */
161 # define THREAD_TCB(thread, thread_state) \
162 ({ int __sel = (thread_state)->basic.gs; \
163 struct descriptor __desc, *___desc = &__desc; \
164 unsigned int __count = 1; \
165 kern_return_t __err; \
166 if (HURD_SEL_LDT(__sel)) \
167 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
168 else \
169 __err = __i386_get_gdt ((thread), __sel, &__desc); \
170 assert_perror (__err); \
171 assert (__count == 1); \
172 HURD_DESC_TLS(___desc);})
174 /* Install new dtv for current thread. */
175 # define INSTALL_NEW_DTV(dtvp) \
176 ({ asm volatile ("movl %0,%%gs:%P1" \
177 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
179 /* Return the address of the dtv for the current thread. */
180 # define THREAD_DTV() \
181 ({ dtv_t *_dtv; \
182 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
183 _dtv; })
185 # include <mach/machine/thread_status.h>
187 /* Set up TLS in the new thread of a fork child, copying from the original. */
188 static inline kern_return_t __attribute__ ((unused))
189 _hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
191 /* Fetch the selector set by _hurd_tls_init. */
192 int sel;
193 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
194 if (sel == state->ds) /* _hurd_tls_init was never called. */
195 return 0;
197 struct descriptor desc, *_desc = &desc;
198 error_t err;
199 unsigned int count = 1;
201 if (HURD_SEL_LDT(sel))
202 err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
203 else
204 err = __i386_get_gdt (orig, sel, &desc);
206 assert_perror (err);
207 if (err)
208 return err;
210 if (HURD_SEL_LDT(sel))
211 err = __i386_set_ldt (child, sel, &desc, 1);
212 else
213 err = __i386_set_gdt (child, &sel, desc);
215 state->gs = sel;
216 return err;
219 static inline kern_return_t __attribute__ ((unused))
220 _hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
222 /* Fetch the selector set by _hurd_tls_init. */
223 int sel;
224 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
225 if (sel == state->ds) /* _hurd_tls_init was never called. */
226 return 0;
228 HURD_TLS_DESC_DECL (desc, tcb);
229 error_t err;
231 tcb->tcb = tcb;
232 tcb->self = child;
234 if (HURD_SEL_LDT(sel))
235 err = __i386_set_ldt (child, sel, &desc, 1);
236 else
237 err = __i386_set_gdt (child, &sel, desc);
239 state->gs = sel;
240 return err;
243 #endif /* !__ASSEMBLER__ */
245 #endif /* i386/tls.h */