xref: /xnu-8796.121.2/bsd/pthread/pthread_shims.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2012-2016 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions #define PTHREAD_INTERNAL 1
30*c54f35caSApple OSS Distributions 
31*c54f35caSApple OSS Distributions #include <stdatomic.h>
32*c54f35caSApple OSS Distributions #include <kern/debug.h>
33*c54f35caSApple OSS Distributions #include <kern/mach_param.h>
34*c54f35caSApple OSS Distributions #include <kern/sched_prim.h>
35*c54f35caSApple OSS Distributions #include <kern/task.h>
36*c54f35caSApple OSS Distributions #include <kern/thread.h>
37*c54f35caSApple OSS Distributions #include <kern/affinity.h>
38*c54f35caSApple OSS Distributions #include <kern/zalloc.h>
39*c54f35caSApple OSS Distributions #include <kern/policy_internal.h>
40*c54f35caSApple OSS Distributions #include <kern/sync_sema.h>
41*c54f35caSApple OSS Distributions 
42*c54f35caSApple OSS Distributions #include <machine/machine_routines.h>
43*c54f35caSApple OSS Distributions #include <mach/task.h>
44*c54f35caSApple OSS Distributions #include <mach/thread_act.h>
45*c54f35caSApple OSS Distributions #include <sys/param.h>
46*c54f35caSApple OSS Distributions #include <sys/eventvar.h>
47*c54f35caSApple OSS Distributions #include <sys/pthread_shims.h>
48*c54f35caSApple OSS Distributions #include <pthread/workqueue_internal.h>
49*c54f35caSApple OSS Distributions #include <sys/cdefs.h>
50*c54f35caSApple OSS Distributions #include <sys/proc_info.h>
51*c54f35caSApple OSS Distributions #include <sys/proc_internal.h>
52*c54f35caSApple OSS Distributions #include <sys/sysproto.h>
53*c54f35caSApple OSS Distributions #include <sys/systm.h>
54*c54f35caSApple OSS Distributions #include <sys/ulock.h>
55*c54f35caSApple OSS Distributions #include <vm/vm_map.h>
56*c54f35caSApple OSS Distributions #include <vm/vm_protos.h>
57*c54f35caSApple OSS Distributions #include <kern/kcdata.h>
58*c54f35caSApple OSS Distributions 
59*c54f35caSApple OSS Distributions /* version number of the in-kernel shims given to pthread.kext */
60*c54f35caSApple OSS Distributions #define PTHREAD_SHIMS_VERSION 1
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions #define PTHREAD_CALLBACK_MEMBER kevent_workq_internal
63*c54f35caSApple OSS Distributions 
64*c54f35caSApple OSS Distributions /* compile time asserts to check the length of structures in pthread_shims.h */
65*c54f35caSApple OSS Distributions static_assert((sizeof(struct pthread_functions_s) - offsetof(struct pthread_functions_s, psynch_rw_yieldwrlock) - sizeof(void*)) == (sizeof(void*) * 100));
66*c54f35caSApple OSS Distributions static_assert((sizeof(struct pthread_callbacks_s) - offsetof(struct pthread_callbacks_s, PTHREAD_CALLBACK_MEMBER) - sizeof(void*)) == (sizeof(void*) * 100));
67*c54f35caSApple OSS Distributions 
68*c54f35caSApple OSS Distributions /* old pthread code had definitions for these as they don't exist in headers */
69*c54f35caSApple OSS Distributions extern kern_return_t mach_port_deallocate(ipc_space_t, mach_port_name_t);
70*c54f35caSApple OSS Distributions extern void thread_deallocate_safe(thread_t thread);
71*c54f35caSApple OSS Distributions 
72*c54f35caSApple OSS Distributions #define PTHREAD_STRUCT_ACCESSOR(get, set, rettype, structtype, member) \
73*c54f35caSApple OSS Distributions 	static rettype \
74*c54f35caSApple OSS Distributions 	get(structtype x) { \
75*c54f35caSApple OSS Distributions 	        return (x)->member; \
76*c54f35caSApple OSS Distributions 	} \
77*c54f35caSApple OSS Distributions 	static void \
78*c54f35caSApple OSS Distributions 	set(structtype x, rettype y) { \
79*c54f35caSApple OSS Distributions 	        (x)->member = y; \
80*c54f35caSApple OSS Distributions 	}
81*c54f35caSApple OSS Distributions 
82*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_threadstart, proc_set_threadstart, user_addr_t, struct proc*, p_threadstart);
83*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_pthsize, proc_set_pthsize, int, struct proc*, p_pthsize);
84*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_wqthread, proc_set_wqthread, user_addr_t, struct proc*, p_wqthread);
85*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_stack_addr_hint, proc_set_stack_addr_hint, user_addr_t, struct proc *, p_stack_addr_hint);
86*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_pthread_tsd_offset, proc_set_pthread_tsd_offset, uint32_t, struct proc *, p_pth_tsd_offset);
87*c54f35caSApple OSS Distributions 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);
88*c54f35caSApple OSS Distributions PTHREAD_STRUCT_ACCESSOR(proc_get_pthhash, proc_set_pthhash, void*, struct proc*, p_pthhash);
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions #define WQPTR_IS_INITING_VALUE ((void *)~(uintptr_t)0)
91*c54f35caSApple OSS Distributions 
92*c54f35caSApple OSS Distributions static void
proc_set_dispatchqueue_offset(struct proc * p,uint64_t offset)93*c54f35caSApple OSS Distributions proc_set_dispatchqueue_offset(struct proc *p, uint64_t offset)
94*c54f35caSApple OSS Distributions {
95*c54f35caSApple OSS Distributions 	p->p_dispatchqueue_offset = offset;
96*c54f35caSApple OSS Distributions }
97*c54f35caSApple OSS Distributions 
98*c54f35caSApple OSS Distributions static void
proc_set_workqueue_quantum_offset(struct proc * p,uint64_t offset)99*c54f35caSApple OSS Distributions proc_set_workqueue_quantum_offset(struct proc *p, uint64_t offset)
100*c54f35caSApple OSS Distributions {
101*c54f35caSApple OSS Distributions 	p->p_pthread_wq_quantum_offset = offset;
102*c54f35caSApple OSS Distributions }
103*c54f35caSApple OSS Distributions 
104*c54f35caSApple OSS Distributions static void
proc_set_return_to_kernel_offset(struct proc * p,uint64_t offset)105*c54f35caSApple OSS Distributions proc_set_return_to_kernel_offset(struct proc *p, uint64_t offset)
106*c54f35caSApple OSS Distributions {
107*c54f35caSApple OSS Distributions 	p->p_return_to_kernel_offset = offset;
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions 
110*c54f35caSApple OSS Distributions static user_addr_t
proc_get_user_stack(struct proc * p)111*c54f35caSApple OSS Distributions proc_get_user_stack(struct proc *p)
112*c54f35caSApple OSS Distributions {
113*c54f35caSApple OSS Distributions 	return p->user_stack;
114*c54f35caSApple OSS Distributions }
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions static void
uthread_set_returnval(struct uthread * uth,int retval)117*c54f35caSApple OSS Distributions uthread_set_returnval(struct uthread *uth, int retval)
118*c54f35caSApple OSS Distributions {
119*c54f35caSApple OSS Distributions 	uth->uu_rval[0] = retval;
120*c54f35caSApple OSS Distributions }
121*c54f35caSApple OSS Distributions 
122*c54f35caSApple OSS Distributions __attribute__((noreturn))
123*c54f35caSApple OSS Distributions static void
pthread_returning_to_userspace(void)124*c54f35caSApple OSS Distributions pthread_returning_to_userspace(void)
125*c54f35caSApple OSS Distributions {
126*c54f35caSApple OSS Distributions 	thread_exception_return();
127*c54f35caSApple OSS Distributions }
128*c54f35caSApple OSS Distributions 
129*c54f35caSApple OSS Distributions __attribute__((noreturn))
130*c54f35caSApple OSS Distributions static void
pthread_bootstrap_return(void)131*c54f35caSApple OSS Distributions pthread_bootstrap_return(void)
132*c54f35caSApple OSS Distributions {
133*c54f35caSApple OSS Distributions 	thread_bootstrap_return();
134*c54f35caSApple OSS Distributions }
135*c54f35caSApple OSS Distributions 
136*c54f35caSApple OSS Distributions static uint32_t
get_task_threadmax(void)137*c54f35caSApple OSS Distributions get_task_threadmax(void)
138*c54f35caSApple OSS Distributions {
139*c54f35caSApple OSS Distributions 	return task_threadmax;
140*c54f35caSApple OSS Distributions }
141*c54f35caSApple OSS Distributions 
142*c54f35caSApple OSS Distributions static uint64_t
proc_get_register(struct proc * p)143*c54f35caSApple OSS Distributions proc_get_register(struct proc *p)
144*c54f35caSApple OSS Distributions {
145*c54f35caSApple OSS Distributions 	return p->p_lflag & P_LREGISTER;
146*c54f35caSApple OSS Distributions }
147*c54f35caSApple OSS Distributions 
148*c54f35caSApple OSS Distributions static void
proc_set_register(struct proc * p)149*c54f35caSApple OSS Distributions proc_set_register(struct proc *p)
150*c54f35caSApple OSS Distributions {
151*c54f35caSApple OSS Distributions 	proc_setregister(p);
152*c54f35caSApple OSS Distributions }
153*c54f35caSApple OSS Distributions 
154*c54f35caSApple OSS Distributions static void*
uthread_get_uukwe(struct uthread * t)155*c54f35caSApple OSS Distributions uthread_get_uukwe(struct uthread *t)
156*c54f35caSApple OSS Distributions {
157*c54f35caSApple OSS Distributions 	return &t->uu_save.uus_kwe;
158*c54f35caSApple OSS Distributions }
159*c54f35caSApple OSS Distributions 
160*c54f35caSApple OSS Distributions static int
uthread_is_cancelled(struct uthread * t)161*c54f35caSApple OSS Distributions uthread_is_cancelled(struct uthread *t)
162*c54f35caSApple OSS Distributions {
163*c54f35caSApple OSS Distributions 	return (t->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL;
164*c54f35caSApple OSS Distributions }
165*c54f35caSApple OSS Distributions 
166*c54f35caSApple OSS Distributions static vm_map_t
_current_map(void)167*c54f35caSApple OSS Distributions _current_map(void)
168*c54f35caSApple OSS Distributions {
169*c54f35caSApple OSS Distributions 	return current_map();
170*c54f35caSApple OSS Distributions }
171*c54f35caSApple OSS Distributions 
172*c54f35caSApple OSS Distributions static boolean_t
qos_main_thread_active(void)173*c54f35caSApple OSS Distributions qos_main_thread_active(void)
174*c54f35caSApple OSS Distributions {
175*c54f35caSApple OSS Distributions 	return TRUE;
176*c54f35caSApple OSS Distributions }
177*c54f35caSApple OSS Distributions 
178*c54f35caSApple OSS Distributions static int
proc_usynch_get_requested_thread_qos(struct uthread * uth)179*c54f35caSApple OSS Distributions proc_usynch_get_requested_thread_qos(struct uthread *uth)
180*c54f35caSApple OSS Distributions {
181*c54f35caSApple OSS Distributions 	thread_t thread = uth ? get_machthread(uth) : current_thread();
182*c54f35caSApple OSS Distributions 	int      requested_qos;
183*c54f35caSApple OSS Distributions 
184*c54f35caSApple OSS Distributions 	requested_qos = proc_get_thread_policy(thread, TASK_POLICY_ATTRIBUTE, TASK_POLICY_QOS);
185*c54f35caSApple OSS Distributions 
186*c54f35caSApple OSS Distributions 	/*
187*c54f35caSApple OSS Distributions 	 * For the purposes of userspace synchronization, it doesn't make sense to
188*c54f35caSApple OSS Distributions 	 * place an override of UNSPECIFIED on another thread, if the current thread
189*c54f35caSApple OSS Distributions 	 * doesn't have any QoS set. In these cases, upgrade to
190*c54f35caSApple OSS Distributions 	 * THREAD_QOS_USER_INTERACTIVE.
191*c54f35caSApple OSS Distributions 	 */
192*c54f35caSApple OSS Distributions 	if (requested_qos == THREAD_QOS_UNSPECIFIED) {
193*c54f35caSApple OSS Distributions 		requested_qos = THREAD_QOS_USER_INTERACTIVE;
194*c54f35caSApple OSS Distributions 	}
195*c54f35caSApple OSS Distributions 
196*c54f35caSApple OSS Distributions 	return requested_qos;
197*c54f35caSApple OSS Distributions }
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions static boolean_t
proc_usynch_thread_qos_add_override_for_resource(task_t task,struct uthread * uth,uint64_t tid,int override_qos,boolean_t first_override_for_resource,user_addr_t resource,int resource_type)200*c54f35caSApple OSS Distributions proc_usynch_thread_qos_add_override_for_resource(task_t task, struct uthread *uth,
201*c54f35caSApple OSS Distributions     uint64_t tid, int override_qos, boolean_t first_override_for_resource,
202*c54f35caSApple OSS Distributions     user_addr_t resource, int resource_type)
203*c54f35caSApple OSS Distributions {
204*c54f35caSApple OSS Distributions 	thread_t thread = uth ? get_machthread(uth) : THREAD_NULL;
205*c54f35caSApple OSS Distributions 
206*c54f35caSApple OSS Distributions 	return proc_thread_qos_add_override(task, thread, tid, override_qos,
207*c54f35caSApple OSS Distributions 	           first_override_for_resource, resource, resource_type) == 0;
208*c54f35caSApple OSS Distributions }
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions static boolean_t
proc_usynch_thread_qos_remove_override_for_resource(task_t task,struct uthread * uth,uint64_t tid,user_addr_t resource,int resource_type)211*c54f35caSApple OSS Distributions proc_usynch_thread_qos_remove_override_for_resource(task_t task,
212*c54f35caSApple OSS Distributions     struct uthread *uth, uint64_t tid, user_addr_t resource, int resource_type)
213*c54f35caSApple OSS Distributions {
214*c54f35caSApple OSS Distributions 	thread_t thread = uth ? get_machthread(uth) : THREAD_NULL;
215*c54f35caSApple OSS Distributions 
216*c54f35caSApple OSS Distributions 	return proc_thread_qos_remove_override(task, thread, tid, resource,
217*c54f35caSApple OSS Distributions 	           resource_type) == 0;
218*c54f35caSApple OSS Distributions }
219*c54f35caSApple OSS Distributions 
220*c54f35caSApple OSS Distributions 
221*c54f35caSApple OSS Distributions static wait_result_t
psynch_wait_prepare(uintptr_t kwq,struct turnstile ** tstore,thread_t owner,block_hint_t block_hint,uint64_t deadline)222*c54f35caSApple OSS Distributions psynch_wait_prepare(uintptr_t kwq, struct turnstile **tstore,
223*c54f35caSApple OSS Distributions     thread_t owner, block_hint_t block_hint, uint64_t deadline)
224*c54f35caSApple OSS Distributions {
225*c54f35caSApple OSS Distributions 	struct turnstile *ts;
226*c54f35caSApple OSS Distributions 	wait_result_t wr;
227*c54f35caSApple OSS Distributions 
228*c54f35caSApple OSS Distributions 	if (tstore) {
229*c54f35caSApple OSS Distributions 		ts = turnstile_prepare(kwq, tstore, TURNSTILE_NULL,
230*c54f35caSApple OSS Distributions 		    TURNSTILE_PTHREAD_MUTEX);
231*c54f35caSApple OSS Distributions 
232*c54f35caSApple OSS Distributions 		turnstile_update_inheritor(ts, owner,
233*c54f35caSApple OSS Distributions 		    (TURNSTILE_DELAYED_UPDATE | TURNSTILE_INHERITOR_THREAD));
234*c54f35caSApple OSS Distributions 
235*c54f35caSApple OSS Distributions 		thread_set_pending_block_hint(current_thread(), block_hint);
236*c54f35caSApple OSS Distributions 
237*c54f35caSApple OSS Distributions 		wr = waitq_assert_wait64_leeway(&ts->ts_waitq, (event64_t)kwq,
238*c54f35caSApple OSS Distributions 		    THREAD_ABORTSAFE, TIMEOUT_URGENCY_USER_NORMAL, deadline, 0);
239*c54f35caSApple OSS Distributions 	} else {
240*c54f35caSApple OSS Distributions 		thread_set_pending_block_hint(current_thread(), block_hint);
241*c54f35caSApple OSS Distributions 
242*c54f35caSApple OSS Distributions 		wr = assert_wait_deadline_with_leeway((event_t)kwq, THREAD_ABORTSAFE,
243*c54f35caSApple OSS Distributions 		    TIMEOUT_URGENCY_USER_NORMAL, deadline, 0);
244*c54f35caSApple OSS Distributions 	}
245*c54f35caSApple OSS Distributions 
246*c54f35caSApple OSS Distributions 	return wr;
247*c54f35caSApple OSS Distributions }
248*c54f35caSApple OSS Distributions 
249*c54f35caSApple OSS Distributions static void
psynch_wait_update_complete(struct turnstile * ts)250*c54f35caSApple OSS Distributions psynch_wait_update_complete(struct turnstile *ts)
251*c54f35caSApple OSS Distributions {
252*c54f35caSApple OSS Distributions 	assert(ts);
253*c54f35caSApple OSS Distributions 	turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_NOT_HELD);
254*c54f35caSApple OSS Distributions }
255*c54f35caSApple OSS Distributions 
256*c54f35caSApple OSS Distributions static void
psynch_wait_complete(uintptr_t kwq,struct turnstile ** tstore)257*c54f35caSApple OSS Distributions psynch_wait_complete(uintptr_t kwq, struct turnstile **tstore)
258*c54f35caSApple OSS Distributions {
259*c54f35caSApple OSS Distributions 	assert(tstore);
260*c54f35caSApple OSS Distributions 	turnstile_complete(kwq, tstore, NULL, TURNSTILE_PTHREAD_MUTEX);
261*c54f35caSApple OSS Distributions }
262*c54f35caSApple OSS Distributions 
263*c54f35caSApple OSS Distributions static void
psynch_wait_update_owner(uintptr_t kwq,thread_t owner,struct turnstile ** tstore)264*c54f35caSApple OSS Distributions psynch_wait_update_owner(uintptr_t kwq, thread_t owner,
265*c54f35caSApple OSS Distributions     struct turnstile **tstore)
266*c54f35caSApple OSS Distributions {
267*c54f35caSApple OSS Distributions 	struct turnstile *ts;
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions 	ts = turnstile_prepare(kwq, tstore, TURNSTILE_NULL,
270*c54f35caSApple OSS Distributions 	    TURNSTILE_PTHREAD_MUTEX);
271*c54f35caSApple OSS Distributions 
272*c54f35caSApple OSS Distributions 	turnstile_update_inheritor(ts, owner,
273*c54f35caSApple OSS Distributions 	    (TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD));
274*c54f35caSApple OSS Distributions 	turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
275*c54f35caSApple OSS Distributions 	turnstile_complete(kwq, tstore, NULL, TURNSTILE_PTHREAD_MUTEX);
276*c54f35caSApple OSS Distributions }
277*c54f35caSApple OSS Distributions 
278*c54f35caSApple OSS Distributions static void
psynch_wait_cleanup(void)279*c54f35caSApple OSS Distributions psynch_wait_cleanup(void)
280*c54f35caSApple OSS Distributions {
281*c54f35caSApple OSS Distributions 	turnstile_cleanup();
282*c54f35caSApple OSS Distributions }
283*c54f35caSApple OSS Distributions 
284*c54f35caSApple OSS Distributions static kern_return_t
psynch_wait_wakeup(uintptr_t kwq,struct ksyn_waitq_element * kwe,struct turnstile ** tstore)285*c54f35caSApple OSS Distributions psynch_wait_wakeup(uintptr_t kwq, struct ksyn_waitq_element *kwe,
286*c54f35caSApple OSS Distributions     struct turnstile **tstore)
287*c54f35caSApple OSS Distributions {
288*c54f35caSApple OSS Distributions 	struct thread *th;
289*c54f35caSApple OSS Distributions 	struct turnstile *ts;
290*c54f35caSApple OSS Distributions 	kern_return_t kr;
291*c54f35caSApple OSS Distributions 
292*c54f35caSApple OSS Distributions 	th = get_machthread(__container_of(kwe, struct uthread, uu_save.uus_kwe));
293*c54f35caSApple OSS Distributions 
294*c54f35caSApple OSS Distributions 	if (tstore) {
295*c54f35caSApple OSS Distributions 		ts = turnstile_prepare(kwq, tstore, TURNSTILE_NULL,
296*c54f35caSApple OSS Distributions 		    TURNSTILE_PTHREAD_MUTEX);
297*c54f35caSApple OSS Distributions 		turnstile_update_inheritor(ts, th,
298*c54f35caSApple OSS Distributions 		    (TURNSTILE_IMMEDIATE_UPDATE | TURNSTILE_INHERITOR_THREAD));
299*c54f35caSApple OSS Distributions 
300*c54f35caSApple OSS Distributions 		kr = waitq_wakeup64_thread(&ts->ts_waitq, (event64_t)kwq, th,
301*c54f35caSApple OSS Distributions 		    THREAD_AWAKENED);
302*c54f35caSApple OSS Distributions 
303*c54f35caSApple OSS Distributions 		turnstile_update_inheritor_complete(ts, TURNSTILE_INTERLOCK_HELD);
304*c54f35caSApple OSS Distributions 		turnstile_complete(kwq, tstore, NULL, TURNSTILE_PTHREAD_MUTEX);
305*c54f35caSApple OSS Distributions 	} else {
306*c54f35caSApple OSS Distributions 		kr = thread_wakeup_thread((event_t)kwq, th);
307*c54f35caSApple OSS Distributions 	}
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions 	return kr;
310*c54f35caSApple OSS Distributions }
311*c54f35caSApple OSS Distributions 
312*c54f35caSApple OSS Distributions /* kernel (core) to kext shims */
313*c54f35caSApple OSS Distributions 
314*c54f35caSApple OSS Distributions void
pthread_init(void)315*c54f35caSApple OSS Distributions pthread_init(void)
316*c54f35caSApple OSS Distributions {
317*c54f35caSApple OSS Distributions 	if (!pthread_functions) {
318*c54f35caSApple OSS Distributions 		panic("pthread kernel extension not loaded (function table is NULL).");
319*c54f35caSApple OSS Distributions 	}
320*c54f35caSApple OSS Distributions 	pthread_functions->pthread_init();
321*c54f35caSApple OSS Distributions }
322*c54f35caSApple OSS Distributions 
323*c54f35caSApple OSS Distributions void
pth_proc_hashinit(proc_t p)324*c54f35caSApple OSS Distributions pth_proc_hashinit(proc_t p)
325*c54f35caSApple OSS Distributions {
326*c54f35caSApple OSS Distributions 	pthread_functions->pth_proc_hashinit(p);
327*c54f35caSApple OSS Distributions }
328*c54f35caSApple OSS Distributions 
329*c54f35caSApple OSS Distributions void
pth_proc_hashdelete(proc_t p)330*c54f35caSApple OSS Distributions pth_proc_hashdelete(proc_t p)
331*c54f35caSApple OSS Distributions {
332*c54f35caSApple OSS Distributions 	pthread_functions->pth_proc_hashdelete(p);
333*c54f35caSApple OSS Distributions }
334*c54f35caSApple OSS Distributions 
335*c54f35caSApple OSS Distributions /* syscall shims */
336*c54f35caSApple OSS Distributions int
bsdthread_create(struct proc * p,struct bsdthread_create_args * uap,user_addr_t * retval)337*c54f35caSApple OSS Distributions bsdthread_create(struct proc *p, struct bsdthread_create_args *uap, user_addr_t *retval)
338*c54f35caSApple OSS Distributions {
339*c54f35caSApple OSS Distributions 	return pthread_functions->bsdthread_create(p, uap->func, uap->func_arg, uap->stack, uap->pthread, uap->flags, retval);
340*c54f35caSApple OSS Distributions }
341*c54f35caSApple OSS Distributions 
342*c54f35caSApple OSS Distributions int
bsdthread_register(struct proc * p,struct bsdthread_register_args * uap,__unused int32_t * retval)343*c54f35caSApple OSS Distributions bsdthread_register(struct proc *p, struct bsdthread_register_args *uap, __unused int32_t *retval)
344*c54f35caSApple OSS Distributions {
345*c54f35caSApple OSS Distributions 	kern_return_t kr;
346*c54f35caSApple OSS Distributions 	static_assert(offsetof(struct bsdthread_register_args, threadstart) + sizeof(user_addr_t) ==
347*c54f35caSApple OSS Distributions 	    offsetof(struct bsdthread_register_args, wqthread));
348*c54f35caSApple OSS Distributions 	kr = machine_thread_function_pointers_convert_from_user(current_thread(), &uap->threadstart, 2);
349*c54f35caSApple OSS Distributions 	assert(kr == KERN_SUCCESS);
350*c54f35caSApple OSS Distributions 
351*c54f35caSApple OSS Distributions 	if (pthread_functions->version >= 1) {
352*c54f35caSApple OSS Distributions 		return pthread_functions->bsdthread_register2(p, uap->threadstart,
353*c54f35caSApple OSS Distributions 		           uap->wqthread, uap->flags, uap->stack_addr_hint,
354*c54f35caSApple OSS Distributions 		           uap->targetconc_ptr, uap->dispatchqueue_offset,
355*c54f35caSApple OSS Distributions 		           uap->tsd_offset, retval);
356*c54f35caSApple OSS Distributions 	} else {
357*c54f35caSApple OSS Distributions 		return pthread_functions->bsdthread_register(p, uap->threadstart,
358*c54f35caSApple OSS Distributions 		           uap->wqthread, uap->flags, uap->stack_addr_hint,
359*c54f35caSApple OSS Distributions 		           uap->targetconc_ptr, uap->dispatchqueue_offset,
360*c54f35caSApple OSS Distributions 		           retval);
361*c54f35caSApple OSS Distributions 	}
362*c54f35caSApple OSS Distributions }
363*c54f35caSApple OSS Distributions 
364*c54f35caSApple OSS Distributions int
bsdthread_terminate(struct proc * p,struct bsdthread_terminate_args * uap,int32_t * retval)365*c54f35caSApple OSS Distributions bsdthread_terminate(struct proc *p, struct bsdthread_terminate_args *uap, int32_t *retval)
366*c54f35caSApple OSS Distributions {
367*c54f35caSApple OSS Distributions 	thread_t th = current_thread();
368*c54f35caSApple OSS Distributions 	uthread_t uth = current_uthread();
369*c54f35caSApple OSS Distributions 	struct _bsdthread_terminate *bts = &uth->uu_save.uus_bsdthread_terminate;
370*c54f35caSApple OSS Distributions 	mach_port_name_t sem = (mach_port_name_t)uap->sema_or_ulock;
371*c54f35caSApple OSS Distributions 	mach_port_name_t thp = uap->port;
372*c54f35caSApple OSS Distributions 
373*c54f35caSApple OSS Distributions 	if (thread_get_tag(th) & THREAD_TAG_WORKQUEUE) {
374*c54f35caSApple OSS Distributions 		workq_thread_terminate(p, get_bsdthread_info(th));
375*c54f35caSApple OSS Distributions 	}
376*c54f35caSApple OSS Distributions 
377*c54f35caSApple OSS Distributions 	/*
378*c54f35caSApple OSS Distributions 	 * Gross compatibility hack: ports end in 0x3 and ulocks are aligned.
379*c54f35caSApple OSS Distributions 	 * If the `semaphore` value doesn't look like a port, then it is
380*c54f35caSApple OSS Distributions 	 * a ulock address that will be woken by uthread_joiner_wake()
381*c54f35caSApple OSS Distributions 	 *
382*c54f35caSApple OSS Distributions 	 * We also need to delay destroying the thread port so that
383*c54f35caSApple OSS Distributions 	 * pthread_join()'s ulock_wait() can resolve the thread until
384*c54f35caSApple OSS Distributions 	 * uthread_joiner_wake() has run.
385*c54f35caSApple OSS Distributions 	 */
386*c54f35caSApple OSS Distributions 	if (uap->sema_or_ulock && uap->sema_or_ulock != ipc_entry_name_mask(sem)) {
387*c54f35caSApple OSS Distributions 		thread_set_tag(th, THREAD_TAG_USER_JOIN);
388*c54f35caSApple OSS Distributions 		bts->ulock_addr = uap->sema_or_ulock;
389*c54f35caSApple OSS Distributions 		bts->kport = thp;
390*c54f35caSApple OSS Distributions 
391*c54f35caSApple OSS Distributions 		sem = thp = MACH_PORT_NULL;
392*c54f35caSApple OSS Distributions 	}
393*c54f35caSApple OSS Distributions 
394*c54f35caSApple OSS Distributions 	return pthread_functions->bsdthread_terminate(p, uap->stackaddr, uap->freesize, thp, sem, retval);
395*c54f35caSApple OSS Distributions }
396*c54f35caSApple OSS Distributions 
397*c54f35caSApple OSS Distributions int
thread_selfid(struct proc * p,__unused struct thread_selfid_args * uap,uint64_t * retval)398*c54f35caSApple OSS Distributions thread_selfid(struct proc *p, __unused struct thread_selfid_args *uap, uint64_t *retval)
399*c54f35caSApple OSS Distributions {
400*c54f35caSApple OSS Distributions 	return pthread_functions->thread_selfid(p, retval);
401*c54f35caSApple OSS Distributions }
402*c54f35caSApple OSS Distributions 
403*c54f35caSApple OSS Distributions /* pthread synchroniser syscalls */
404*c54f35caSApple OSS Distributions 
405*c54f35caSApple OSS Distributions int
psynch_mutexwait(proc_t p,struct psynch_mutexwait_args * uap,uint32_t * retval)406*c54f35caSApple OSS Distributions psynch_mutexwait(proc_t p, struct psynch_mutexwait_args *uap, uint32_t *retval)
407*c54f35caSApple OSS Distributions {
408*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_mutexwait(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval);
409*c54f35caSApple OSS Distributions }
410*c54f35caSApple OSS Distributions 
411*c54f35caSApple OSS Distributions int
psynch_mutexdrop(proc_t p,struct psynch_mutexdrop_args * uap,uint32_t * retval)412*c54f35caSApple OSS Distributions psynch_mutexdrop(proc_t p, struct psynch_mutexdrop_args *uap, uint32_t *retval)
413*c54f35caSApple OSS Distributions {
414*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_mutexdrop(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval);
415*c54f35caSApple OSS Distributions }
416*c54f35caSApple OSS Distributions 
417*c54f35caSApple OSS Distributions int
psynch_cvbroad(proc_t p,struct psynch_cvbroad_args * uap,uint32_t * retval)418*c54f35caSApple OSS Distributions psynch_cvbroad(proc_t p, struct psynch_cvbroad_args *uap, uint32_t *retval)
419*c54f35caSApple OSS Distributions {
420*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_cvbroad(p, uap->cv, uap->cvlsgen, uap->cvudgen, uap->flags, uap->mutex, uap->mugen, uap->tid, retval);
421*c54f35caSApple OSS Distributions }
422*c54f35caSApple OSS Distributions 
423*c54f35caSApple OSS Distributions int
psynch_cvsignal(proc_t p,struct psynch_cvsignal_args * uap,uint32_t * retval)424*c54f35caSApple OSS Distributions psynch_cvsignal(proc_t p, struct psynch_cvsignal_args *uap, uint32_t *retval)
425*c54f35caSApple OSS Distributions {
426*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_cvsignal(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->thread_port, uap->mutex, uap->mugen, uap->tid, uap->flags, retval);
427*c54f35caSApple OSS Distributions }
428*c54f35caSApple OSS Distributions 
429*c54f35caSApple OSS Distributions int
psynch_cvwait(proc_t p,struct psynch_cvwait_args * uap,uint32_t * retval)430*c54f35caSApple OSS Distributions psynch_cvwait(proc_t p, struct psynch_cvwait_args * uap, uint32_t * retval)
431*c54f35caSApple OSS Distributions {
432*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_cvwait(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->mutex, uap->mugen, uap->flags, uap->sec, uap->nsec, retval);
433*c54f35caSApple OSS Distributions }
434*c54f35caSApple OSS Distributions 
435*c54f35caSApple OSS Distributions int
psynch_cvclrprepost(proc_t p,struct psynch_cvclrprepost_args * uap,int * retval)436*c54f35caSApple OSS Distributions psynch_cvclrprepost(proc_t p, struct psynch_cvclrprepost_args * uap, int *retval)
437*c54f35caSApple OSS Distributions {
438*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_cvclrprepost(p, uap->cv, uap->cvgen, uap->cvugen, uap->cvsgen, uap->prepocnt, uap->preposeq, uap->flags, retval);
439*c54f35caSApple OSS Distributions }
440*c54f35caSApple OSS Distributions 
441*c54f35caSApple OSS Distributions int
psynch_rw_longrdlock(proc_t p,struct psynch_rw_longrdlock_args * uap,uint32_t * retval)442*c54f35caSApple OSS Distributions psynch_rw_longrdlock(proc_t p, struct psynch_rw_longrdlock_args * uap, uint32_t *retval)
443*c54f35caSApple OSS Distributions {
444*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_rw_longrdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
445*c54f35caSApple OSS Distributions }
446*c54f35caSApple OSS Distributions 
447*c54f35caSApple OSS Distributions int
psynch_rw_rdlock(proc_t p,struct psynch_rw_rdlock_args * uap,uint32_t * retval)448*c54f35caSApple OSS Distributions psynch_rw_rdlock(proc_t p, struct psynch_rw_rdlock_args * uap, uint32_t * retval)
449*c54f35caSApple OSS Distributions {
450*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_rw_rdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
451*c54f35caSApple OSS Distributions }
452*c54f35caSApple OSS Distributions 
453*c54f35caSApple OSS Distributions int
psynch_rw_unlock(proc_t p,struct psynch_rw_unlock_args * uap,uint32_t * retval)454*c54f35caSApple OSS Distributions psynch_rw_unlock(proc_t p, struct psynch_rw_unlock_args *uap, uint32_t *retval)
455*c54f35caSApple OSS Distributions {
456*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_rw_unlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
457*c54f35caSApple OSS Distributions }
458*c54f35caSApple OSS Distributions 
459*c54f35caSApple OSS Distributions int
psynch_rw_unlock2(__unused proc_t p,__unused struct psynch_rw_unlock2_args * uap,__unused uint32_t * retval)460*c54f35caSApple OSS Distributions psynch_rw_unlock2(__unused proc_t p, __unused struct psynch_rw_unlock2_args *uap, __unused uint32_t *retval)
461*c54f35caSApple OSS Distributions {
462*c54f35caSApple OSS Distributions 	return ENOTSUP;
463*c54f35caSApple OSS Distributions }
464*c54f35caSApple OSS Distributions 
465*c54f35caSApple OSS Distributions int
psynch_rw_wrlock(proc_t p,struct psynch_rw_wrlock_args * uap,uint32_t * retval)466*c54f35caSApple OSS Distributions psynch_rw_wrlock(proc_t p, struct psynch_rw_wrlock_args *uap, uint32_t *retval)
467*c54f35caSApple OSS Distributions {
468*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_rw_wrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
469*c54f35caSApple OSS Distributions }
470*c54f35caSApple OSS Distributions 
471*c54f35caSApple OSS Distributions int
psynch_rw_yieldwrlock(proc_t p,struct psynch_rw_yieldwrlock_args * uap,uint32_t * retval)472*c54f35caSApple OSS Distributions psynch_rw_yieldwrlock(proc_t p, struct psynch_rw_yieldwrlock_args *uap, uint32_t *retval)
473*c54f35caSApple OSS Distributions {
474*c54f35caSApple OSS Distributions 	return pthread_functions->psynch_rw_yieldwrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
475*c54f35caSApple OSS Distributions }
476*c54f35caSApple OSS Distributions 
477*c54f35caSApple OSS Distributions int
psynch_rw_upgrade(__unused proc_t p,__unused struct psynch_rw_upgrade_args * uap,__unused uint32_t * retval)478*c54f35caSApple OSS Distributions psynch_rw_upgrade(__unused proc_t p, __unused struct psynch_rw_upgrade_args * uap, __unused uint32_t *retval)
479*c54f35caSApple OSS Distributions {
480*c54f35caSApple OSS Distributions 	return 0;
481*c54f35caSApple OSS Distributions }
482*c54f35caSApple OSS Distributions 
483*c54f35caSApple OSS Distributions int
psynch_rw_downgrade(__unused proc_t p,__unused struct psynch_rw_downgrade_args * uap,__unused int * retval)484*c54f35caSApple OSS Distributions psynch_rw_downgrade(__unused proc_t p, __unused struct psynch_rw_downgrade_args * uap, __unused int *retval)
485*c54f35caSApple OSS Distributions {
486*c54f35caSApple OSS Distributions 	return 0;
487*c54f35caSApple OSS Distributions }
488*c54f35caSApple OSS Distributions 
489*c54f35caSApple OSS Distributions void
kdp_pthread_find_owner(thread_t thread,struct stackshot_thread_waitinfo * waitinfo)490*c54f35caSApple OSS Distributions kdp_pthread_find_owner(thread_t thread, struct stackshot_thread_waitinfo *waitinfo)
491*c54f35caSApple OSS Distributions {
492*c54f35caSApple OSS Distributions 	if (pthread_functions->pthread_find_owner) {
493*c54f35caSApple OSS Distributions 		pthread_functions->pthread_find_owner(thread, waitinfo);
494*c54f35caSApple OSS Distributions 	}
495*c54f35caSApple OSS Distributions }
496*c54f35caSApple OSS Distributions 
497*c54f35caSApple OSS Distributions void *
kdp_pthread_get_thread_kwq(thread_t thread)498*c54f35caSApple OSS Distributions kdp_pthread_get_thread_kwq(thread_t thread)
499*c54f35caSApple OSS Distributions {
500*c54f35caSApple OSS Distributions 	if (pthread_functions->pthread_get_thread_kwq) {
501*c54f35caSApple OSS Distributions 		return pthread_functions->pthread_get_thread_kwq(thread);
502*c54f35caSApple OSS Distributions 	}
503*c54f35caSApple OSS Distributions 
504*c54f35caSApple OSS Distributions 	return NULL;
505*c54f35caSApple OSS Distributions }
506*c54f35caSApple OSS Distributions 
507*c54f35caSApple OSS Distributions void
thread_will_park_or_terminate(__unused thread_t thread)508*c54f35caSApple OSS Distributions thread_will_park_or_terminate(__unused thread_t thread)
509*c54f35caSApple OSS Distributions {
510*c54f35caSApple OSS Distributions }
511*c54f35caSApple OSS Distributions 
512*c54f35caSApple OSS Distributions static bool
old_proc_get_pthread_jit_allowlist(struct proc * t)513*c54f35caSApple OSS Distributions old_proc_get_pthread_jit_allowlist(struct proc *t)
514*c54f35caSApple OSS Distributions {
515*c54f35caSApple OSS Distributions 	bool unused_late = false;
516*c54f35caSApple OSS Distributions 	return proc_get_pthread_jit_allowlist(t, &unused_late);
517*c54f35caSApple OSS Distributions }
518*c54f35caSApple OSS Distributions 
519*c54f35caSApple OSS Distributions /*
520*c54f35caSApple OSS Distributions  * The callbacks structure (defined in pthread_shims.h) contains a collection
521*c54f35caSApple OSS Distributions  * of kernel functions that were not deemed sensible to expose as a KPI to all
522*c54f35caSApple OSS Distributions  * kernel extensions. So the kext is given them in the form of a structure of
523*c54f35caSApple OSS Distributions  * function pointers.
524*c54f35caSApple OSS Distributions  */
525*c54f35caSApple OSS Distributions static const struct pthread_callbacks_s pthread_callbacks = {
526*c54f35caSApple OSS Distributions 	.version = PTHREAD_SHIMS_VERSION,
527*c54f35caSApple OSS Distributions 	.config_thread_max = CONFIG_THREAD_MAX,
528*c54f35caSApple OSS Distributions 	.get_task_threadmax = get_task_threadmax,
529*c54f35caSApple OSS Distributions 
530*c54f35caSApple OSS Distributions 	.proc_get_threadstart = proc_get_threadstart,
531*c54f35caSApple OSS Distributions 	.proc_set_threadstart = proc_set_threadstart,
532*c54f35caSApple OSS Distributions 	.proc_get_pthsize = proc_get_pthsize,
533*c54f35caSApple OSS Distributions 	.proc_set_pthsize = proc_set_pthsize,
534*c54f35caSApple OSS Distributions 	.proc_get_wqthread = proc_get_wqthread,
535*c54f35caSApple OSS Distributions 	.proc_set_wqthread = proc_set_wqthread,
536*c54f35caSApple OSS Distributions 	.proc_set_dispatchqueue_offset = proc_set_dispatchqueue_offset,
537*c54f35caSApple OSS Distributions 	.proc_set_workqueue_quantum_offset = proc_set_workqueue_quantum_offset,
538*c54f35caSApple OSS Distributions 	.proc_get_pthhash = proc_get_pthhash,
539*c54f35caSApple OSS Distributions 	.proc_set_pthhash = proc_set_pthhash,
540*c54f35caSApple OSS Distributions 	.proc_get_register = proc_get_register,
541*c54f35caSApple OSS Distributions 	.proc_set_register = proc_set_register,
542*c54f35caSApple OSS Distributions 	.proc_get_pthread_jit_allowlist = old_proc_get_pthread_jit_allowlist,
543*c54f35caSApple OSS Distributions 	.proc_get_pthread_jit_allowlist2 = proc_get_pthread_jit_allowlist,
544*c54f35caSApple OSS Distributions 
545*c54f35caSApple OSS Distributions 	/* kernel IPI interfaces */
546*c54f35caSApple OSS Distributions 	.task_get_ipcspace = get_task_ipcspace,
547*c54f35caSApple OSS Distributions 	.vm_map_page_info = vm_map_page_info,
548*c54f35caSApple OSS Distributions 	.ipc_port_copyout_send_pinned = ipc_port_copyout_send_pinned,
549*c54f35caSApple OSS Distributions 	.thread_set_wq_state32 = thread_set_wq_state32,
550*c54f35caSApple OSS Distributions 	.thread_set_wq_state64 = thread_set_wq_state64,
551*c54f35caSApple OSS Distributions 
552*c54f35caSApple OSS Distributions 	.uthread_get_uukwe = uthread_get_uukwe,
553*c54f35caSApple OSS Distributions 	.uthread_set_returnval = uthread_set_returnval,
554*c54f35caSApple OSS Distributions 	.uthread_is_cancelled = uthread_is_cancelled,
555*c54f35caSApple OSS Distributions 
556*c54f35caSApple OSS Distributions 	.thread_exception_return = pthread_returning_to_userspace,
557*c54f35caSApple OSS Distributions 	.thread_bootstrap_return = pthread_bootstrap_return,
558*c54f35caSApple OSS Distributions 	.unix_syscall_return = unix_syscall_return,
559*c54f35caSApple OSS Distributions 
560*c54f35caSApple OSS Distributions 	.get_bsdthread_info = get_bsdthread_info,
561*c54f35caSApple OSS Distributions 	.thread_policy_set_internal = thread_policy_set_internal,
562*c54f35caSApple OSS Distributions 	.thread_policy_get = thread_policy_get,
563*c54f35caSApple OSS Distributions 
564*c54f35caSApple OSS Distributions 	.__pthread_testcancel = __pthread_testcancel,
565*c54f35caSApple OSS Distributions 
566*c54f35caSApple OSS Distributions 	.mach_port_deallocate = mach_port_deallocate,
567*c54f35caSApple OSS Distributions 	.semaphore_signal_internal_trap = semaphore_signal_internal_trap,
568*c54f35caSApple OSS Distributions 	.current_map = _current_map,
569*c54f35caSApple OSS Distributions 
570*c54f35caSApple OSS Distributions 	.thread_create_immovable = thread_create_immovable,
571*c54f35caSApple OSS Distributions 	.thread_terminate_pinned = thread_terminate_pinned,
572*c54f35caSApple OSS Distributions 	.thread_resume = thread_resume,
573*c54f35caSApple OSS Distributions 
574*c54f35caSApple OSS Distributions 	.kevent_workq_internal = kevent_workq_internal,
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions 	.convert_thread_to_port_pinned = convert_thread_to_port_pinned,
577*c54f35caSApple OSS Distributions 
578*c54f35caSApple OSS Distributions 	.proc_get_stack_addr_hint = proc_get_stack_addr_hint,
579*c54f35caSApple OSS Distributions 	.proc_set_stack_addr_hint = proc_set_stack_addr_hint,
580*c54f35caSApple OSS Distributions 	.proc_get_pthread_tsd_offset = proc_get_pthread_tsd_offset,
581*c54f35caSApple OSS Distributions 	.proc_set_pthread_tsd_offset = proc_set_pthread_tsd_offset,
582*c54f35caSApple OSS Distributions 	.proc_get_mach_thread_self_tsd_offset = proc_get_mach_thread_self_tsd_offset,
583*c54f35caSApple OSS Distributions 	.proc_set_mach_thread_self_tsd_offset = proc_set_mach_thread_self_tsd_offset,
584*c54f35caSApple OSS Distributions 
585*c54f35caSApple OSS Distributions 	.thread_set_tsd_base = thread_set_tsd_base,
586*c54f35caSApple OSS Distributions 
587*c54f35caSApple OSS Distributions 	.proc_usynch_get_requested_thread_qos = proc_usynch_get_requested_thread_qos,
588*c54f35caSApple OSS Distributions 
589*c54f35caSApple OSS Distributions 	.qos_main_thread_active = qos_main_thread_active,
590*c54f35caSApple OSS Distributions 	.thread_set_voucher_name = thread_set_voucher_name,
591*c54f35caSApple OSS Distributions 
592*c54f35caSApple OSS Distributions 	.proc_usynch_thread_qos_add_override_for_resource = proc_usynch_thread_qos_add_override_for_resource,
593*c54f35caSApple OSS Distributions 	.proc_usynch_thread_qos_remove_override_for_resource = proc_usynch_thread_qos_remove_override_for_resource,
594*c54f35caSApple OSS Distributions 
595*c54f35caSApple OSS Distributions 	.thread_set_tag = thread_set_tag,
596*c54f35caSApple OSS Distributions 	.thread_get_tag = thread_get_tag,
597*c54f35caSApple OSS Distributions 
598*c54f35caSApple OSS Distributions 	.proc_set_return_to_kernel_offset = proc_set_return_to_kernel_offset,
599*c54f35caSApple OSS Distributions 	.thread_will_park_or_terminate = thread_will_park_or_terminate,
600*c54f35caSApple OSS Distributions 
601*c54f35caSApple OSS Distributions 	.proc_get_user_stack = proc_get_user_stack,
602*c54f35caSApple OSS Distributions 	.task_findtid = task_findtid,
603*c54f35caSApple OSS Distributions 	.thread_deallocate_safe = thread_deallocate_safe,
604*c54f35caSApple OSS Distributions 
605*c54f35caSApple OSS Distributions 	.psynch_wait_prepare = psynch_wait_prepare,
606*c54f35caSApple OSS Distributions 	.psynch_wait_update_complete = psynch_wait_update_complete,
607*c54f35caSApple OSS Distributions 	.psynch_wait_complete = psynch_wait_complete,
608*c54f35caSApple OSS Distributions 	.psynch_wait_cleanup = psynch_wait_cleanup,
609*c54f35caSApple OSS Distributions 	.psynch_wait_wakeup = psynch_wait_wakeup,
610*c54f35caSApple OSS Distributions 	.psynch_wait_update_owner = psynch_wait_update_owner,
611*c54f35caSApple OSS Distributions };
612*c54f35caSApple OSS Distributions 
613*c54f35caSApple OSS Distributions pthread_callbacks_t pthread_kern = &pthread_callbacks;
614*c54f35caSApple OSS Distributions pthread_functions_t pthread_functions = NULL;
615*c54f35caSApple OSS Distributions 
616*c54f35caSApple OSS Distributions /*
617*c54f35caSApple OSS Distributions  * pthread_kext_register is called by pthread.kext upon load, it has to provide
618*c54f35caSApple OSS Distributions  * us with a function pointer table of pthread internal calls. In return, this
619*c54f35caSApple OSS Distributions  * file provides it with a table of function pointers it needs.
620*c54f35caSApple OSS Distributions  */
621*c54f35caSApple OSS Distributions 
622*c54f35caSApple OSS Distributions void
pthread_kext_register(pthread_functions_t fns,pthread_callbacks_t * callbacks)623*c54f35caSApple OSS Distributions pthread_kext_register(pthread_functions_t fns, pthread_callbacks_t *callbacks)
624*c54f35caSApple OSS Distributions {
625*c54f35caSApple OSS Distributions 	if (pthread_functions != NULL) {
626*c54f35caSApple OSS Distributions 		panic("Re-initialisation of pthread kext callbacks.");
627*c54f35caSApple OSS Distributions 	}
628*c54f35caSApple OSS Distributions 
629*c54f35caSApple OSS Distributions 	if (callbacks != NULL) {
630*c54f35caSApple OSS Distributions 		*callbacks = &pthread_callbacks;
631*c54f35caSApple OSS Distributions 	} else {
632*c54f35caSApple OSS Distributions 		panic("pthread_kext_register called without callbacks pointer.");
633*c54f35caSApple OSS Distributions 	}
634*c54f35caSApple OSS Distributions 
635*c54f35caSApple OSS Distributions 	if (fns) {
636*c54f35caSApple OSS Distributions 		pthread_functions = fns;
637*c54f35caSApple OSS Distributions 	}
638*c54f35caSApple OSS Distributions }
639