OSDN Git Service

4e7887a744cfe4548af81c118bbcd1c5647b97c6
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / mips / tls.h
1 /* Definitions for thread-local data handling.  linuxthreads/MIPS version.
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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/>.  */
18
19 #ifndef _TLS_H
20 #define _TLS_H
21
22 #ifndef __ASSEMBLER__
23
24 # include <stdbool.h>
25 # include <pt-machine.h>
26 # include <stddef.h>
27
28 /* Type for the dtv.  */
29 typedef union dtv
30 {
31   size_t counter;
32   struct
33   {
34     void *val;
35     bool is_static;
36   } pointer;
37 } dtv_t;
38
39 # define READ_THREAD_POINTER() \
40     ({ void *__result;                                                        \
41        __asm__ __volatile__ (".set\tpush\n\t.set\tmips32r2\n\t"                       \
42                      "rdhwr\t%0, $29\n\t.set\tpop" : "=v" (__result));        \
43        __result; })
44
45 #else /* __ASSEMBLER__ */
46 # include <tcb-offsets.h>
47
48 /* Note: rd must be $v1 to be ABI-conformant.  */
49 # define READ_THREAD_POINTER(rd) \
50         .set    push;                                                         \
51         .set    mips32r2;                                                     \
52         rdhwr   rd, $29;                                                      \
53         .set    pop
54 #endif /* __ASSEMBLER__ */
55
56 /* LinuxThreads can only use TLS if both floating stacks (in the MIPS case,
57    that means support for "rdhwr") and support from the tools are available.
58
59    We have to define USE_TLS consistently, or ldsodefs.h will lay out types
60    differently between an NPTL build and a LinuxThreads build.  It can be set
61    for libc.so and not libpthread.so, but only if we provide appropriate padding
62    in the _pthread_descr_struct.
63
64    Currently nothing defines FLOATING_STACKS.  We could assume this based on
65    kernel version once the TLS patches are available in kernel.org, but
66    it hardly seems worth it.  Use NPTL if you can.
67
68    To avoid bothering with the TLS support code at all, use configure
69    --without-tls.  */
70
71 #if defined HAVE_TLS_SUPPORT \
72     && (defined FLOATING_STACKS || !defined IS_IN_libpthread)
73
74 /* Signal that TLS support is available.  */
75 # define USE_TLS        1
76
77 /* Include padding in _pthread_descr_struct so that libc can find p_errno,
78    if libpthread will only include the padding because of the !IS_IN_libpthread
79    check.  */
80 #ifndef FLOATING_STACKS
81 # define INCLUDE_TLS_PADDING    1
82 #endif
83
84 # ifndef __ASSEMBLER__
85
86 /* This layout is actually wholly private and not affected by the ABI.
87    Nor does it overlap the pthread data structure, so we need nothing
88    extra here at all.  */
89 typedef struct
90 {
91   dtv_t *dtv;
92   void *private;
93 } tcbhead_t;
94
95 /* This is the size of the initial TCB.  */
96 #  define TLS_INIT_TCB_SIZE     0
97
98 /* Alignment requirements for the initial TCB.  */
99 #  define TLS_INIT_TCB_ALIGN    __alignof__ (struct _pthread_descr_struct)
100
101 /* This is the size of the TCB.  */
102 #  define TLS_TCB_SIZE          0
103
104 /* Alignment requirements for the TCB.  */
105 #  define TLS_TCB_ALIGN         __alignof__ (struct _pthread_descr_struct)
106
107 /* This is the size we need before TCB.  */
108 #  define TLS_PRE_TCB_SIZE \
109   (sizeof (struct _pthread_descr_struct)                                      \
110    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
111
112 /* The thread pointer (in hardware register $29) points to the end of
113    the TCB + 0x7000, as for PowerPC.  The pthread_descr structure is
114    immediately in front of the TCB.  */
115 #define TLS_TCB_OFFSET          0x7000
116
117 /* The DTV is allocated at the TP; the TCB is placed elsewhere.  */
118 /* This is not really true for powerpc64.  We are following alpha
119    where the DTV pointer is first doubleword in the TCB.  */
120 #  define TLS_DTV_AT_TP 1
121
122 /* Install the dtv pointer.  The pointer passed is to the element with
123    index -1 which contain the length.  */
124 #  define INSTALL_DTV(TCBP, DTVP) \
125   (((tcbhead_t *) (TCBP))[-1].dtv = (DTVP) + 1)
126
127 /* Install new dtv for current thread.  */
128 #  define INSTALL_NEW_DTV(DTV) (THREAD_DTV() = (DTV))
129
130 /* Return dtv of given thread descriptor.  */
131 #  define GET_DTV(TCBP) (((tcbhead_t *) (TCBP))[-1].dtv)
132
133 /* Code to initially initialize the thread pointer.  This might need
134    special attention since 'errno' is not yet available and if the
135    operation can cause a failure 'errno' must not be touched.  */
136 # define TLS_INIT_TP(tcbp, secondcall) \
137   ({ INTERNAL_SYSCALL_DECL (err);                                       \
138      long result_var;                                                   \
139      result_var = INTERNAL_SYSCALL (set_thread_area, err, 1,            \
140                                     (char *) (tcbp) + TLS_TCB_OFFSET);  \
141      INTERNAL_SYSCALL_ERROR_P (result_var, err)                         \
142        ? "unknown error" : NULL; })
143
144 /* Return the address of the dtv for the current thread.  */
145 # define THREAD_DTV() \
146   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
147
148 /* Return the thread descriptor for the current thread.  */
149 #  undef THREAD_SELF
150 #  define THREAD_SELF \
151     ((pthread_descr) (READ_THREAD_POINTER () \
152                       - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
153
154 /* Get the thread descriptor definition.  */
155 #  include <linuxthreads/descr.h>
156
157 /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
158    different value to mean unset l_tls_offset.  */
159 #  define NO_TLS_OFFSET -1
160
161 /* Initializing the thread pointer requires a syscall which may not be
162    available, so don't do it if we don't need to.  */
163 #  define TLS_INIT_TP_EXPENSIVE 1
164
165 # endif /* __ASSEMBLER__ */
166
167 #endif /* HAVE_TLS_SUPPORT */
168
169 #endif  /* tls.h */