]>
Commit | Line | Data |
---|---|---|
39236c6e | 1 | /* |
39037602 | 2 | * Copyright (c) 2012-2016 Apple Inc. All rights reserved. |
39236c6e A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
14 | * | |
15 | * Please obtain a copy of the License at | |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
25 | * | |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
27 | */ | |
28 | ||
29 | #define PTHREAD_INTERNAL 1 | |
30 | ||
5ba3f43e | 31 | #include <stdatomic.h> |
39236c6e A |
32 | #include <kern/debug.h> |
33 | #include <kern/mach_param.h> | |
34 | #include <kern/sched_prim.h> | |
35 | #include <kern/task.h> | |
36 | #include <kern/thread.h> | |
37 | #include <kern/affinity.h> | |
38 | #include <kern/zalloc.h> | |
39037602 A |
39 | #include <kern/policy_internal.h> |
40 | ||
39236c6e A |
41 | #include <machine/machine_routines.h> |
42 | #include <mach/task.h> | |
43 | #include <mach/thread_act.h> | |
44 | #include <sys/param.h> | |
5ba3f43e | 45 | #include <sys/eventvar.h> |
39236c6e | 46 | #include <sys/pthread_shims.h> |
5ba3f43e | 47 | #include <sys/proc_info.h> |
39236c6e A |
48 | #include <sys/proc_internal.h> |
49 | #include <sys/sysproto.h> | |
50 | #include <sys/systm.h> | |
51 | #include <vm/vm_map.h> | |
52 | #include <vm/vm_protos.h> | |
39037602 | 53 | #include <kern/kcdata.h> |
39236c6e A |
54 | |
55 | /* version number of the in-kernel shims given to pthread.kext */ | |
56 | #define PTHREAD_SHIMS_VERSION 1 | |
57 | ||
fe8ab488 | 58 | /* on arm, the callbacks function has two #ifdef arm ponters */ |
5ba3f43e A |
59 | #if defined(__arm__) |
60 | #define PTHREAD_CALLBACK_MEMBER map_is_1gb | |
61 | #else | |
fe8ab488 | 62 | #define PTHREAD_CALLBACK_MEMBER ml_get_max_cpus |
5ba3f43e | 63 | #endif |
fe8ab488 A |
64 | |
65 | /* compile time asserts to check the length of structures in pthread_shims.h */ | |
39037602 A |
66 | static_assert((sizeof(struct pthread_functions_s) - offsetof(struct pthread_functions_s, psynch_rw_yieldwrlock) - sizeof(void*)) == (sizeof(void*) * 100)); |
67 | static_assert((sizeof(struct pthread_callbacks_s) - offsetof(struct pthread_callbacks_s, PTHREAD_CALLBACK_MEMBER) - sizeof(void*)) == (sizeof(void*) * 100)); | |
fe8ab488 | 68 | |
39236c6e A |
69 | /* old pthread code had definitions for these as they don't exist in headers */ |
70 | extern kern_return_t mach_port_deallocate(ipc_space_t, mach_port_name_t); | |
71 | extern kern_return_t semaphore_signal_internal_trap(mach_port_name_t); | |
72 | ||
73 | #define PTHREAD_STRUCT_ACCESSOR(get, set, rettype, structtype, member) \ | |
74 | static rettype \ | |
75 | get(structtype x) { \ | |
76 | return (x)->member; \ | |
77 | } \ | |
78 | static void \ | |
79 | set(structtype x, rettype y) { \ | |
80 | (x)->member = y; \ | |
81 | } | |
39037602 | 82 | |
39236c6e A |
83 | PTHREAD_STRUCT_ACCESSOR(proc_get_threadstart, proc_set_threadstart, user_addr_t, struct proc*, p_threadstart); |
84 | PTHREAD_STRUCT_ACCESSOR(proc_get_pthsize, proc_set_pthsize, int, struct proc*, p_pthsize); | |
85 | PTHREAD_STRUCT_ACCESSOR(proc_get_wqthread, proc_set_wqthread, user_addr_t, struct proc*, p_wqthread); | |
fe8ab488 | 86 | PTHREAD_STRUCT_ACCESSOR(proc_get_stack_addr_hint, proc_set_stack_addr_hint, user_addr_t, struct proc *, p_stack_addr_hint); |
39236c6e A |
87 | PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_offset, proc_set_dispatchqueue_offset, uint64_t, struct proc*, p_dispatchqueue_offset); |
88 | PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_serialno_offset, proc_set_dispatchqueue_serialno_offset, uint64_t, struct proc*, p_dispatchqueue_serialno_offset); | |
fe8ab488 | 89 | PTHREAD_STRUCT_ACCESSOR(proc_get_pthread_tsd_offset, proc_set_pthread_tsd_offset, uint32_t, struct proc *, p_pth_tsd_offset); |
5ba3f43e | 90 | PTHREAD_STRUCT_ACCESSOR(proc_get_mach_thread_self_tsd_offset, proc_set_mach_thread_self_tsd_offset, uint64_t, struct proc *, p_mach_thread_self_offset); |
39236c6e | 91 | PTHREAD_STRUCT_ACCESSOR(proc_get_pthhash, proc_set_pthhash, void*, struct proc*, p_pthhash); |
5ba3f43e A |
92 | PTHREAD_STRUCT_ACCESSOR(proc_get_return_to_kernel_offset, proc_set_return_to_kernel_offset, uint64_t, struct proc*, p_return_to_kernel_offset); |
93 | PTHREAD_STRUCT_ACCESSOR(proc_get_user_stack, proc_set_user_stack, user_addr_t, struct proc*, user_stack); | |
39236c6e A |
94 | |
95 | PTHREAD_STRUCT_ACCESSOR(uthread_get_threadlist, uthread_set_threadlist, void*, struct uthread*, uu_threadlist); | |
96 | PTHREAD_STRUCT_ACCESSOR(uthread_get_sigmask, uthread_set_sigmask, sigset_t, struct uthread*, uu_sigmask); | |
97 | PTHREAD_STRUCT_ACCESSOR(uthread_get_returnval, uthread_set_returnval, int, struct uthread*, uu_rval[0]); | |
98 | ||
39037602 A |
99 | #define WQPTR_IS_INITING_VALUE ((void *)~(uintptr_t)0) |
100 | ||
101 | static void * | |
102 | proc_get_wqptr(struct proc *p) { | |
103 | void *wqptr = p->p_wqptr; | |
104 | return (wqptr == WQPTR_IS_INITING_VALUE) ? NULL : wqptr; | |
105 | } | |
106 | static void | |
107 | proc_set_wqptr(struct proc *p, void *y) { | |
108 | proc_lock(p); | |
109 | ||
110 | assert(y == NULL || p->p_wqptr == WQPTR_IS_INITING_VALUE); | |
111 | ||
112 | p->p_wqptr = y; | |
113 | ||
114 | if (y != NULL){ | |
115 | wakeup(&p->p_wqptr); | |
116 | } | |
117 | ||
118 | proc_unlock(p); | |
119 | } | |
120 | static boolean_t | |
121 | proc_init_wqptr_or_wait(struct proc *p) { | |
122 | proc_lock(p); | |
123 | ||
124 | if (p->p_wqptr == NULL){ | |
125 | p->p_wqptr = WQPTR_IS_INITING_VALUE; | |
126 | proc_unlock(p); | |
127 | ||
128 | return TRUE; | |
129 | } else if (p->p_wqptr == WQPTR_IS_INITING_VALUE){ | |
130 | assert_wait(&p->p_wqptr, THREAD_UNINT); | |
131 | proc_unlock(p); | |
132 | thread_block(THREAD_CONTINUE_NULL); | |
133 | ||
134 | return FALSE; | |
135 | } else { | |
136 | proc_unlock(p); | |
137 | ||
138 | return FALSE; | |
139 | } | |
140 | } | |
141 | ||
142 | __attribute__((noreturn)) | |
39236c6e A |
143 | static void |
144 | pthread_returning_to_userspace(void) | |
145 | { | |
146 | thread_exception_return(); | |
147 | } | |
148 | ||
149 | static uint32_t | |
150 | get_task_threadmax(void) { | |
151 | return task_threadmax; | |
152 | } | |
153 | ||
154 | static task_t | |
155 | proc_get_task(struct proc *p) { | |
156 | return p->task; | |
157 | } | |
158 | ||
39236c6e A |
159 | static uint64_t |
160 | proc_get_register(struct proc *p) { | |
161 | return (p->p_lflag & P_LREGISTER); | |
162 | } | |
163 | ||
164 | static void | |
165 | proc_set_register(struct proc *p) { | |
166 | proc_setregister(p); | |
167 | } | |
168 | ||
169 | static void* | |
170 | uthread_get_uukwe(struct uthread *t) | |
171 | { | |
172 | return &t->uu_kevent.uu_kwe; | |
173 | } | |
174 | ||
175 | static int | |
176 | uthread_is_cancelled(struct uthread *t) | |
177 | { | |
178 | return (t->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL; | |
179 | } | |
180 | ||
181 | static vm_map_t | |
182 | _current_map(void) | |
183 | { | |
184 | return current_map(); | |
185 | } | |
186 | ||
fe8ab488 A |
187 | static boolean_t |
188 | qos_main_thread_active(void) | |
189 | { | |
190 | return TRUE; | |
191 | } | |
192 | ||
5ba3f43e A |
193 | #if defined(__arm__) |
194 | /* On iOS, the stack placement depends on the address space size */ | |
195 | static uint32_t | |
196 | map_is_1gb(vm_map_t map) | |
197 | { | |
198 | return ((!vm_map_is_64bit(map)) && (get_map_max(map) == ml_get_max_offset(FALSE, MACHINE_MAX_OFFSET_MIN))); | |
199 | } | |
200 | #endif | |
fe8ab488 A |
201 | |
202 | static int proc_usynch_get_requested_thread_qos(struct uthread *uth) | |
203 | { | |
fe8ab488 A |
204 | thread_t thread = uth ? uth->uu_thread : current_thread(); |
205 | int requested_qos; | |
206 | ||
39037602 | 207 | requested_qos = proc_get_thread_policy(thread, TASK_POLICY_ATTRIBUTE, TASK_POLICY_QOS); |
fe8ab488 A |
208 | |
209 | /* | |
39037602 A |
210 | * For the purposes of userspace synchronization, it doesn't make sense to |
211 | * place an override of UNSPECIFIED on another thread, if the current thread | |
212 | * doesn't have any QoS set. In these cases, upgrade to | |
fe8ab488 A |
213 | * THREAD_QOS_USER_INTERACTIVE. |
214 | */ | |
215 | if (requested_qos == THREAD_QOS_UNSPECIFIED) { | |
216 | requested_qos = THREAD_QOS_USER_INTERACTIVE; | |
217 | } | |
218 | ||
219 | return requested_qos; | |
220 | } | |
221 | ||
39037602 A |
222 | static int |
223 | proc_usynch_thread_qos_add_override_for_resource_check_owner(thread_t thread, | |
224 | int override_qos, boolean_t first_override_for_resource, | |
225 | user_addr_t resource, int resource_type, | |
226 | user_addr_t user_lock_addr, mach_port_name_t user_lock_owner) | |
fe8ab488 | 227 | { |
39037602 A |
228 | return proc_thread_qos_add_override_check_owner(thread, override_qos, |
229 | first_override_for_resource, resource, resource_type, | |
230 | user_lock_addr, user_lock_owner); | |
fe8ab488 A |
231 | } |
232 | ||
39037602 A |
233 | static boolean_t |
234 | proc_usynch_thread_qos_add_override_for_resource(task_t task, struct uthread *uth, | |
235 | uint64_t tid, int override_qos, boolean_t first_override_for_resource, | |
236 | user_addr_t resource, int resource_type) | |
fe8ab488 | 237 | { |
fe8ab488 A |
238 | thread_t thread = uth ? uth->uu_thread : THREAD_NULL; |
239 | ||
39037602 A |
240 | return proc_thread_qos_add_override(task, thread, tid, override_qos, |
241 | first_override_for_resource, resource, resource_type); | |
a1c7dba1 A |
242 | } |
243 | ||
39037602 A |
244 | static boolean_t |
245 | proc_usynch_thread_qos_remove_override_for_resource(task_t task, | |
246 | struct uthread *uth, uint64_t tid, user_addr_t resource, int resource_type) | |
a1c7dba1 A |
247 | { |
248 | thread_t thread = uth ? uth->uu_thread : THREAD_NULL; | |
249 | ||
250 | return proc_thread_qos_remove_override(task, thread, tid, resource, resource_type); | |
251 | } | |
252 | ||
39037602 A |
253 | static boolean_t |
254 | proc_usynch_thread_qos_reset_override_for_resource(task_t task, | |
255 | struct uthread *uth, uint64_t tid, user_addr_t resource, int resource_type) | |
a1c7dba1 A |
256 | { |
257 | thread_t thread = uth ? uth->uu_thread : THREAD_NULL; | |
258 | ||
259 | return proc_thread_qos_reset_override(task, thread, tid, resource, resource_type); | |
fe8ab488 | 260 | } |
39236c6e | 261 | |
39037602 A |
262 | static boolean_t |
263 | proc_usynch_thread_qos_squash_override_for_resource(thread_t thread, | |
264 | user_addr_t resource, int resource_type) | |
265 | { | |
266 | return proc_thread_qos_squash_override(thread, resource, resource_type); | |
267 | } | |
268 | ||
39236c6e A |
269 | /* kernel (core) to kext shims */ |
270 | ||
271 | void | |
272 | pthread_init(void) | |
273 | { | |
274 | if (!pthread_functions) { | |
275 | panic("pthread kernel extension not loaded (function table is NULL)."); | |
276 | } | |
277 | pthread_functions->pthread_init(); | |
278 | } | |
279 | ||
39037602 | 280 | int |
39236c6e A |
281 | fill_procworkqueue(proc_t p, struct proc_workqueueinfo * pwqinfo) |
282 | { | |
283 | return pthread_functions->fill_procworkqueue(p, pwqinfo); | |
284 | } | |
285 | ||
39037602 A |
286 | /* |
287 | * Returns true if the workqueue flags are available, and will fill | |
288 | * in exceeded_total and exceeded_constrained. | |
289 | */ | |
290 | boolean_t | |
291 | workqueue_get_pwq_exceeded(void *v, boolean_t *exceeded_total, | |
292 | boolean_t *exceeded_constrained) | |
39236c6e | 293 | { |
39037602 A |
294 | proc_t p = v; |
295 | struct proc_workqueueinfo pwqinfo; | |
296 | int err; | |
297 | ||
298 | assert(p != NULL); | |
299 | assert(exceeded_total != NULL); | |
300 | assert(exceeded_constrained != NULL); | |
301 | ||
302 | err = fill_procworkqueue(p, &pwqinfo); | |
303 | if (err) { | |
304 | return FALSE; | |
305 | } | |
306 | if (!(pwqinfo.pwq_state & WQ_FLAGS_AVAILABLE)) { | |
307 | return FALSE; | |
308 | } | |
309 | ||
310 | *exceeded_total = (pwqinfo.pwq_state & WQ_EXCEEDED_TOTAL_THREAD_LIMIT); | |
311 | *exceeded_constrained = (pwqinfo.pwq_state & WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT); | |
312 | ||
313 | return TRUE; | |
39236c6e A |
314 | } |
315 | ||
39037602 A |
316 | uint32_t |
317 | workqueue_get_pwq_state_kdp(void * v) | |
39236c6e | 318 | { |
39037602 A |
319 | static_assert((WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT << 17) == kTaskWqExceededConstrainedThreadLimit); |
320 | static_assert((WQ_EXCEEDED_TOTAL_THREAD_LIMIT << 17) == kTaskWqExceededTotalThreadLimit); | |
321 | static_assert((WQ_FLAGS_AVAILABLE << 17) == kTaskWqFlagsAvailable); | |
322 | static_assert((WQ_FLAGS_AVAILABLE | WQ_EXCEEDED_TOTAL_THREAD_LIMIT | WQ_EXCEEDED_CONSTRAINED_THREAD_LIMIT) == 0x7); | |
323 | proc_t p = v; | |
324 | if (pthread_functions == NULL || pthread_functions->get_pwq_state_kdp == NULL) | |
325 | return 0; | |
326 | else | |
327 | return pthread_functions->get_pwq_state_kdp(p); | |
39236c6e A |
328 | } |
329 | ||
330 | void | |
331 | workqueue_exit(struct proc *p) | |
332 | { | |
333 | pthread_functions->workqueue_exit(p); | |
334 | } | |
335 | ||
336 | void | |
337 | workqueue_mark_exiting(struct proc *p) | |
338 | { | |
339 | pthread_functions->workqueue_mark_exiting(p); | |
340 | } | |
341 | ||
342 | void | |
343 | workqueue_thread_yielded(void) | |
344 | { | |
345 | pthread_functions->workqueue_thread_yielded(); | |
346 | } | |
347 | ||
348 | sched_call_t | |
349 | workqueue_get_sched_callback(void) | |
350 | { | |
351 | if (pthread_functions->workqueue_get_sched_callback) { | |
352 | return pthread_functions->workqueue_get_sched_callback(); | |
353 | } | |
354 | return NULL; | |
355 | } | |
356 | ||
357 | void | |
358 | pth_proc_hashinit(proc_t p) | |
359 | { | |
360 | pthread_functions->pth_proc_hashinit(p); | |
361 | } | |
362 | ||
363 | void | |
364 | pth_proc_hashdelete(proc_t p) | |
365 | { | |
366 | pthread_functions->pth_proc_hashdelete(p); | |
367 | } | |
368 | ||
369 | /* syscall shims */ | |
370 | int | |
371 | bsdthread_create(struct proc *p, struct bsdthread_create_args *uap, user_addr_t *retval) | |
372 | { | |
373 | return pthread_functions->bsdthread_create(p, uap->func, uap->func_arg, uap->stack, uap->pthread, uap->flags, retval); | |
374 | } | |
375 | ||
376 | int | |
377 | bsdthread_register(struct proc *p, struct bsdthread_register_args *uap, __unused int32_t *retval) | |
378 | { | |
fe8ab488 A |
379 | if (pthread_functions->version >= 1) { |
380 | return pthread_functions->bsdthread_register2(p, uap->threadstart, uap->wqthread, | |
381 | uap->flags, uap->stack_addr_hint, | |
382 | uap->targetconc_ptr, uap->dispatchqueue_offset, | |
383 | uap->tsd_offset, retval); | |
384 | } else { | |
385 | return pthread_functions->bsdthread_register(p, uap->threadstart, uap->wqthread, | |
386 | uap->flags, uap->stack_addr_hint, | |
387 | uap->targetconc_ptr, uap->dispatchqueue_offset, | |
388 | retval); | |
389 | } | |
39236c6e A |
390 | } |
391 | ||
392 | int | |
393 | bsdthread_terminate(struct proc *p, struct bsdthread_terminate_args *uap, int32_t *retval) | |
394 | { | |
395 | return pthread_functions->bsdthread_terminate(p, uap->stackaddr, uap->freesize, uap->port, uap->sem, retval); | |
396 | } | |
397 | ||
fe8ab488 A |
398 | int |
399 | bsdthread_ctl(struct proc *p, struct bsdthread_ctl_args *uap, int *retval) | |
400 | { | |
401 | return pthread_functions->bsdthread_ctl(p, uap->cmd, uap->arg1, uap->arg2, uap->arg3, retval); | |
402 | } | |
403 | ||
404 | ||
39236c6e A |
405 | int |
406 | thread_selfid(struct proc *p, __unused struct thread_selfid_args *uap, uint64_t *retval) | |
407 | { | |
408 | return pthread_functions->thread_selfid(p, retval); | |
409 | } | |
410 | ||
411 | int | |
412 | workq_kernreturn(struct proc *p, struct workq_kernreturn_args *uap, int32_t *retval) | |
413 | { | |
414 | return pthread_functions->workq_kernreturn(p, uap->options, uap->item, uap->affinity, uap->prio, retval); | |
415 | } | |
416 | ||
417 | int | |
418 | workq_open(struct proc *p, __unused struct workq_open_args *uap, int32_t *retval) | |
419 | { | |
420 | return pthread_functions->workq_open(p, retval); | |
421 | } | |
422 | ||
423 | /* pthread synchroniser syscalls */ | |
424 | ||
425 | int | |
426 | psynch_mutexwait(proc_t p, struct psynch_mutexwait_args *uap, uint32_t *retval) | |
427 | { | |
428 | return pthread_functions->psynch_mutexwait(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval); | |
429 | } | |
430 | ||
431 | int | |
432 | psynch_mutexdrop(proc_t p, struct psynch_mutexdrop_args *uap, uint32_t *retval) | |
433 | { | |
434 | return pthread_functions->psynch_mutexdrop(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval); | |
435 | } | |
436 | ||
437 | int | |
438 | psynch_cvbroad(proc_t p, struct psynch_cvbroad_args *uap, uint32_t *retval) | |
439 | { | |
440 | return pthread_functions->psynch_cvbroad(p, uap->cv, uap->cvlsgen, uap->cvudgen, uap->flags, uap->mutex, uap->mugen, uap->tid, retval); | |
441 | } | |
442 | ||
443 | int | |
444 | psynch_cvsignal(proc_t p, struct psynch_cvsignal_args *uap, uint32_t *retval) | |
445 | { | |
446 | return pthread_functions->psynch_cvsignal(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->thread_port, uap->mutex, uap->mugen, uap->tid, uap->flags, retval); | |
447 | } | |
448 | ||
449 | int | |
450 | psynch_cvwait(proc_t p, struct psynch_cvwait_args * uap, uint32_t * retval) | |
451 | { | |
452 | return pthread_functions->psynch_cvwait(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->mutex, uap->mugen, uap->flags, uap->sec, uap->nsec, retval); | |
453 | } | |
454 | ||
455 | int | |
456 | psynch_cvclrprepost(proc_t p, struct psynch_cvclrprepost_args * uap, int *retval) | |
457 | { | |
458 | return pthread_functions->psynch_cvclrprepost(p, uap->cv, uap->cvgen, uap->cvugen, uap->cvsgen, uap->prepocnt, uap->preposeq, uap->flags, retval); | |
459 | } | |
460 | ||
461 | int | |
462 | psynch_rw_longrdlock(proc_t p, struct psynch_rw_longrdlock_args * uap, uint32_t *retval) | |
463 | { | |
464 | return pthread_functions->psynch_rw_longrdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval); | |
465 | } | |
466 | ||
467 | int | |
468 | psynch_rw_rdlock(proc_t p, struct psynch_rw_rdlock_args * uap, uint32_t * retval) | |
469 | { | |
470 | return pthread_functions->psynch_rw_rdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval); | |
471 | } | |
472 | ||
473 | int | |
474 | psynch_rw_unlock(proc_t p, struct psynch_rw_unlock_args *uap, uint32_t *retval) | |
475 | { | |
476 | return pthread_functions->psynch_rw_unlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval); | |
477 | } | |
478 | ||
479 | int | |
480 | psynch_rw_unlock2(__unused proc_t p, __unused struct psynch_rw_unlock2_args *uap, __unused uint32_t *retval) | |
481 | { | |
482 | return ENOTSUP; | |
483 | } | |
484 | ||
485 | int | |
486 | psynch_rw_wrlock(proc_t p, struct psynch_rw_wrlock_args *uap, uint32_t *retval) | |
487 | { | |
488 | return pthread_functions->psynch_rw_wrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval); | |
489 | } | |
490 | ||
491 | int | |
492 | psynch_rw_yieldwrlock(proc_t p, struct psynch_rw_yieldwrlock_args *uap, uint32_t *retval) | |
493 | { | |
494 | return pthread_functions->psynch_rw_yieldwrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval); | |
495 | } | |
496 | ||
497 | int | |
498 | psynch_rw_upgrade(__unused proc_t p, __unused struct psynch_rw_upgrade_args * uap, __unused uint32_t *retval) | |
499 | { | |
500 | return 0; | |
501 | } | |
502 | ||
503 | int | |
504 | psynch_rw_downgrade(__unused proc_t p, __unused struct psynch_rw_downgrade_args * uap, __unused int *retval) | |
505 | { | |
506 | return 0; | |
507 | } | |
508 | ||
39037602 A |
509 | int |
510 | thread_qos_from_pthread_priority(unsigned long priority, unsigned long *flags) | |
511 | { | |
512 | return pthread_functions->thread_qos_from_pthread_priority(priority, flags); | |
513 | } | |
514 | ||
515 | unsigned long | |
516 | pthread_priority_canonicalize(unsigned long priority, boolean_t propagation) | |
517 | { | |
5ba3f43e A |
518 | return pthread_functions->pthread_priority_canonicalize2(priority, propagation); |
519 | } | |
520 | ||
521 | boolean_t | |
522 | workq_thread_has_been_unbound(thread_t th, int qos_class) | |
523 | { | |
524 | if (pthread_functions->workq_thread_has_been_unbound) { | |
525 | return pthread_functions->workq_thread_has_been_unbound(th, qos_class); | |
39037602 | 526 | } else { |
5ba3f43e A |
527 | panic("pthread kext does not support workq_thread_has_been_unbound"); |
528 | return false; | |
39037602 A |
529 | } |
530 | } | |
531 | ||
813fb2f6 A |
532 | void |
533 | kdp_pthread_find_owner(thread_t thread, struct stackshot_thread_waitinfo *waitinfo) | |
534 | { | |
535 | if (pthread_functions->pthread_find_owner) | |
536 | pthread_functions->pthread_find_owner(thread, waitinfo); | |
537 | } | |
538 | ||
539 | void * | |
540 | kdp_pthread_get_thread_kwq(thread_t thread) | |
541 | { | |
542 | if (pthread_functions->pthread_get_thread_kwq) | |
543 | return pthread_functions->pthread_get_thread_kwq(thread); | |
544 | ||
545 | return NULL; | |
546 | } | |
547 | ||
5ba3f43e A |
548 | static void |
549 | thread_will_park_or_terminate(thread_t thread) | |
550 | { | |
551 | if (thread_owned_workloops_count(thread)) { | |
552 | (void)kevent_exit_on_workloop_ownership_leak(thread); | |
553 | } | |
554 | } | |
555 | ||
556 | #if defined(__arm64__) | |
557 | static unsigned __int128 | |
558 | atomic_fetch_add_128_relaxed(_Atomic unsigned __int128 *ptr, unsigned __int128 value) | |
559 | { | |
560 | return atomic_fetch_add_explicit(ptr, value, memory_order_relaxed); | |
561 | } | |
562 | ||
563 | static unsigned __int128 | |
564 | atomic_load_128_relaxed(_Atomic unsigned __int128 *ptr) | |
565 | { | |
566 | return atomic_load_explicit(ptr, memory_order_relaxed); | |
567 | } | |
568 | #endif | |
569 | ||
39236c6e A |
570 | /* |
571 | * The callbacks structure (defined in pthread_shims.h) contains a collection | |
572 | * of kernel functions that were not deemed sensible to expose as a KPI to all | |
573 | * kernel extensions. So the kext is given them in the form of a structure of | |
574 | * function pointers. | |
575 | */ | |
39037602 | 576 | static const struct pthread_callbacks_s pthread_callbacks = { |
39236c6e A |
577 | .version = PTHREAD_SHIMS_VERSION, |
578 | .config_thread_max = CONFIG_THREAD_MAX, | |
579 | .get_task_threadmax = get_task_threadmax, | |
580 | ||
581 | .proc_get_threadstart = proc_get_threadstart, | |
582 | .proc_set_threadstart = proc_set_threadstart, | |
583 | .proc_get_pthsize = proc_get_pthsize, | |
584 | .proc_set_pthsize = proc_set_pthsize, | |
585 | .proc_get_wqthread = proc_get_wqthread, | |
586 | .proc_set_wqthread = proc_set_wqthread, | |
39236c6e A |
587 | .proc_get_dispatchqueue_offset = proc_get_dispatchqueue_offset, |
588 | .proc_set_dispatchqueue_offset = proc_set_dispatchqueue_offset, | |
589 | .proc_get_wqptr = proc_get_wqptr, | |
590 | .proc_set_wqptr = proc_set_wqptr, | |
39037602 | 591 | .proc_get_pthhash = proc_get_pthhash, |
39236c6e A |
592 | .proc_set_pthhash = proc_set_pthhash, |
593 | .proc_get_task = proc_get_task, | |
594 | .proc_lock = proc_lock, | |
39037602 | 595 | .proc_unlock = proc_unlock, |
39236c6e A |
596 | .proc_get_register = proc_get_register, |
597 | .proc_set_register = proc_set_register, | |
598 | ||
599 | /* kernel IPI interfaces */ | |
600 | .ipc_port_copyout_send = ipc_port_copyout_send, | |
601 | .task_get_ipcspace = get_task_ipcspace, | |
602 | .vm_map_page_info = vm_map_page_info, | |
603 | .vm_map_switch = vm_map_switch, | |
604 | .thread_set_wq_state32 = thread_set_wq_state32, | |
5ba3f43e | 605 | #if !defined(__arm__) |
39236c6e | 606 | .thread_set_wq_state64 = thread_set_wq_state64, |
5ba3f43e | 607 | #endif |
39236c6e A |
608 | |
609 | .uthread_get_threadlist = uthread_get_threadlist, | |
610 | .uthread_set_threadlist = uthread_set_threadlist, | |
611 | .uthread_get_sigmask = uthread_get_sigmask, | |
612 | .uthread_set_sigmask = uthread_set_sigmask, | |
613 | .uthread_get_uukwe = uthread_get_uukwe, | |
614 | .uthread_get_returnval = uthread_get_returnval, | |
615 | .uthread_set_returnval = uthread_set_returnval, | |
616 | .uthread_is_cancelled = uthread_is_cancelled, | |
39037602 | 617 | |
39236c6e A |
618 | .thread_exception_return = pthread_returning_to_userspace, |
619 | .thread_bootstrap_return = thread_bootstrap_return, | |
620 | .unix_syscall_return = unix_syscall_return, | |
621 | ||
622 | .absolutetime_to_microtime = absolutetime_to_microtime, | |
623 | ||
39037602 A |
624 | .thread_set_workq_pri = thread_set_workq_pri, |
625 | .thread_set_workq_qos = thread_set_workq_qos, | |
39236c6e A |
626 | |
627 | .get_bsdthread_info = (void*)get_bsdthread_info, | |
628 | .thread_sched_call = thread_sched_call, | |
629 | .thread_static_param = thread_static_param, | |
630 | .thread_create_workq = thread_create_workq, | |
631 | .thread_policy_set_internal = thread_policy_set_internal, | |
fe8ab488 A |
632 | .thread_policy_get = thread_policy_get, |
633 | .thread_set_voucher_name = thread_set_voucher_name, | |
39236c6e A |
634 | |
635 | .thread_affinity_set = thread_affinity_set, | |
636 | ||
637 | .zalloc = zalloc, | |
638 | .zfree = zfree, | |
639 | .zinit = zinit, | |
640 | ||
5ba3f43e A |
641 | .workloop_fulfill_threadreq = workloop_fulfill_threadreq, |
642 | ||
39236c6e A |
643 | .__pthread_testcancel = __pthread_testcancel, |
644 | ||
645 | .mach_port_deallocate = mach_port_deallocate, | |
646 | .semaphore_signal_internal_trap = semaphore_signal_internal_trap, | |
647 | .current_map = _current_map, | |
648 | .thread_create = thread_create, | |
649 | .thread_resume = thread_resume, | |
39037602 | 650 | |
39236c6e A |
651 | .convert_thread_to_port = convert_thread_to_port, |
652 | .ml_get_max_cpus = (void*)ml_get_max_cpus, | |
653 | ||
5ba3f43e A |
654 | #if defined(__arm__) |
655 | .map_is_1gb = map_is_1gb, | |
656 | #endif | |
657 | #if defined(__arm64__) | |
658 | .atomic_fetch_add_128_relaxed = atomic_fetch_add_128_relaxed, | |
659 | .atomic_load_128_relaxed = atomic_load_128_relaxed, | |
660 | #endif | |
39236c6e A |
661 | |
662 | .proc_get_dispatchqueue_serialno_offset = proc_get_dispatchqueue_serialno_offset, | |
663 | .proc_set_dispatchqueue_serialno_offset = proc_set_dispatchqueue_serialno_offset, | |
fe8ab488 A |
664 | |
665 | .proc_get_stack_addr_hint = proc_get_stack_addr_hint, | |
666 | .proc_set_stack_addr_hint = proc_set_stack_addr_hint, | |
667 | .proc_get_pthread_tsd_offset = proc_get_pthread_tsd_offset, | |
668 | .proc_set_pthread_tsd_offset = proc_set_pthread_tsd_offset, | |
5ba3f43e A |
669 | .proc_get_mach_thread_self_tsd_offset = proc_get_mach_thread_self_tsd_offset, |
670 | .proc_set_mach_thread_self_tsd_offset = proc_set_mach_thread_self_tsd_offset, | |
fe8ab488 A |
671 | |
672 | .thread_set_tsd_base = thread_set_tsd_base, | |
673 | ||
674 | .proc_usynch_get_requested_thread_qos = proc_usynch_get_requested_thread_qos, | |
fe8ab488 A |
675 | |
676 | .qos_main_thread_active = qos_main_thread_active, | |
a1c7dba1 | 677 | |
39037602 | 678 | .proc_usynch_thread_qos_add_override_for_resource_check_owner = proc_usynch_thread_qos_add_override_for_resource_check_owner, |
a1c7dba1 A |
679 | .proc_usynch_thread_qos_add_override_for_resource = proc_usynch_thread_qos_add_override_for_resource, |
680 | .proc_usynch_thread_qos_remove_override_for_resource = proc_usynch_thread_qos_remove_override_for_resource, | |
681 | .proc_usynch_thread_qos_reset_override_for_resource = proc_usynch_thread_qos_reset_override_for_resource, | |
39037602 A |
682 | |
683 | .proc_init_wqptr_or_wait = proc_init_wqptr_or_wait, | |
684 | ||
685 | .thread_set_tag = thread_set_tag, | |
686 | .thread_get_tag = thread_get_tag, | |
687 | ||
688 | .proc_usynch_thread_qos_squash_override_for_resource = proc_usynch_thread_qos_squash_override_for_resource, | |
689 | .task_get_default_manager_qos = task_get_default_manager_qos, | |
690 | .thread_create_workq_waiting = thread_create_workq_waiting, | |
5ba3f43e A |
691 | |
692 | .proc_get_return_to_kernel_offset = proc_get_return_to_kernel_offset, | |
693 | .proc_set_return_to_kernel_offset = proc_set_return_to_kernel_offset, | |
694 | .thread_will_park_or_terminate = thread_will_park_or_terminate, | |
695 | ||
696 | .qos_max_parallelism = qos_max_parallelism, | |
697 | ||
698 | .proc_get_user_stack = proc_get_user_stack, | |
699 | .proc_set_user_stack = proc_set_user_stack, | |
39236c6e A |
700 | }; |
701 | ||
702 | pthread_callbacks_t pthread_kern = &pthread_callbacks; | |
703 | pthread_functions_t pthread_functions = NULL; | |
704 | ||
705 | /* | |
706 | * pthread_kext_register is called by pthread.kext upon load, it has to provide | |
707 | * us with a function pointer table of pthread internal calls. In return, this | |
708 | * file provides it with a table of function pointers it needs. | |
709 | */ | |
710 | ||
711 | void | |
712 | pthread_kext_register(pthread_functions_t fns, pthread_callbacks_t *callbacks) | |
713 | { | |
714 | if (pthread_functions != NULL) { | |
715 | panic("Re-initialisation of pthread kext callbacks."); | |
716 | } | |
39037602 | 717 | |
39236c6e A |
718 | if (callbacks != NULL) { |
719 | *callbacks = &pthread_callbacks; | |
720 | } else { | |
721 | panic("pthread_kext_register called without callbacks pointer."); | |
722 | } | |
39037602 | 723 | |
39236c6e A |
724 | if (fns) { |
725 | pthread_functions = fns; | |
726 | } | |
727 | } |