Hurd: fix port leak in TLS
[glibc.git] / sysdeps / mach / hurd / i386 / tls.h
blob771c94ff95e34cb19803b690c7323a47a6294c82
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);
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. */
119 tcb->tcb = tcb;
121 /* Get the first available selector. */
122 int sel = -1;
123 error_t err = __i386_set_gdt (self, &sel, desc);
124 if (err == MIG_BAD_ID)
126 /* Old kernel, use a per-thread LDT. */
127 sel = 0x27;
128 err = __i386_set_ldt (self, sel, &desc, 1);
129 assert_perror (err);
130 if (err)
132 msg = "i386_set_ldt failed";
133 goto out;
136 else if (err)
138 assert_perror (err); /* Separate from above with different line #. */
139 msg = "i386_set_gdt failed";
140 goto out;
143 /* Now install the new selector. */
144 asm volatile ("mov %w0, %%gs" :: "q" (sel));
146 out:
147 __mach_port_deallocate (__mach_task_self (), self);
148 return msg;
151 /* Code to initially initialize the thread pointer. This might need
152 special attention since 'errno' is not yet available and if the
153 operation can cause a failure 'errno' must not be touched. */
154 # define TLS_INIT_TP(descr) \
155 _hurd_tls_init ((tcbhead_t *) (descr))
157 /* Return the TCB address of the current thread. */
158 # define THREAD_SELF \
159 ({ tcbhead_t *__tcb; \
160 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
161 : "i" (offsetof (tcbhead_t, tcb))); \
162 __tcb;})
164 /* Return the TCB address of a thread given its state.
165 Note: this is expensive. */
166 # define THREAD_TCB(thread, thread_state) \
167 ({ int __sel = (thread_state)->basic.gs; \
168 struct descriptor __desc, *___desc = &__desc; \
169 unsigned int __count = 1; \
170 kern_return_t __err; \
171 if (HURD_SEL_LDT (__sel)) \
172 __err = __i386_get_ldt ((thread), __sel, 1, &___desc, &__count); \
173 else \
174 __err = __i386_get_gdt ((thread), __sel, &__desc); \
175 assert_perror (__err); \
176 assert (__count == 1); \
177 HURD_DESC_TLS (___desc);})
179 /* Install new dtv for current thread. */
180 # define INSTALL_NEW_DTV(dtvp) \
181 ({ asm volatile ("movl %0,%%gs:%P1" \
182 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
184 /* Return the address of the dtv for the current thread. */
185 # define THREAD_DTV() \
186 ({ dtv_t *_dtv; \
187 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
188 _dtv; })
190 # include <mach/machine/thread_status.h>
192 /* Set up TLS in the new thread of a fork child, copying from the original. */
193 static inline kern_return_t __attribute__ ((unused))
194 _hurd_tls_fork (thread_t child, thread_t orig, struct i386_thread_state *state)
196 /* Fetch the selector set by _hurd_tls_init. */
197 int sel;
198 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
199 if (sel == state->ds) /* _hurd_tls_init was never called. */
200 return 0;
202 struct descriptor desc, *_desc = &desc;
203 error_t err;
204 unsigned int count = 1;
206 if (HURD_SEL_LDT (sel))
207 err = __i386_get_ldt (orig, sel, 1, &_desc, &count);
208 else
209 err = __i386_get_gdt (orig, sel, &desc);
211 assert_perror (err);
212 if (err)
213 return err;
215 if (HURD_SEL_LDT (sel))
216 err = __i386_set_ldt (child, sel, &desc, 1);
217 else
218 err = __i386_set_gdt (child, &sel, desc);
220 state->gs = sel;
221 return err;
224 static inline kern_return_t __attribute__ ((unused))
225 _hurd_tls_new (thread_t child, struct i386_thread_state *state, tcbhead_t *tcb)
227 /* Fetch the selector set by _hurd_tls_init. */
228 int sel;
229 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
230 if (sel == state->ds) /* _hurd_tls_init was never called. */
231 return 0;
233 HURD_TLS_DESC_DECL (desc, tcb);
234 error_t err;
236 tcb->tcb = tcb;
237 tcb->self = child;
239 if (HURD_SEL_LDT (sel))
240 err = __i386_set_ldt (child, sel, &desc, 1);
241 else
242 err = __i386_set_gdt (child, &sel, desc);
244 state->gs = sel;
245 return err;
248 #endif /* !__ASSEMBLER__ */
250 #endif /* i386/tls.h */