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