1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef _INTERNAL_PTHREAD_IMPL_H
17 #define _INTERNAL_PTHREAD_IMPL_H
18
19 #include <pthread.h>
20 #include <signal.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <sys/mman.h>
24 #include "libc.h"
25 #include "syscall.h"
26 #include "atomic.h"
27 #include "futex.h"
28
29 #include "pthread_arch.h"
30
31 #define pthread __pthread
32 #define TLS_RESERVE_SLOT 15
33
34 struct pthread {
35 /* Part 1 -- these fields may be external or
36 * internal (accessed via asm) ABI. Do not change. */
37 struct pthread *self;
38 #ifndef TLS_ABOVE_TP
39 uintptr_t *dtv;
40 #endif
41 struct pthread *prev, *next; /* non-ABI */
42 uintptr_t sysinfo;
43 #ifndef TLS_ABOVE_TP
44 #ifdef CANARY_PAD
45 uintptr_t canary_pad;
46 #endif
47 uintptr_t canary;
48 #endif
49
50 /* Part 2 -- implementation details, non-ABI. */
51 int tid;
52 int pid;
53 int proc_tid;
54 int errno_val;
55 int by_vfork;
56 volatile int detach_state;
57 #ifdef FEATURE_PTHREAD_CANCEL
58 volatile int cancel;
59 volatile unsigned char canceldisable, cancelasync;
60 #endif
61 unsigned char tsd_used:1;
62 unsigned char dlerror_flag:1;
63 unsigned char *map_base;
64 size_t map_size;
65 void *stack;
66 size_t stack_size;
67 size_t guard_size;
68 void *result;
69 struct __ptcb *cancelbuf;
70 void **tsd;
71 struct {
72 volatile void *volatile head;
73 long off;
74 volatile void *volatile pending;
75 } robust_list;
76 int h_errno_val;
77 volatile int timer_id;
78 locale_t locale;
79 volatile int killlock[1];
80 char *dlerror_buf;
81 void *stdio_locks;
82 #ifdef RESERVE_SIGNAL_STACK
83 void *signal_stack;
84 #endif
85
86 #ifdef USE_GWP_ASAN
87 /* musl doesn't support libc using tls, so we reserve some slots here for gwp_asan to use. */
88 struct {
89 uint32_t random_state;
90 uint32_t next_sample_counter : 31;
91 char recursive_guard : 1;
92 } gwp_asan_tls;
93
94 #endif
95
96 /* Part 3 -- the positions of these fields relative to
97 * the end of the structure is external and internal ABI. */
98 #ifdef TLS_ABOVE_TP
99 /* The tls_slots will be accessed by kernel, so don't use it.
100 * To solve the problem that the kernel isn't synchronized with the musl,
101 * so make pre/post reserved slots for musl.
102 * pre-reserved : tls_slots[0-4]
103 * kernel used : tls_slots[5-9]
104 * post-reserved : tls_slot[10-14] */
105 void *tls_slots[TLS_RESERVE_SLOT];
106 uintptr_t canary;
107 uintptr_t *dtv;
108 #endif
109 };
110
111 enum {
112 DT_EXITED = 0,
113 DT_EXITING,
114 DT_JOINABLE,
115 DT_DETACHED,
116 };
117
118 #define __SU (sizeof(size_t)/sizeof(int))
119
120 #define _a_stacksize __u.__s[0]
121 #define _a_guardsize __u.__s[1]
122 #define _a_stackaddr __u.__s[2]
123 #define _a_detach __u.__i[3*__SU+0]
124 #define _a_sched __u.__i[3*__SU+1]
125 #define _a_policy __u.__i[3*__SU+2]
126 #define _a_prio __u.__i[3*__SU+3]
127
128 /* we define the original value of _m_* in include/pthread.h
129 * as macros MUTEX_* to make the user be able to
130 * access the inner attribute of the mutex struct.
131 * Then, we modify the value of _m_* macros to MUTEX_* here,
132 * so that we can immediately be aware of the changes that
133 * the open source society has made to these original macros,
134 * because patching will fail if the value of the _m_* are
135 * changed by musl society */
136 #define _m_type __u.__i[0]
137 #define _m_lock __u.__vi[1]
138 #define _m_waiters __u.__vi[2]
139 #define _m_prev __u.__p[3]
140 #define _m_next __u.__p[4]
141 #define _m_clock __u.__i[4]
142 #define _m_count __u.__i[5]
143
144 #define _c_shared __u.__p[0]
145 #define _c_seq __u.__vi[2]
146 #define _c_waiters __u.__vi[3]
147 #define _c_clock __u.__i[4]
148 #define _c_lock __u.__vi[8]
149 #define _c_head __u.__p[1]
150 #define _c_tail __u.__p[5]
151 #define _rw_lock __u.__vi[0]
152 #define _rw_waiters __u.__vi[1]
153 #define _rw_shared __u.__i[2]
154 #define _rw_clock __u.__i[4]
155 #define _b_lock __u.__vi[0]
156 #define _b_waiters __u.__vi[1]
157 #define _b_limit __u.__i[2]
158 #define _b_count __u.__vi[3]
159 #define _b_waiters2 __u.__vi[4]
160 #define _b_inst __u.__p[3]
161
162 #ifndef TP_OFFSET
163 #define TP_OFFSET 0
164 #endif
165
166 #ifndef DTP_OFFSET
167 #define DTP_OFFSET 0
168 #endif
169
170 #ifdef TLS_ABOVE_TP
171 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET)
172 #define __pthread_self() ((pthread_t)(__get_tp() - sizeof(struct __pthread) - TP_OFFSET))
173 #else
174 #define TP_ADJ(p) (p)
175 #define __pthread_self() ((pthread_t)__get_tp())
176 #endif
177
178 #ifndef tls_mod_off_t
179 #define tls_mod_off_t size_t
180 #endif
181
182 #define SIGTIMER 32
183 #define SIGCANCEL 33
184 #define SIGSYNCCALL 34
185
186 #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 })
187 #define SIGPT_SET \
188 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
189 [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) })
190 #define SIGTIMER_SET \
191 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
192 0x80000000 })
193
194 void *__tls_get_addr(tls_mod_off_t *);
195 hidden int __init_tp(void *);
196 hidden void *__copy_tls(unsigned char *);
197 hidden void __reset_tls();
198
199 hidden void __membarrier_init(void);
200 hidden void __dl_thread_cleanup(void);
201 hidden void __testcancel();
202 hidden void __do_cleanup_push(struct __ptcb *);
203 hidden void __do_cleanup_pop(struct __ptcb *);
204 hidden void __pthread_tsd_run_dtors();
205
206 hidden void __pthread_key_delete_synccall(void (*)(void *), void *);
207 hidden int __pthread_key_delete_impl(pthread_key_t);
208
209 extern hidden volatile size_t __pthread_tsd_size;
210 extern hidden void *__pthread_tsd_main[];
211 extern hidden volatile int __eintr_valid_flag;
212
213 hidden int __clone(int (*)(void *), void *, int, void *, ...);
214 hidden int __set_thread_area(void *);
215 hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *);
216 hidden void __unmapself(void *, size_t);
217
218 hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
219 hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int);
220 hidden void __wait(volatile int *, volatile int *, int, int);
__wake(volatile void *addr, int cnt, int priv)221 static inline void __wake(volatile void *addr, int cnt, int priv)
222 {
223 if (priv) priv = FUTEX_PRIVATE;
224 if (cnt<0) cnt = INT_MAX;
225 __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS ||
226 __syscall(SYS_futex, addr, FUTEX_WAKE, cnt);
227 }
__futexwait(volatile void *addr, int val, int priv)228 static inline void __futexwait(volatile void *addr, int val, int priv)
229 {
230 if (priv) priv = FUTEX_PRIVATE;
231 __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS ||
232 __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
233 }
234
235 #define MS_PER_S 1000
236 #define US_PER_S 1000000
__timespec_from_ms(struct timespec* ts, const unsigned ms)237 static inline void __timespec_from_ms(struct timespec* ts, const unsigned ms)
238 {
239 if (ts == NULL) {
240 return;
241 }
242 ts->tv_sec = ms / MS_PER_S;
243 ts->tv_nsec = (ms % MS_PER_S) * US_PER_S;
244 }
245
246 #define NS_PER_S 1000000000
__absolute_timespec_from_timespec(struct timespec *abs_ts, const struct timespec *ts, clockid_t clock)247 static inline void __absolute_timespec_from_timespec(struct timespec *abs_ts,
248 const struct timespec *ts, clockid_t clock)
249 {
250 if (abs_ts == NULL || ts == NULL) {
251 return;
252 }
253 clock_gettime(clock, abs_ts);
254 abs_ts->tv_sec += ts->tv_sec;
255 abs_ts->tv_nsec += ts->tv_nsec;
256 if (abs_ts->tv_nsec >= NS_PER_S) {
257 abs_ts->tv_nsec -= NS_PER_S;
258 abs_ts->tv_sec++;
259 }
260 }
261
262 #ifdef RESERVE_SIGNAL_STACK
263 hidden void pthread_reserve_signal_stack();
264 hidden void pthread_release_signal_stack();
265 #endif
266
267 hidden void __acquire_ptc(void);
268 hidden void __release_ptc(void);
269 hidden void __inhibit_ptc(void);
270
271 hidden void __tl_lock(void);
272 hidden void __tl_unlock(void);
273 hidden void __tl_sync(pthread_t);
274 hidden struct pthread* __pthread_list_find(pthread_t, const char*);
275
276 extern hidden volatile int __thread_list_lock;
277
278 extern hidden volatile int __abort_lock[1];
279
280 extern hidden unsigned __default_stacksize;
281 extern hidden unsigned __default_guardsize;
282
283 #ifdef TARGET_STACK_SIZE
284 #define DEFAULT_STACK_SIZE TARGET_STACK_SIZE
285 #else
286 #define DEFAULT_STACK_SIZE 131072
287 #endif
288
289 #ifdef TARGET_GUARD_SIZE
290 #define DEFAULT_GUARD_SIZE TARGET_GUARD_SIZE
291 #else
292 #define DEFAULT_GUARD_SIZE 8192
293 #endif
294
295 #define DEFAULT_STACK_MAX (8<<20)
296 #define DEFAULT_GUARD_MAX (1<<20)
297
298 #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1)
299
300 #endif
301