2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #define PTHREAD_INTERNAL 1
31 #include <kern/debug.h>
32 #include <kern/mach_param.h>
33 #include <kern/sched_prim.h>
34 #include <kern/task.h>
35 #include <kern/thread.h>
36 #include <kern/affinity.h>
37 #include <kern/zalloc.h>
38 #include <machine/machine_routines.h>
39 #include <mach/task.h>
40 #include <mach/thread_act.h>
41 #include <sys/param.h>
42 #include <sys/pthread_shims.h>
43 #include <sys/proc_internal.h>
44 #include <sys/sysproto.h>
45 #include <sys/systm.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_protos.h>
49 /* version number of the in-kernel shims given to pthread.kext */
50 #define PTHREAD_SHIMS_VERSION 1
52 /* old pthread code had definitions for these as they don't exist in headers */
53 extern kern_return_t
mach_port_deallocate(ipc_space_t
, mach_port_name_t
);
54 extern kern_return_t
semaphore_signal_internal_trap(mach_port_name_t
);
56 #define PTHREAD_STRUCT_ACCESSOR(get, set, rettype, structtype, member) \
62 set(structtype x, rettype y) { \
66 PTHREAD_STRUCT_ACCESSOR(proc_get_threadstart
, proc_set_threadstart
, user_addr_t
, struct proc
*, p_threadstart
);
67 PTHREAD_STRUCT_ACCESSOR(proc_get_pthsize
, proc_set_pthsize
, int, struct proc
*, p_pthsize
);
68 PTHREAD_STRUCT_ACCESSOR(proc_get_wqthread
, proc_set_wqthread
, user_addr_t
, struct proc
*, p_wqthread
);
69 PTHREAD_STRUCT_ACCESSOR(proc_get_targconc
, proc_set_targconc
, user_addr_t
, struct proc
*, p_targconc
);
70 PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_offset
, proc_set_dispatchqueue_offset
, uint64_t, struct proc
*, p_dispatchqueue_offset
);
71 PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_serialno_offset
, proc_set_dispatchqueue_serialno_offset
, uint64_t, struct proc
*, p_dispatchqueue_serialno_offset
);
72 PTHREAD_STRUCT_ACCESSOR(proc_get_wqptr
, proc_set_wqptr
, void*, struct proc
*, p_wqptr
);
73 PTHREAD_STRUCT_ACCESSOR(proc_get_wqsize
, proc_set_wqsize
, int, struct proc
*, p_wqsize
);
74 PTHREAD_STRUCT_ACCESSOR(proc_get_pthhash
, proc_set_pthhash
, void*, struct proc
*, p_pthhash
);
76 PTHREAD_STRUCT_ACCESSOR(uthread_get_threadlist
, uthread_set_threadlist
, void*, struct uthread
*, uu_threadlist
);
77 PTHREAD_STRUCT_ACCESSOR(uthread_get_sigmask
, uthread_set_sigmask
, sigset_t
, struct uthread
*, uu_sigmask
);
78 PTHREAD_STRUCT_ACCESSOR(uthread_get_returnval
, uthread_set_returnval
, int, struct uthread
*, uu_rval
[0]);
81 pthread_returning_to_userspace(void)
83 thread_exception_return();
87 get_task_threadmax(void) {
88 return task_threadmax
;
92 proc_get_task(struct proc
*p
) {
97 proc_get_wqlockptr(struct proc
*p
) {
98 return &(p
->p_wqlock
);
102 proc_get_wqinitingptr(struct proc
*p
) {
103 return &(p
->p_wqiniting
);
107 proc_get_register(struct proc
*p
) {
108 return (p
->p_lflag
& P_LREGISTER
);
112 proc_set_register(struct proc
*p
) {
117 uthread_get_uukwe(struct uthread
*t
)
119 return &t
->uu_kevent
.uu_kwe
;
123 uthread_is_cancelled(struct uthread
*t
)
125 return (t
->uu_flag
& (UT_CANCELDISABLE
| UT_CANCEL
| UT_CANCELED
)) == UT_CANCEL
;
131 return current_map();
135 /* kernel (core) to kext shims */
140 if (!pthread_functions
) {
141 panic("pthread kernel extension not loaded (function table is NULL).");
143 pthread_functions
->pthread_init();
147 fill_procworkqueue(proc_t p
, struct proc_workqueueinfo
* pwqinfo
)
149 return pthread_functions
->fill_procworkqueue(p
, pwqinfo
);
153 workqueue_init_lock(proc_t p
)
155 pthread_functions
->workqueue_init_lock(p
);
159 workqueue_destroy_lock(proc_t p
)
161 pthread_functions
->workqueue_destroy_lock(p
);
165 workqueue_exit(struct proc
*p
)
167 pthread_functions
->workqueue_exit(p
);
171 workqueue_mark_exiting(struct proc
*p
)
173 pthread_functions
->workqueue_mark_exiting(p
);
177 workqueue_thread_yielded(void)
179 pthread_functions
->workqueue_thread_yielded();
183 workqueue_get_sched_callback(void)
185 if (pthread_functions
->workqueue_get_sched_callback
) {
186 return pthread_functions
->workqueue_get_sched_callback();
192 pth_proc_hashinit(proc_t p
)
194 pthread_functions
->pth_proc_hashinit(p
);
198 pth_proc_hashdelete(proc_t p
)
200 pthread_functions
->pth_proc_hashdelete(p
);
205 bsdthread_create(struct proc
*p
, struct bsdthread_create_args
*uap
, user_addr_t
*retval
)
207 return pthread_functions
->bsdthread_create(p
, uap
->func
, uap
->func_arg
, uap
->stack
, uap
->pthread
, uap
->flags
, retval
);
211 bsdthread_register(struct proc
*p
, struct bsdthread_register_args
*uap
, __unused
int32_t *retval
)
213 return pthread_functions
->bsdthread_register(p
, uap
->threadstart
, uap
->wqthread
, uap
->pthsize
, uap
->dummy_value
,
214 uap
->targetconc_ptr
, uap
->dispatchqueue_offset
, retval
);
218 bsdthread_terminate(struct proc
*p
, struct bsdthread_terminate_args
*uap
, int32_t *retval
)
220 return pthread_functions
->bsdthread_terminate(p
, uap
->stackaddr
, uap
->freesize
, uap
->port
, uap
->sem
, retval
);
224 thread_selfid(struct proc
*p
, __unused
struct thread_selfid_args
*uap
, uint64_t *retval
)
226 return pthread_functions
->thread_selfid(p
, retval
);
230 workq_kernreturn(struct proc
*p
, struct workq_kernreturn_args
*uap
, int32_t *retval
)
232 return pthread_functions
->workq_kernreturn(p
, uap
->options
, uap
->item
, uap
->affinity
, uap
->prio
, retval
);
236 workq_open(struct proc
*p
, __unused
struct workq_open_args
*uap
, int32_t *retval
)
238 return pthread_functions
->workq_open(p
, retval
);
241 /* pthread synchroniser syscalls */
244 psynch_mutexwait(proc_t p
, struct psynch_mutexwait_args
*uap
, uint32_t *retval
)
246 return pthread_functions
->psynch_mutexwait(p
, uap
->mutex
, uap
->mgen
, uap
->ugen
, uap
->tid
, uap
->flags
, retval
);
250 psynch_mutexdrop(proc_t p
, struct psynch_mutexdrop_args
*uap
, uint32_t *retval
)
252 return pthread_functions
->psynch_mutexdrop(p
, uap
->mutex
, uap
->mgen
, uap
->ugen
, uap
->tid
, uap
->flags
, retval
);
256 psynch_cvbroad(proc_t p
, struct psynch_cvbroad_args
*uap
, uint32_t *retval
)
258 return pthread_functions
->psynch_cvbroad(p
, uap
->cv
, uap
->cvlsgen
, uap
->cvudgen
, uap
->flags
, uap
->mutex
, uap
->mugen
, uap
->tid
, retval
);
262 psynch_cvsignal(proc_t p
, struct psynch_cvsignal_args
*uap
, uint32_t *retval
)
264 return pthread_functions
->psynch_cvsignal(p
, uap
->cv
, uap
->cvlsgen
, uap
->cvugen
, uap
->thread_port
, uap
->mutex
, uap
->mugen
, uap
->tid
, uap
->flags
, retval
);
268 psynch_cvwait(proc_t p
, struct psynch_cvwait_args
* uap
, uint32_t * retval
)
270 return pthread_functions
->psynch_cvwait(p
, uap
->cv
, uap
->cvlsgen
, uap
->cvugen
, uap
->mutex
, uap
->mugen
, uap
->flags
, uap
->sec
, uap
->nsec
, retval
);
274 psynch_cvclrprepost(proc_t p
, struct psynch_cvclrprepost_args
* uap
, int *retval
)
276 return pthread_functions
->psynch_cvclrprepost(p
, uap
->cv
, uap
->cvgen
, uap
->cvugen
, uap
->cvsgen
, uap
->prepocnt
, uap
->preposeq
, uap
->flags
, retval
);
280 psynch_rw_longrdlock(proc_t p
, struct psynch_rw_longrdlock_args
* uap
, uint32_t *retval
)
282 return pthread_functions
->psynch_rw_longrdlock(p
, uap
->rwlock
, uap
->lgenval
, uap
->ugenval
, uap
->rw_wc
, uap
->flags
, retval
);
286 psynch_rw_rdlock(proc_t p
, struct psynch_rw_rdlock_args
* uap
, uint32_t * retval
)
288 return pthread_functions
->psynch_rw_rdlock(p
, uap
->rwlock
, uap
->lgenval
, uap
->ugenval
, uap
->rw_wc
, uap
->flags
, retval
);
292 psynch_rw_unlock(proc_t p
, struct psynch_rw_unlock_args
*uap
, uint32_t *retval
)
294 return pthread_functions
->psynch_rw_unlock(p
, uap
->rwlock
, uap
->lgenval
, uap
->ugenval
, uap
->rw_wc
, uap
->flags
, retval
);
298 psynch_rw_unlock2(__unused proc_t p
, __unused
struct psynch_rw_unlock2_args
*uap
, __unused
uint32_t *retval
)
304 psynch_rw_wrlock(proc_t p
, struct psynch_rw_wrlock_args
*uap
, uint32_t *retval
)
306 return pthread_functions
->psynch_rw_wrlock(p
, uap
->rwlock
, uap
->lgenval
, uap
->ugenval
, uap
->rw_wc
, uap
->flags
, retval
);
310 psynch_rw_yieldwrlock(proc_t p
, struct psynch_rw_yieldwrlock_args
*uap
, uint32_t *retval
)
312 return pthread_functions
->psynch_rw_yieldwrlock(p
, uap
->rwlock
, uap
->lgenval
, uap
->ugenval
, uap
->rw_wc
, uap
->flags
, retval
);
316 psynch_rw_upgrade(__unused proc_t p
, __unused
struct psynch_rw_upgrade_args
* uap
, __unused
uint32_t *retval
)
322 psynch_rw_downgrade(__unused proc_t p
, __unused
struct psynch_rw_downgrade_args
* uap
, __unused
int *retval
)
327 /* unimplemented guard */
330 // unhooked_panic(void)
332 // panic("pthread system call not hooked up");
336 * The callbacks structure (defined in pthread_shims.h) contains a collection
337 * of kernel functions that were not deemed sensible to expose as a KPI to all
338 * kernel extensions. So the kext is given them in the form of a structure of
341 static struct pthread_callbacks_s pthread_callbacks
= {
342 .version
= PTHREAD_SHIMS_VERSION
,
343 .config_thread_max
= CONFIG_THREAD_MAX
,
344 .get_task_threadmax
= get_task_threadmax
,
346 .proc_get_threadstart
= proc_get_threadstart
,
347 .proc_set_threadstart
= proc_set_threadstart
,
348 .proc_get_pthsize
= proc_get_pthsize
,
349 .proc_set_pthsize
= proc_set_pthsize
,
350 .proc_get_wqthread
= proc_get_wqthread
,
351 .proc_set_wqthread
= proc_set_wqthread
,
352 .proc_get_targconc
= proc_get_targconc
,
353 .proc_set_targconc
= proc_set_targconc
,
354 .proc_get_dispatchqueue_offset
= proc_get_dispatchqueue_offset
,
355 .proc_set_dispatchqueue_offset
= proc_set_dispatchqueue_offset
,
356 .proc_get_wqptr
= proc_get_wqptr
,
357 .proc_set_wqptr
= proc_set_wqptr
,
358 .proc_get_wqsize
= proc_get_wqsize
,
359 .proc_set_wqsize
= proc_set_wqsize
,
360 .proc_get_wqlockptr
= proc_get_wqlockptr
,
361 .proc_get_wqinitingptr
= proc_get_wqinitingptr
,
362 .proc_get_pthhash
= proc_get_pthhash
,
363 .proc_set_pthhash
= proc_set_pthhash
,
364 .proc_get_task
= proc_get_task
,
365 .proc_lock
= proc_lock
,
366 .proc_unlock
= proc_unlock
,
367 .proc_get_register
= proc_get_register
,
368 .proc_set_register
= proc_set_register
,
370 /* kernel IPI interfaces */
371 .ipc_port_copyout_send
= ipc_port_copyout_send
,
372 .task_get_ipcspace
= get_task_ipcspace
,
373 .vm_map_page_info
= vm_map_page_info
,
374 .vm_map_switch
= vm_map_switch
,
375 .thread_set_wq_state32
= thread_set_wq_state32
,
376 .thread_set_wq_state64
= thread_set_wq_state64
,
378 .uthread_get_threadlist
= uthread_get_threadlist
,
379 .uthread_set_threadlist
= uthread_set_threadlist
,
380 .uthread_get_sigmask
= uthread_get_sigmask
,
381 .uthread_set_sigmask
= uthread_set_sigmask
,
382 .uthread_get_uukwe
= uthread_get_uukwe
,
383 .uthread_get_returnval
= uthread_get_returnval
,
384 .uthread_set_returnval
= uthread_set_returnval
,
385 .uthread_is_cancelled
= uthread_is_cancelled
,
387 .thread_exception_return
= pthread_returning_to_userspace
,
388 .thread_bootstrap_return
= thread_bootstrap_return
,
389 .unix_syscall_return
= unix_syscall_return
,
391 .absolutetime_to_microtime
= absolutetime_to_microtime
,
393 .proc_restore_workq_bgthreadpolicy
= proc_restore_workq_bgthreadpolicy
,
394 .proc_apply_workq_bgthreadpolicy
= proc_apply_workq_bgthreadpolicy
,
396 .get_bsdthread_info
= (void*)get_bsdthread_info
,
397 .thread_sched_call
= thread_sched_call
,
398 .thread_static_param
= thread_static_param
,
399 .thread_create_workq
= thread_create_workq
,
400 .thread_policy_set_internal
= thread_policy_set_internal
,
402 .thread_affinity_set
= thread_affinity_set
,
408 .__pthread_testcancel
= __pthread_testcancel
,
410 .mach_port_deallocate
= mach_port_deallocate
,
411 .semaphore_signal_internal_trap
= semaphore_signal_internal_trap
,
412 .current_map
= _current_map
,
413 .thread_create
= thread_create
,
414 .thread_resume
= thread_resume
,
416 .convert_thread_to_port
= convert_thread_to_port
,
417 .ml_get_max_cpus
= (void*)ml_get_max_cpus
,
420 .proc_get_dispatchqueue_serialno_offset
= proc_get_dispatchqueue_serialno_offset
,
421 .proc_set_dispatchqueue_serialno_offset
= proc_set_dispatchqueue_serialno_offset
,
424 pthread_callbacks_t pthread_kern
= &pthread_callbacks
;
425 pthread_functions_t pthread_functions
= NULL
;
428 * pthread_kext_register is called by pthread.kext upon load, it has to provide
429 * us with a function pointer table of pthread internal calls. In return, this
430 * file provides it with a table of function pointers it needs.
434 pthread_kext_register(pthread_functions_t fns
, pthread_callbacks_t
*callbacks
)
436 if (pthread_functions
!= NULL
) {
437 panic("Re-initialisation of pthread kext callbacks.");
440 if (callbacks
!= NULL
) {
441 *callbacks
= &pthread_callbacks
;
443 panic("pthread_kext_register called without callbacks pointer.");
447 pthread_functions
= fns
;