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