(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / sysdeps / mach / hurd / i386 / tls.h
blobff849716e0adfa4781cfe6fd68dfbc1874e05d24
1 /* Definitions for thread-local data handling. Hurd/i386 version.
2 Copyright (C) 2003, 2004 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 /* Indiciate that TLS support is available. */
29 # define USE_TLS 1
31 /* The TCB can have any size and the memory following the address the
32 thread pointer points to is unspecified. Allocate the TCB there. */
33 # define TLS_TCB_AT_TP 1
35 # ifndef ASSEMBLER
37 /* Use i386-specific RPCs to arrange that %gs segment register prefix
38 addresses the TCB in each thread. */
39 # include <mach/i386/mach_i386.h>
41 # ifndef HAVE_I386_SET_GDT
42 # define __i386_set_gdt(thr, sel, desc) ((void) (thr), (void) (sel), (void) (desc), MIG_BAD_ID)
43 # endif
45 # include <errno.h>
46 # include <assert.h>
48 #define HURD_TLS_DESC_DECL(desc, tcb) \
49 struct descriptor desc = \
50 { /* low word: */ \
51 0xffff /* limit 0..15 */ \
52 | (((unsigned int) (tcb)) << 16) /* base 0..15 */ \
53 , /* high word: */ \
54 ((((unsigned int) (tcb)) >> 16) & 0xff) /* base 16..23 */ \
55 | ((0x12 | 0x60 | 0x80) << 8) /* access = ACC_DATA_W|ACC_PL_U|ACC_P */ \
56 | (0xf << 16) /* limit 16..19 */ \
57 | ((4 | 8) << 20) /* granularity = SZ_32|SZ_G */ \
58 | (((unsigned int) (tcb)) & 0xff000000) /* base 24..31 */ \
62 static inline const char * __attribute__ ((unused))
63 _hurd_tls_init (tcbhead_t *tcb, int secondcall)
65 HURD_TLS_DESC_DECL (desc, tcb);
67 if (!secondcall)
69 /* This field is used by TLS accesses to get our "thread pointer"
70 from the TLS point of view. */
71 tcb->tcb = tcb;
73 /* Cache our thread port. */
74 tcb->self = __mach_thread_self ();
76 /* Get the first available selector. */
77 int sel = -1;
78 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
79 if (err == MIG_BAD_ID)
81 /* Old kernel, use a per-thread LDT. */
82 sel = 0x27;
83 err = __i386_set_ldt (tcb->self, sel, &desc, 1);
84 assert_perror (err);
85 if (err)
86 return "i386_set_ldt failed";
88 else if (err)
90 assert_perror (err); /* Separate from above with different line #. */
91 return "i386_set_gdt failed";
94 /* Now install the new selector. */
95 asm volatile ("mov %w0, %%gs" :: "q" (sel));
97 else
99 /* Fetch the selector set by the first call. */
100 int sel;
101 asm ("mov %%gs, %w0" : "=q" (sel));
102 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
104 error_t err = __i386_set_ldt (tcb->self, sel, &desc, 1);
105 assert_perror (err);
106 if (err)
107 return "i386_set_ldt failed";
109 else
111 error_t err = __i386_set_gdt (tcb->self, &sel, desc);
112 assert_perror (err);
113 if (err)
114 return "i386_set_gdt failed";
118 return 0;
121 /* Code to initially initialize the thread pointer. This might need
122 special attention since 'errno' is not yet available and if the
123 operation can cause a failure 'errno' must not be touched. */
124 # define TLS_INIT_TP(descr, secondcall) \
125 _hurd_tls_init ((tcbhead_t *) (descr), (secondcall))
126 # define TLS_INIT_TP_EXPENSIVE 1
128 /* Return the TCB address of the current thread. */
129 # define THREAD_SELF \
130 ({ tcbhead_t *__tcb; \
131 __asm__ ("movl %%gs:%c1,%0" : "=r" (__tcb) \
132 : "i" (offsetof (tcbhead_t, tcb))); \
133 __tcb;})
135 /* Install new dtv for current thread. */
136 # define INSTALL_NEW_DTV(dtvp) \
137 ({ asm volatile ("movl %0,%%gs:%P1" \
138 : : "ir" (dtvp), "i" (offsetof (tcbhead_t, dtv))); })
140 /* Return the address of the dtv for the current thread. */
141 # define THREAD_DTV() \
142 ({ dtv_t *_dtv; \
143 asm ("movl %%gs:%P1,%0" : "=q" (_dtv) : "i" (offsetof (tcbhead_t, dtv)));\
144 _dtv; })
146 #include <mach/machine/thread_status.h>
148 /* Set up TLS in the new thread of a fork child, copying from our own. */
149 static inline error_t __attribute__ ((unused))
150 _hurd_tls_fork (thread_t child, struct i386_thread_state *state)
152 /* Fetch the selector set by _hurd_tls_init. */
153 int sel;
154 asm ("mov %%gs, %w0" : "=q" (sel));
155 if (sel == state->ds) /* _hurd_tls_init was never called. */
156 return 0;
158 tcbhead_t *const tcb = THREAD_SELF;
159 HURD_TLS_DESC_DECL (desc, tcb);
160 error_t err;
162 if (__builtin_expect (sel, 0x50) & 4) /* LDT selector */
163 err = __i386_set_ldt (child, sel, &desc, 1);
164 else
165 err = __i386_set_gdt (child, &sel, desc);
167 state->gs = sel;
168 return err;
171 # endif /* !ASSEMBLER */
172 #endif /* HAVE_TLS_SUPPORT */
174 #endif /* i386/tls.h */