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