]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/pthread_shims.c
xnu-3247.10.11.tar.gz
[apple/xnu.git] / bsd / kern / pthread_shims.c
1 /*
2 * Copyright (c) 2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #define PTHREAD_INTERNAL 1
30
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>
48
49 /* version number of the in-kernel shims given to pthread.kext */
50 #define PTHREAD_SHIMS_VERSION 1
51
52 /* on arm, the callbacks function has two #ifdef arm ponters */
53 #define PTHREAD_CALLBACK_MEMBER ml_get_max_cpus
54
55 /* compile time asserts to check the length of structures in pthread_shims.h */
56 char pthread_functions_size_compile_assert[(sizeof(struct pthread_functions_s) - offsetof(struct pthread_functions_s, psynch_rw_yieldwrlock) - sizeof(void*)) == (sizeof(void*) * 100) ? 1 : -1];
57 char pthread_callbacks_size_compile_assert[(sizeof(struct pthread_callbacks_s) - offsetof(struct pthread_callbacks_s, PTHREAD_CALLBACK_MEMBER) - sizeof(void*)) == (sizeof(void*) * 100) ? 1 : -1];
58
59 /* old pthread code had definitions for these as they don't exist in headers */
60 extern kern_return_t mach_port_deallocate(ipc_space_t, mach_port_name_t);
61 extern kern_return_t semaphore_signal_internal_trap(mach_port_name_t);
62
63 #define PTHREAD_STRUCT_ACCESSOR(get, set, rettype, structtype, member) \
64 static rettype \
65 get(structtype x) { \
66 return (x)->member; \
67 } \
68 static void \
69 set(structtype x, rettype y) { \
70 (x)->member = y; \
71 }
72
73 PTHREAD_STRUCT_ACCESSOR(proc_get_threadstart, proc_set_threadstart, user_addr_t, struct proc*, p_threadstart);
74 PTHREAD_STRUCT_ACCESSOR(proc_get_pthsize, proc_set_pthsize, int, struct proc*, p_pthsize);
75 PTHREAD_STRUCT_ACCESSOR(proc_get_wqthread, proc_set_wqthread, user_addr_t, struct proc*, p_wqthread);
76 PTHREAD_STRUCT_ACCESSOR(proc_get_targconc, proc_set_targconc, user_addr_t, struct proc*, p_targconc);
77 PTHREAD_STRUCT_ACCESSOR(proc_get_stack_addr_hint, proc_set_stack_addr_hint, user_addr_t, struct proc *, p_stack_addr_hint);
78 PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_offset, proc_set_dispatchqueue_offset, uint64_t, struct proc*, p_dispatchqueue_offset);
79 PTHREAD_STRUCT_ACCESSOR(proc_get_dispatchqueue_serialno_offset, proc_set_dispatchqueue_serialno_offset, uint64_t, struct proc*, p_dispatchqueue_serialno_offset);
80 PTHREAD_STRUCT_ACCESSOR(proc_get_pthread_tsd_offset, proc_set_pthread_tsd_offset, uint32_t, struct proc *, p_pth_tsd_offset);
81 PTHREAD_STRUCT_ACCESSOR(proc_get_wqptr, proc_set_wqptr, void*, struct proc*, p_wqptr);
82 PTHREAD_STRUCT_ACCESSOR(proc_get_wqsize, proc_set_wqsize, int, struct proc*, p_wqsize);
83 PTHREAD_STRUCT_ACCESSOR(proc_get_pthhash, proc_set_pthhash, void*, struct proc*, p_pthhash);
84
85 PTHREAD_STRUCT_ACCESSOR(uthread_get_threadlist, uthread_set_threadlist, void*, struct uthread*, uu_threadlist);
86 PTHREAD_STRUCT_ACCESSOR(uthread_get_sigmask, uthread_set_sigmask, sigset_t, struct uthread*, uu_sigmask);
87 PTHREAD_STRUCT_ACCESSOR(uthread_get_returnval, uthread_set_returnval, int, struct uthread*, uu_rval[0]);
88
89 static void
90 pthread_returning_to_userspace(void)
91 {
92 thread_exception_return();
93 }
94
95 static uint32_t
96 get_task_threadmax(void) {
97 return task_threadmax;
98 }
99
100 static task_t
101 proc_get_task(struct proc *p) {
102 return p->task;
103 }
104
105 static lck_spin_t*
106 proc_get_wqlockptr(struct proc *p) {
107 return &(p->p_wqlock);
108 }
109
110 static boolean_t*
111 proc_get_wqinitingptr(struct proc *p) {
112 return &(p->p_wqiniting);
113 }
114
115 static uint64_t
116 proc_get_register(struct proc *p) {
117 return (p->p_lflag & P_LREGISTER);
118 }
119
120 static void
121 proc_set_register(struct proc *p) {
122 proc_setregister(p);
123 }
124
125 static void*
126 uthread_get_uukwe(struct uthread *t)
127 {
128 return &t->uu_kevent.uu_kwe;
129 }
130
131 static int
132 uthread_is_cancelled(struct uthread *t)
133 {
134 return (t->uu_flag & (UT_CANCELDISABLE | UT_CANCEL | UT_CANCELED)) == UT_CANCEL;
135 }
136
137 static vm_map_t
138 _current_map(void)
139 {
140 return current_map();
141 }
142
143 static boolean_t
144 qos_main_thread_active(void)
145 {
146 return TRUE;
147 }
148
149
150 static int proc_usynch_get_requested_thread_qos(struct uthread *uth)
151 {
152 task_t task = current_task();
153 thread_t thread = uth ? uth->uu_thread : current_thread();
154 int requested_qos;
155
156 requested_qos = proc_get_task_policy(task, thread, TASK_POLICY_ATTRIBUTE, TASK_POLICY_QOS);
157
158 /*
159 * For the purposes of userspace synchronization, it doesn't make sense to place an override of UNSPECIFIED
160 * on another thread, if the current thread doesn't have any QoS set. In these cases, upgrade to
161 * THREAD_QOS_USER_INTERACTIVE.
162 */
163 if (requested_qos == THREAD_QOS_UNSPECIFIED) {
164 requested_qos = THREAD_QOS_USER_INTERACTIVE;
165 }
166
167 return requested_qos;
168 }
169
170 static boolean_t proc_usynch_thread_qos_add_override(struct uthread *uth, uint64_t tid, int override_qos, boolean_t first_override_for_resource)
171 {
172 task_t task = current_task();
173 thread_t thread = uth ? uth->uu_thread : THREAD_NULL;
174
175 return proc_thread_qos_add_override(task, thread, tid, override_qos, first_override_for_resource, USER_ADDR_NULL, THREAD_QOS_OVERRIDE_TYPE_UNKNOWN);
176 }
177
178 static boolean_t proc_usynch_thread_qos_remove_override(struct uthread *uth, uint64_t tid)
179 {
180 task_t task = current_task();
181 thread_t thread = uth ? uth->uu_thread : THREAD_NULL;
182
183 return proc_thread_qos_remove_override(task, thread, tid, USER_ADDR_NULL, THREAD_QOS_OVERRIDE_TYPE_UNKNOWN);
184 }
185
186 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)
187 {
188 thread_t thread = uth ? uth->uu_thread : THREAD_NULL;
189
190 return proc_thread_qos_add_override(task, thread, tid, override_qos, first_override_for_resource, resource, resource_type);
191 }
192
193 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)
194 {
195 thread_t thread = uth ? uth->uu_thread : THREAD_NULL;
196
197 return proc_thread_qos_remove_override(task, thread, tid, resource, resource_type);
198 }
199
200 static boolean_t proc_usynch_thread_qos_reset_override_for_resource(task_t task, struct uthread *uth, uint64_t tid, user_addr_t resource, int resource_type)
201 {
202 thread_t thread = uth ? uth->uu_thread : THREAD_NULL;
203
204 return proc_thread_qos_reset_override(task, thread, tid, resource, resource_type);
205 }
206
207 /* kernel (core) to kext shims */
208
209 void
210 pthread_init(void)
211 {
212 if (!pthread_functions) {
213 panic("pthread kernel extension not loaded (function table is NULL).");
214 }
215 pthread_functions->pthread_init();
216 }
217
218 int
219 fill_procworkqueue(proc_t p, struct proc_workqueueinfo * pwqinfo)
220 {
221 return pthread_functions->fill_procworkqueue(p, pwqinfo);
222 }
223
224 void
225 workqueue_init_lock(proc_t p)
226 {
227 pthread_functions->workqueue_init_lock(p);
228 }
229
230 void
231 workqueue_destroy_lock(proc_t p)
232 {
233 pthread_functions->workqueue_destroy_lock(p);
234 }
235
236 void
237 workqueue_exit(struct proc *p)
238 {
239 pthread_functions->workqueue_exit(p);
240 }
241
242 void
243 workqueue_mark_exiting(struct proc *p)
244 {
245 pthread_functions->workqueue_mark_exiting(p);
246 }
247
248 void
249 workqueue_thread_yielded(void)
250 {
251 pthread_functions->workqueue_thread_yielded();
252 }
253
254 sched_call_t
255 workqueue_get_sched_callback(void)
256 {
257 if (pthread_functions->workqueue_get_sched_callback) {
258 return pthread_functions->workqueue_get_sched_callback();
259 }
260 return NULL;
261 }
262
263 void
264 pth_proc_hashinit(proc_t p)
265 {
266 pthread_functions->pth_proc_hashinit(p);
267 }
268
269 void
270 pth_proc_hashdelete(proc_t p)
271 {
272 pthread_functions->pth_proc_hashdelete(p);
273 }
274
275 /* syscall shims */
276 int
277 bsdthread_create(struct proc *p, struct bsdthread_create_args *uap, user_addr_t *retval)
278 {
279 return pthread_functions->bsdthread_create(p, uap->func, uap->func_arg, uap->stack, uap->pthread, uap->flags, retval);
280 }
281
282 int
283 bsdthread_register(struct proc *p, struct bsdthread_register_args *uap, __unused int32_t *retval)
284 {
285 if (pthread_functions->version >= 1) {
286 return pthread_functions->bsdthread_register2(p, uap->threadstart, uap->wqthread,
287 uap->flags, uap->stack_addr_hint,
288 uap->targetconc_ptr, uap->dispatchqueue_offset,
289 uap->tsd_offset, retval);
290 } else {
291 return pthread_functions->bsdthread_register(p, uap->threadstart, uap->wqthread,
292 uap->flags, uap->stack_addr_hint,
293 uap->targetconc_ptr, uap->dispatchqueue_offset,
294 retval);
295 }
296 }
297
298 int
299 bsdthread_terminate(struct proc *p, struct bsdthread_terminate_args *uap, int32_t *retval)
300 {
301 return pthread_functions->bsdthread_terminate(p, uap->stackaddr, uap->freesize, uap->port, uap->sem, retval);
302 }
303
304 int
305 bsdthread_ctl(struct proc *p, struct bsdthread_ctl_args *uap, int *retval)
306 {
307 return pthread_functions->bsdthread_ctl(p, uap->cmd, uap->arg1, uap->arg2, uap->arg3, retval);
308 }
309
310
311 int
312 thread_selfid(struct proc *p, __unused struct thread_selfid_args *uap, uint64_t *retval)
313 {
314 return pthread_functions->thread_selfid(p, retval);
315 }
316
317 int
318 workq_kernreturn(struct proc *p, struct workq_kernreturn_args *uap, int32_t *retval)
319 {
320 return pthread_functions->workq_kernreturn(p, uap->options, uap->item, uap->affinity, uap->prio, retval);
321 }
322
323 int
324 workq_open(struct proc *p, __unused struct workq_open_args *uap, int32_t *retval)
325 {
326 return pthread_functions->workq_open(p, retval);
327 }
328
329 /* pthread synchroniser syscalls */
330
331 int
332 psynch_mutexwait(proc_t p, struct psynch_mutexwait_args *uap, uint32_t *retval)
333 {
334 return pthread_functions->psynch_mutexwait(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval);
335 }
336
337 int
338 psynch_mutexdrop(proc_t p, struct psynch_mutexdrop_args *uap, uint32_t *retval)
339 {
340 return pthread_functions->psynch_mutexdrop(p, uap->mutex, uap->mgen, uap->ugen, uap->tid, uap->flags, retval);
341 }
342
343 int
344 psynch_cvbroad(proc_t p, struct psynch_cvbroad_args *uap, uint32_t *retval)
345 {
346 return pthread_functions->psynch_cvbroad(p, uap->cv, uap->cvlsgen, uap->cvudgen, uap->flags, uap->mutex, uap->mugen, uap->tid, retval);
347 }
348
349 int
350 psynch_cvsignal(proc_t p, struct psynch_cvsignal_args *uap, uint32_t *retval)
351 {
352 return pthread_functions->psynch_cvsignal(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->thread_port, uap->mutex, uap->mugen, uap->tid, uap->flags, retval);
353 }
354
355 int
356 psynch_cvwait(proc_t p, struct psynch_cvwait_args * uap, uint32_t * retval)
357 {
358 return pthread_functions->psynch_cvwait(p, uap->cv, uap->cvlsgen, uap->cvugen, uap->mutex, uap->mugen, uap->flags, uap->sec, uap->nsec, retval);
359 }
360
361 int
362 psynch_cvclrprepost(proc_t p, struct psynch_cvclrprepost_args * uap, int *retval)
363 {
364 return pthread_functions->psynch_cvclrprepost(p, uap->cv, uap->cvgen, uap->cvugen, uap->cvsgen, uap->prepocnt, uap->preposeq, uap->flags, retval);
365 }
366
367 int
368 psynch_rw_longrdlock(proc_t p, struct psynch_rw_longrdlock_args * uap, uint32_t *retval)
369 {
370 return pthread_functions->psynch_rw_longrdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
371 }
372
373 int
374 psynch_rw_rdlock(proc_t p, struct psynch_rw_rdlock_args * uap, uint32_t * retval)
375 {
376 return pthread_functions->psynch_rw_rdlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
377 }
378
379 int
380 psynch_rw_unlock(proc_t p, struct psynch_rw_unlock_args *uap, uint32_t *retval)
381 {
382 return pthread_functions->psynch_rw_unlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
383 }
384
385 int
386 psynch_rw_unlock2(__unused proc_t p, __unused struct psynch_rw_unlock2_args *uap, __unused uint32_t *retval)
387 {
388 return ENOTSUP;
389 }
390
391 int
392 psynch_rw_wrlock(proc_t p, struct psynch_rw_wrlock_args *uap, uint32_t *retval)
393 {
394 return pthread_functions->psynch_rw_wrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
395 }
396
397 int
398 psynch_rw_yieldwrlock(proc_t p, struct psynch_rw_yieldwrlock_args *uap, uint32_t *retval)
399 {
400 return pthread_functions->psynch_rw_yieldwrlock(p, uap->rwlock, uap->lgenval, uap->ugenval, uap->rw_wc, uap->flags, retval);
401 }
402
403 int
404 psynch_rw_upgrade(__unused proc_t p, __unused struct psynch_rw_upgrade_args * uap, __unused uint32_t *retval)
405 {
406 return 0;
407 }
408
409 int
410 psynch_rw_downgrade(__unused proc_t p, __unused struct psynch_rw_downgrade_args * uap, __unused int *retval)
411 {
412 return 0;
413 }
414
415 /*
416 * The callbacks structure (defined in pthread_shims.h) contains a collection
417 * of kernel functions that were not deemed sensible to expose as a KPI to all
418 * kernel extensions. So the kext is given them in the form of a structure of
419 * function pointers.
420 */
421 static struct pthread_callbacks_s pthread_callbacks = {
422 .version = PTHREAD_SHIMS_VERSION,
423 .config_thread_max = CONFIG_THREAD_MAX,
424 .get_task_threadmax = get_task_threadmax,
425
426 .proc_get_threadstart = proc_get_threadstart,
427 .proc_set_threadstart = proc_set_threadstart,
428 .proc_get_pthsize = proc_get_pthsize,
429 .proc_set_pthsize = proc_set_pthsize,
430 .proc_get_wqthread = proc_get_wqthread,
431 .proc_set_wqthread = proc_set_wqthread,
432 .proc_get_targconc = proc_get_targconc,
433 .proc_set_targconc = proc_set_targconc,
434 .proc_get_dispatchqueue_offset = proc_get_dispatchqueue_offset,
435 .proc_set_dispatchqueue_offset = proc_set_dispatchqueue_offset,
436 .proc_get_wqptr = proc_get_wqptr,
437 .proc_set_wqptr = proc_set_wqptr,
438 .proc_get_wqsize = proc_get_wqsize,
439 .proc_set_wqsize = proc_set_wqsize,
440 .proc_get_wqlockptr = proc_get_wqlockptr,
441 .proc_get_wqinitingptr = proc_get_wqinitingptr,
442 .proc_get_pthhash = proc_get_pthhash,
443 .proc_set_pthhash = proc_set_pthhash,
444 .proc_get_task = proc_get_task,
445 .proc_lock = proc_lock,
446 .proc_unlock = proc_unlock,
447 .proc_get_register = proc_get_register,
448 .proc_set_register = proc_set_register,
449
450 /* kernel IPI interfaces */
451 .ipc_port_copyout_send = ipc_port_copyout_send,
452 .task_get_ipcspace = get_task_ipcspace,
453 .vm_map_page_info = vm_map_page_info,
454 .vm_map_switch = vm_map_switch,
455 .thread_set_wq_state32 = thread_set_wq_state32,
456 .thread_set_wq_state64 = thread_set_wq_state64,
457
458 .uthread_get_threadlist = uthread_get_threadlist,
459 .uthread_set_threadlist = uthread_set_threadlist,
460 .uthread_get_sigmask = uthread_get_sigmask,
461 .uthread_set_sigmask = uthread_set_sigmask,
462 .uthread_get_uukwe = uthread_get_uukwe,
463 .uthread_get_returnval = uthread_get_returnval,
464 .uthread_set_returnval = uthread_set_returnval,
465 .uthread_is_cancelled = uthread_is_cancelled,
466
467 .thread_exception_return = pthread_returning_to_userspace,
468 .thread_bootstrap_return = thread_bootstrap_return,
469 .unix_syscall_return = unix_syscall_return,
470
471 .absolutetime_to_microtime = absolutetime_to_microtime,
472
473 .proc_restore_workq_bgthreadpolicy = proc_restore_workq_bgthreadpolicy,
474 .proc_apply_workq_bgthreadpolicy = proc_apply_workq_bgthreadpolicy,
475
476 .get_bsdthread_info = (void*)get_bsdthread_info,
477 .thread_sched_call = thread_sched_call,
478 .thread_static_param = thread_static_param,
479 .thread_create_workq = thread_create_workq,
480 .thread_policy_set_internal = thread_policy_set_internal,
481 .thread_policy_get = thread_policy_get,
482 .thread_set_voucher_name = thread_set_voucher_name,
483
484 .thread_affinity_set = thread_affinity_set,
485
486 .zalloc = zalloc,
487 .zfree = zfree,
488 .zinit = zinit,
489
490 .__pthread_testcancel = __pthread_testcancel,
491
492 .mach_port_deallocate = mach_port_deallocate,
493 .semaphore_signal_internal_trap = semaphore_signal_internal_trap,
494 .current_map = _current_map,
495 .thread_create = thread_create,
496 .thread_resume = thread_resume,
497
498 .convert_thread_to_port = convert_thread_to_port,
499 .ml_get_max_cpus = (void*)ml_get_max_cpus,
500
501
502 .proc_get_dispatchqueue_serialno_offset = proc_get_dispatchqueue_serialno_offset,
503 .proc_set_dispatchqueue_serialno_offset = proc_set_dispatchqueue_serialno_offset,
504
505 .proc_get_stack_addr_hint = proc_get_stack_addr_hint,
506 .proc_set_stack_addr_hint = proc_set_stack_addr_hint,
507 .proc_get_pthread_tsd_offset = proc_get_pthread_tsd_offset,
508 .proc_set_pthread_tsd_offset = proc_set_pthread_tsd_offset,
509
510 .thread_set_tsd_base = thread_set_tsd_base,
511
512 .proc_usynch_get_requested_thread_qos = proc_usynch_get_requested_thread_qos,
513 .proc_usynch_thread_qos_add_override = proc_usynch_thread_qos_add_override,
514 .proc_usynch_thread_qos_remove_override = proc_usynch_thread_qos_remove_override,
515
516 .qos_main_thread_active = qos_main_thread_active,
517
518 .proc_usynch_thread_qos_add_override_for_resource = proc_usynch_thread_qos_add_override_for_resource,
519 .proc_usynch_thread_qos_remove_override_for_resource = proc_usynch_thread_qos_remove_override_for_resource,
520 .proc_usynch_thread_qos_reset_override_for_resource = proc_usynch_thread_qos_reset_override_for_resource,
521 };
522
523 pthread_callbacks_t pthread_kern = &pthread_callbacks;
524 pthread_functions_t pthread_functions = NULL;
525
526 /*
527 * pthread_kext_register is called by pthread.kext upon load, it has to provide
528 * us with a function pointer table of pthread internal calls. In return, this
529 * file provides it with a table of function pointers it needs.
530 */
531
532 void
533 pthread_kext_register(pthread_functions_t fns, pthread_callbacks_t *callbacks)
534 {
535 if (pthread_functions != NULL) {
536 panic("Re-initialisation of pthread kext callbacks.");
537 }
538
539 if (callbacks != NULL) {
540 *callbacks = &pthread_callbacks;
541 } else {
542 panic("pthread_kext_register called without callbacks pointer.");
543 }
544
545 if (fns) {
546 pthread_functions = fns;
547 }
548 }