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