2.9
[glibc/nacl-glibc.git] / sysdeps / mach / hurd / i386 / tls.h
blobd98b485b96ebf92d8a34f569f6b01ab57f4be60a
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003, 2004, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifndef _I386_TLS_H
21 #define _I386_TLS_H
23 #if defined HAVE_TLS_SUPPORT
25 /* Some things really need not be machine-dependent. */
26 # include <sysdeps/mach/hurd/tls.h>
28 /* The TCB can have any size and the memory following the address the
29 thread pointer points to is unspecified. Allocate the TCB there. */
30 # define TLS_TCB_AT_TP 1
32 # ifndef __ASSEMBLER__
34 /* Use i386-specific RPCs to arrange that %gs segment register prefix
35 addresses the TCB in each thread. */
36 # include <mach/i386/mach_i386.h>
38 # ifndef HAVE_I386_SET_GDT
39 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
40 # endif
42 # include <errno.h>
43 # include <assert.h>
45 #define HURD_TLS_DESC_DECL(desc, tcb) \
46 struct descriptor desc = \
47 { /* low word: */ \
48 0xffff /* limit 0..15 */ \
49 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
50 , /* high word: */ \
51 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
52 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
53 | (0xf << 16) /* limit 16..19 */ \
54 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
55 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
59 static inline const char * __attribute__ ((unused))
60 _hurd_tls_init (tcbhead_t *tcb, int secondcall)
62 HURD_TLS_DESC_DECL (desc, tcb);
64 if (!secondcall)
66 /* This field is used by TLS accesses to get our "thread pointer"
67 from the TLS point of view. */
68 tcb->tcb = tcb;
70 /* Cache our thread port. */
71 tcb->self = __mach_thread_self ();
73 /* Get the first available selector. */
74 int sel = -1;
75 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
76 if (err == MIG_BAD_ID)
78 /* Old kernel, use a per-thread LDT. */
79 sel = 0x27;
80 err = __i386_set_ldt (tcb->self, sel, &desc, 1);
81 assert_perror (err);
82 if (err)
83 return "i386_set_ldt failed";
85 else if (err)
87 assert_perror (err); /* Separate from above with different line #. */
88 return "i386_set_gdt failed";
91 /* Now install the new selector. */
92 asm volatile ("mov %w0, %%gs" :: "q" (sel));
94 else
96 /* Fetch the selector set by the first call. */
97 int sel;
98 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
99 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
101 error_t err = __i386_set_ldt (tcb->self, sel, &desc, 1);
102 assert_perror (err);
103 if (err)
104 return "i386_set_ldt failed";
106 else
108 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
109 assert_perror (err);
110 if (err)
111 return "i386_set_gdt failed";
115 return 0;
118 /* Code to initially initialize the thread pointer. This might need
119 special attention since 'errno' is not yet available and if the
120 operation can cause a failure 'errno' must not be touched. */
121 # define TLS_INIT_TP(descr, secondcall) \
122 _hurd_tls_init ((tcbhead_t *) (descr), (secondcall))
123 # define TLS_INIT_TP_EXPENSIVE 1
125 /* Return the TCB address of the current thread. */
126 # define THREAD_SELF \
127 ({ tcbhead_t *__tcb; \
128 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
129 : "i" (offsetof (tcbhead_t, tcb))); \
130 __tcb;})
132 /* Install new dtv for current thread. */
133 # define INSTALL_NEW_DTV(dtvp) \
134 ({ asm volatile ("movl %0,%%gs:%P1" \
135 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
137 /* Return the address of the dtv for the current thread. */
138 # define THREAD_DTV() \
139 ({ dtv_t *_dtv; \
140 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
141 _dtv; })
143 #include <mach/machine/thread_status.h>
145 /* Set up TLS in the new thread of a fork child, copying from our own. */
146 static inline error_t __attribute__ ((unused))
147 _hurd_tls_fork (thread_t child, struct i386_thread_state *state)
149 /* Fetch the selector set by _hurd_tls_init. */
150 int sel;
151 asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
152 if (sel == state->ds) /* _hurd_tls_init was never called. */
153 return 0;
155 tcbhead_t *const tcb = THREAD_SELF;
156 HURD_TLS_DESC_DECL (desc, tcb);
157 error_t err;
159 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
160 err = __i386_set_ldt (child, sel, &desc, 1);
161 else
162 err = __i386_set_gdt (child, &sel, desc);
164 state->gs = sel;
165 return err;
168 # endif /* !__ASSEMBLER__ */
169 #endif /* HAVE_TLS_SUPPORT */
171 #endif /* i386/tls.h */