]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/nptl/sysdeps/mips/tls.h
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / nptl / sysdeps / mips / tls.h
1 /* Definition for thread-local data handling.  NPTL/MIPS version.
2    Copyright (C) 2005, 2007 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  1
21
22 #ifndef __ASSEMBLER__
23 # include <stdbool.h>
24 # include <stddef.h>
25 # include <stdint.h>
26
27 /* Type for the dtv.  */
28 typedef union dtv
29 {
30   size_t counter;
31   struct
32   {
33     void *val;
34     bool is_static;
35   } pointer;
36 } dtv_t;
37
38 /* Note: rd must be $v1 to be ABI-conformant.  */
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 # define READ_THREAD_POINTER(rd) \
49         .set    push;                                                         \
50         .set    mips32r2;                                                     \
51         rdhwr   rd, $29;                                                      \
52         .set    pop
53 #endif /* __ASSEMBLER__ */
54
55
56 /* We require TLS support in the tools.  */
57 #define HAVE_TLS_SUPPORT                1
58 #define HAVE_TLS_MODEL_ATTRIBUTE        1
59 #define HAVE___THREAD                   1
60
61 /* Signal that TLS support is available.  */
62 #define USE_TLS 1
63
64 #ifndef __ASSEMBLER__
65
66 /* Get system call information.  */
67 # include <sysdep.h>
68
69 /* The TP points to the start of the thread blocks.  */
70 # define TLS_DTV_AT_TP  1
71
72 /* Get the thread descriptor definition.  */
73 #include <../../descr.h>
74
75 typedef struct
76 {
77   dtv_t *dtv;
78   void *private;
79 } tcbhead_t;
80
81 /* This is the size of the initial TCB.  Because our TCB is before the thread
82    pointer, we don't need this.  */
83 # define TLS_INIT_TCB_SIZE      0
84
85 /* Alignment requirements for the initial TCB.  */
86 # define TLS_INIT_TCB_ALIGN     __alignof__ (struct pthread)
87
88 /* This is the size of the TCB.  Because our TCB is before the thread
89    pointer, we don't need this.  */
90 # define TLS_TCB_SIZE           0
91
92 /* Alignment requirements for the TCB.  */
93 # define TLS_TCB_ALIGN          __alignof__ (struct pthread)
94
95 /* This is the size we need before TCB - actually, it includes the TCB.  */
96 # define TLS_PRE_TCB_SIZE \
97   (sizeof (struct pthread)                                                    \
98    + ((sizeof (tcbhead_t) + TLS_TCB_ALIGN - 1) & ~(TLS_TCB_ALIGN - 1)))
99
100 /* The thread pointer (in hardware register $29) points to the end of
101    the TCB + 0x7000, as for PowerPC.  The pthread_descr structure is
102    immediately in front of the TCB.  */
103 # define TLS_TCB_OFFSET 0x7000
104
105 /* Install the dtv pointer.  The pointer passed is to the element with
106    index -1 which contain the length.  */
107 # define INSTALL_DTV(tcbp, dtvp) \
108   (((tcbhead_t *) (tcbp))[-1].dtv = (dtvp) + 1)
109
110 /* Install new dtv for current thread.  */
111 # define INSTALL_NEW_DTV(dtv) \
112   (THREAD_DTV() = (dtv))
113
114 /* Return dtv of given thread descriptor.  */
115 # define GET_DTV(tcbp) \
116   (((tcbhead_t *) (tcbp))[-1].dtv)
117
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(tcbp, secondcall) \
122   ({ INTERNAL_SYSCALL_DECL (err);                                       \
123      long result_var attribute_unused;                                  \
124      result_var = INTERNAL_SYSCALL (set_thread_area, err, 1,            \
125                                     (char *) (tcbp) + TLS_TCB_OFFSET);  \
126      INTERNAL_SYSCALL_ERROR_P (result_var, err)                         \
127        ? "unknown error" : NULL; })
128
129 /* Return the address of the dtv for the current thread.  */
130 # define THREAD_DTV() \
131   (((tcbhead_t *) (READ_THREAD_POINTER () - TLS_TCB_OFFSET))[-1].dtv)
132
133 /* Return the thread descriptor for the current thread.  */
134 # define THREAD_SELF \
135  ((struct pthread *) (READ_THREAD_POINTER ()                         \
136                       - TLS_TCB_OFFSET - TLS_PRE_TCB_SIZE))
137
138 /* Magic for libthread_db to know how to do THREAD_SELF.  */
139 # define DB_THREAD_SELF \
140   CONST_THREAD_AREA (32, TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE)
141
142 /* Access to data in the thread descriptor is easy.  */
143 # define THREAD_GETMEM(descr, member) \
144   descr->member
145 # define THREAD_GETMEM_NC(descr, member, idx) \
146   descr->member[idx]
147 # define THREAD_SETMEM(descr, member, value) \
148   descr->member = (value)
149 # define THREAD_SETMEM_NC(descr, member, idx, value) \
150   descr->member[idx] = (value)
151
152 /* l_tls_offset == 0 is perfectly valid on MIPS, so we have to use some
153    different value to mean unset l_tls_offset.  */
154 # define NO_TLS_OFFSET          -1
155 /* Get and set the global scope generation counter in struct pthread.  */
156 #define THREAD_GSCOPE_FLAG_UNUSED 0
157 #define THREAD_GSCOPE_FLAG_USED   1
158 #define THREAD_GSCOPE_FLAG_WAIT   2
159 #define THREAD_GSCOPE_RESET_FLAG() \
160   do                                                                         \
161     { int __res                                                              \
162         = atomic_exchange_rel (&THREAD_SELF->header.gscope_flag,             \
163                                THREAD_GSCOPE_FLAG_UNUSED);                   \
164       if (__res == THREAD_GSCOPE_FLAG_WAIT)                                  \
165         lll_futex_wake (&THREAD_SELF->header.gscope_flag, 1, LLL_PRIVATE);   \
166     }                                                                        \
167   while (0)
168 #define THREAD_GSCOPE_SET_FLAG() \
169   do                                                                         \
170     {                                                                        \
171       THREAD_SELF->header.gscope_flag = THREAD_GSCOPE_FLAG_USED;             \
172       atomic_write_barrier ();                                               \
173     }                                                                        \
174   while (0)
175 #define THREAD_GSCOPE_WAIT() \
176   GL(dl_wait_lookup_done) ()
177
178 #endif /* __ASSEMBLER__ */
179
180 #endif  /* tls.h */