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