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
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)
45 #define HURD_TLS_DESC_DECL(desc, tcb) \
46 struct descriptor desc = \
48 0xffff /* limit 0..15 */ \
49 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
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
);
66 /* This field is used by TLS accesses to get our "thread pointer"
67 from the TLS point of view. */
70 /* Cache our thread port. */
71 tcb
->self
= __mach_thread_self ();
73 /* Get the first available selector. */
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. */
80 err
= __i386_set_ldt (tcb
->self
, sel
, &desc
, 1);
83 return "i386_set_ldt failed";
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
));
96 /* Fetch the selector set by the first call. */
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);
104 return "i386_set_ldt failed";
108 error_t err
= __i386_set_gdt (tcb
->self
, &sel
, desc
);
111 return "i386_set_gdt failed";
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))); \
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() \
140 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, 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. */
151 asm ("mov %%gs, %w0" : "=q" (sel
) : "0" (0));
152 if (sel
== state
->ds
) /* _hurd_tls_init was never called. */
155 tcbhead_t
*const tcb
= THREAD_SELF
;
156 HURD_TLS_DESC_DECL (desc
, tcb
);
159 if (__builtin_expect (sel
, 0x50) & 4) /* LDT selector */
160 err
= __i386_set_ldt (child
, sel
, &desc
, 1);
162 err
= __i386_set_gdt (child
, &sel
, desc
);
168 # endif /* !__ASSEMBLER__ */
169 #endif /* HAVE_TLS_SUPPORT */
171 #endif /* i386/tls.h */