Update copyright notices with scripts/update-copyrights
[glibc.git] / sysdeps / mach / hurd / i386 / tls.h
blob3f84300206bddff811429765c17c7667f9b05a7f
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003-2014 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>
26 /* The TCB can have any size and the memory following the address the
27 thread pointer points to is unspecified. Allocate the TCB there. */
28 #define TLS_TCB_AT_TP 1
30 #ifndef __ASSEMBLER__
32 /* Use i386-specific RPCs to arrange that %gs segment register prefix
33 addresses the TCB in each thread. */
34 # include <mach/i386/mach_i386.h>
36 # ifndef HAVE_I386_SET_GDT
37 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
38 # endif
40 # include <errno.h>
41 # include <assert.h>
43 # define HURD_TLS_DESC_DECL(desc, tcb) \
44 struct descriptor desc = \
45 { /* low word: */ \
46 0xffff /* limit 0..15 */ \
47 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
48 , /* high word: */ \
49 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
50 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
51 | (0xf << 16) /* limit 16..19 */ \
52 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
53 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
57 static inline const char * __attribute__ ((unused))
58 _hurd_tls_init (tcbhead_t *tcb, int secondcall)
60 HURD_TLS_DESC_DECL (desc, tcb);
62 if (!secondcall)
64 /* This field is used by TLS accesses to get our "thread pointer"
65 from the TLS point of view. */
66 tcb->tcb = tcb;
68 /* Cache our thread port. */
69 tcb->self = __mach_thread_self ();
71 /* Get the first available selector. */
72 int sel = -1;
73 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
74 if (err == MIG_BAD_ID)
76 /* Old kernel, use a per-thread LDT. */
77 sel = 0x27;
78 err = __i386_set_ldt (tcb->self, sel, &desc, 1);
79 assert_perror (err);
80 if (err)
81 return "i386_set_ldt failed";
83 else if (err)
85 assert_perror (err); /* Separate from above with different line #. */
86 return "i386_set_gdt failed";
89 /* Now install the new selector. */
90 asm volatile ("mov %w0, %%gs" :: "q" (sel));
92 else
94 /* Fetch the selector set by the first call. */
95 int sel;
96 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
97 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
99 error_t err = __i386_set_ldt (tcb->self, sel, &desc, 1);
100 assert_perror (err);
101 if (err)
102 return "i386_set_ldt failed";
104 else
106 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
107 assert_perror (err);
108 if (err)
109 return "i386_set_gdt failed";
113 return 0;
116 /* Code to initially initialize the thread pointer. This might need
117 special attention since 'errno' is not yet available and if the
118 operation can cause a failure 'errno' must not be touched. */
119 # define TLS_INIT_TP(descr, secondcall) \
120 _hurd_tls_init ((tcbhead_t *) (descr), (secondcall))
122 /* Return the TCB address of the current thread. */
123 # define THREAD_SELF \
124 ({ tcbhead_t *__tcb; \
125 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
126 : "i" (offsetof (tcbhead_t, tcb))); \
127 __tcb;})
129 /* Install new dtv for current thread. */
130 # define INSTALL_NEW_DTV(dtvp) \
131 ({ asm volatile ("movl %0,%%gs:%P1" \
132 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
134 /* Return the address of the dtv for the current thread. */
135 # define THREAD_DTV() \
136 ({ dtv_t *_dtv; \
137 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
138 _dtv; })
140 # include <mach/machine/thread_status.h>
142 /* Set up TLS in the new thread of a fork child, copying from our own. */
143 static inline error_t __attribute__ ((unused))
144 _hurd_tls_fork (thread_t child, struct i386_thread_state *state)
146 /* Fetch the selector set by _hurd_tls_init. */
147 int sel;
148 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
149 if (sel == state->ds) /* _hurd_tls_init was never called. */
150 return 0;
152 tcbhead_t *const tcb = THREAD_SELF;
153 HURD_TLS_DESC_DECL (desc, tcb);
154 error_t err;
156 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
157 err = __i386_set_ldt (child, sel, &desc, 1);
158 else
159 err = __i386_set_gdt (child, &sel, desc);
161 state->gs = sel;
162 return err;
165 #endif /* !__ASSEMBLER__ */
167 #endif /* i386/tls.h */