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