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
33 struct pthread {
34 /* Part 1 -- these fields may be external or
35 * internal (accessed via asm) ABI. Do not change. */
36 struct pthread *self;
37 #ifndef TLS_ABOVE_TP
38 uintptr_t *dtv;
39 #endif
40 struct pthread *prev, *next; /* non-ABI */
41 uintptr_t sysinfo;
42 #ifndef TLS_ABOVE_TP
43 #ifdef CANARY_PAD
44 uintptr_t canary_pad;
45 #endif
46 uintptr_t canary;
47 #endif
48
49 /* Part 2 -- implementation details, non-ABI. */
50 int tid;
51 int pid;
52 int errno_val;
53 volatile int detach_state;
54 #ifdef FEATURE_PTHREAD_CANCEL
55 volatile int cancel;
56 volatile unsigned char canceldisable, cancelasync;
57 #endif
58 unsigned char tsd_used:1;
59 unsigned char dlerror_flag:1;
60 unsigned char *map_base;
61 size_t map_size;
62 void *stack;
63 size_t stack_size;
64 size_t guard_size;
65 void *result;
66 struct __ptcb *cancelbuf;
67 void **tsd;
68 struct {
69 volatile void *volatile head;
70 long off;
71 volatile void *volatile pending;
72 } robust_list;
73 int h_errno_val;
74 volatile int timer_id;
75 locale_t locale;
76 volatile int killlock[1];
77 char *dlerror_buf;
78 void *stdio_locks;
79 #ifdef RESERVE_SIGNAL_STACK
80 void *signal_stack;
81 #endif
82
83 /* Part 3 -- the positions of these fields relative to
84 * the end of the structure is external and internal ABI. */
85 #ifdef TLS_ABOVE_TP
86 uintptr_t canary;
87 uintptr_t *dtv;
88 #endif
89 };
90
91 enum {
92 DT_EXITED = 0,
93 DT_EXITING,
94 DT_JOINABLE,
95 DT_DETACHED,
96 };
97
98 #define __SU (sizeof(size_t)/sizeof(int))
99
100 #define _a_stacksize __u.__s[0]
101 #define _a_guardsize __u.__s[1]
102 #define _a_stackaddr __u.__s[2]
103 #define _a_detach __u.__i[3*__SU+0]
104 #define _a_sched __u.__i[3*__SU+1]
105 #define _a_policy __u.__i[3*__SU+2]
106 #define _a_prio __u.__i[3*__SU+3]
107
108 /* we define the original value of _m_* in include/pthread.h
109 * as macros MUTEX_* to make the user be able to
110 * access the inner attribute of the mutex struct.
111 * Then, we modify the value of _m_* macros to MUTEX_* here,
112 * so that we can immediately be aware of the changes that
113 * the open source society has made to these original macros,
114 * because patching will fail if the value of the _m_* are
115 * changed by musl society */
116 #define _m_type __u.__i[0]
117 #define _m_lock __u.__vi[1]
118 #define _m_waiters __u.__vi[2]
119 #define _m_prev __u.__p[3]
120 #define _m_next __u.__p[4]
121 #define _m_clock __u.__i[4]
122 #define _m_count __u.__i[5]
123
124 #define _c_shared __u.__p[0]
125 #define _c_seq __u.__vi[2]
126 #define _c_waiters __u.__vi[3]
127 #define _c_clock __u.__i[4]
128 #define _c_lock __u.__vi[8]
129 #define _c_head __u.__p[1]
130 #define _c_tail __u.__p[5]
131 #define _rw_lock __u.__vi[0]
132 #define _rw_waiters __u.__vi[1]
133 #define _rw_shared __u.__i[2]
134 #define _rw_clock __u.__i[4]
135 #define _b_lock __u.__vi[0]
136 #define _b_waiters __u.__vi[1]
137 #define _b_limit __u.__i[2]
138 #define _b_count __u.__vi[3]
139 #define _b_waiters2 __u.__vi[4]
140 #define _b_inst __u.__p[3]
141
142 #ifndef TP_OFFSET
143 #define TP_OFFSET 0
144 #endif
145
146 #ifndef DTP_OFFSET
147 #define DTP_OFFSET 0
148 #endif
149
150 #ifdef TLS_ABOVE_TP
151 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + TP_OFFSET)
152 #define __pthread_self() ((pthread_t)(__get_tp() - sizeof(struct __pthread) - TP_OFFSET))
153 #else
154 #define TP_ADJ(p) (p)
155 #define __pthread_self() ((pthread_t)__get_tp())
156 #endif
157
158 #ifndef tls_mod_off_t
159 #define tls_mod_off_t size_t
160 #endif
161
162 #define SIGTIMER 32
163 #define SIGCANCEL 33
164 #define SIGSYNCCALL 34
165
166 #define SIGALL_SET ((sigset_t *)(const unsigned long long [2]){ -1,-1 })
167 #define SIGPT_SET \
168 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
169 [sizeof(long)==4] = 3UL<<(32*(sizeof(long)>4)) })
170 #define SIGTIMER_SET \
171 ((sigset_t *)(const unsigned long [_NSIG/8/sizeof(long)]){ \
172 0x80000000 })
173
174 void *__tls_get_addr(tls_mod_off_t *);
175 hidden int __init_tp(void *);
176 hidden void *__copy_tls(unsigned char *);
177 hidden void __reset_tls();
178
179 hidden void __membarrier_init(void);
180 hidden void __dl_thread_cleanup(void);
181 hidden void __testcancel();
182 hidden void __do_cleanup_push(struct __ptcb *);
183 hidden void __do_cleanup_pop(struct __ptcb *);
184 hidden void __pthread_tsd_run_dtors();
185
186 hidden void __pthread_key_delete_synccall(void (*)(void *), void *);
187 hidden int __pthread_key_delete_impl(pthread_key_t);
188
189 extern hidden volatile size_t __pthread_tsd_size;
190 extern hidden void *__pthread_tsd_main[];
191 extern hidden volatile int __eintr_valid_flag;
192
193 hidden int __clone(int (*)(void *), void *, int, void *, ...);
194 hidden int __set_thread_area(void *);
195 hidden int __libc_sigaction(int, const struct sigaction *, struct sigaction *);
196 hidden void __unmapself(void *, size_t);
197
198 hidden int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
199 hidden int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int);
200 hidden void __wait(volatile int *, volatile int *, int, int);
__wake(volatile void * addr,int cnt,int priv)201 static inline void __wake(volatile void *addr, int cnt, int priv)
202 {
203 if (priv) priv = FUTEX_PRIVATE;
204 if (cnt<0) cnt = INT_MAX;
205 __syscall(SYS_futex, addr, FUTEX_WAKE|priv, cnt) != -ENOSYS ||
206 __syscall(SYS_futex, addr, FUTEX_WAKE, cnt);
207 }
__futexwait(volatile void * addr,int val,int priv)208 static inline void __futexwait(volatile void *addr, int val, int priv)
209 {
210 if (priv) priv = FUTEX_PRIVATE;
211 __syscall(SYS_futex, addr, FUTEX_WAIT|priv, val, 0) != -ENOSYS ||
212 __syscall(SYS_futex, addr, FUTEX_WAIT, val, 0);
213 }
214
215 #define MS_PER_S 1000
216 #define US_PER_S 1000000
__timespec_from_ms(struct timespec * ts,const unsigned ms)217 static inline void __timespec_from_ms(struct timespec* ts, const unsigned ms)
218 {
219 if (ts == NULL) {
220 return;
221 }
222 ts->tv_sec = ms / MS_PER_S;
223 ts->tv_nsec = (ms % MS_PER_S) * US_PER_S;
224 }
225
226 #define NS_PER_S 1000000000
__absolute_timespec_from_timespec(struct timespec * abs_ts,const struct timespec * ts,clockid_t clock)227 static inline void __absolute_timespec_from_timespec(struct timespec *abs_ts,
228 const struct timespec *ts, clockid_t clock)
229 {
230 if (abs_ts == NULL || ts == NULL) {
231 return;
232 }
233 clock_gettime(clock, abs_ts);
234 abs_ts->tv_sec += ts->tv_sec;
235 abs_ts->tv_nsec += ts->tv_nsec;
236 if (abs_ts->tv_nsec >= NS_PER_S) {
237 abs_ts->tv_nsec -= NS_PER_S;
238 abs_ts->tv_sec++;
239 }
240 }
241
242 #ifdef RESERVE_SIGNAL_STACK
243 hidden void pthread_reserve_signal_stack();
244 hidden void pthread_release_signal_stack();
245 #endif
246
247 hidden void __acquire_ptc(void);
248 hidden void __release_ptc(void);
249 hidden void __inhibit_ptc(void);
250
251 hidden void __tl_lock(void);
252 hidden void __tl_unlock(void);
253 hidden void __tl_sync(pthread_t);
254 hidden struct pthread* __pthread_list_find(pthread_t, const char*);
255
256 extern hidden volatile int __thread_list_lock;
257
258 extern hidden volatile int __abort_lock[1];
259
260 extern hidden unsigned __default_stacksize;
261 extern hidden unsigned __default_guardsize;
262
263 #ifdef TARGET_STACK_SIZE
264 #define DEFAULT_STACK_SIZE TARGET_STACK_SIZE
265 #else
266 #define DEFAULT_STACK_SIZE 131072
267 #endif
268
269 #ifdef TARGET_GUARD_SIZE
270 #define DEFAULT_GUARD_SIZE TARGET_GUARD_SIZE
271 #else
272 #define DEFAULT_GUARD_SIZE 8192
273 #endif
274
275 #define DEFAULT_STACK_MAX (8<<20)
276 #define DEFAULT_GUARD_MAX (1<<20)
277
278 #define __ATTRP_C11_THREAD ((void*)(uintptr_t)-1)
279
280 #endif
281