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